T-F-S / csvsimple

A LaTeX package for lightweight CSV file processing.
http://www.ctan.org/pkg/csvsimple
LaTeX Project Public License v1.3c
24 stars 5 forks source link

Problem defining `xltabular` style. #8

Closed mikkelee closed 5 years ago

mikkelee commented 5 years ago

I have a table with contents that are both very wide and very long, thus I would like to use xltabular (basically longtable + tabularx). I have tried creating a custom style, but I get the error:

./tabletest.tex:40: Undefined control sequence.
\csv@table@begin ->\csv 
                        @pretable\begin {xltabular}{\linewidth }{l l X}\csv ...

l.40 }

MWE:

\documentclass{article}

\usepackage{filecontents}

\usepackage{csvsimple}
\usepackage{xltabular}
\usepackage{booktabs}

\csvset{%
  xltabular/.style 2 args={
    @table={\csv@pretable\begin{xltabular}{#1}{#2}\csv@tablehead}{\csv@tablefoot\end{xltabular}\csv@posttable},
    late after line=\\},
}

% full file is ~500 lines
\begin{filecontents*}{\jobname.tsv}
codepoint,entity,name,versions
E262,OEligogon,LATIN CAPITAL LIGATURE OE WITH OGONEK,MUFI: 4.0
E268,Pdblac,LATIN CAPITAL LETTER P WITH DOUBLE ACUTE,MUFI: 4.0
E34E,Vvertline,LATIN CAPITAL LETTER V WITH VERTICAL LINE ABOVE,MUFI: 4.0
E662,oeligogon,LATIN SMALL LIGATURE OE WITH OGONEK,MUFI: 4.0
E668,pdblac,LATIN SMALL LETTER P WITH DOUBLE ACUTE,MUFI: 4.0
F71C,metrdblbrevemacrdblac,METRICAL SYMBOL RESOLVED LIFT WITH DOUBLE ACUTE (PRIMARY STRESS AND ALLITERATION),MUFI: 3.0
\end{filecontents*}

\begin{document}

% this works, but the table is too wide:
\csvautolongtable[
    head to column names,
]{\jobname.tsv}

% this breaks as described above:
\csvloop{
    file={\jobname.tsv},
    xltabular={\linewidth}{l l X},
    table head=\toprule Code Point & Entity Name & Unicode Name\\\midrule\endhead\bottomrule\endfoot,
    late after line=\\,
    %command=\csvcoli & \csvcolii & \csvcoliii,
    command=\csvlinetotablerow,
}

\end{document}
T-F-S commented 5 years ago

This error message results from a missing \makeatletter and \makeatother around your \csvset.

But, with it, it gives another error message

! File ended while scanning use of \TX@get@body.

I do not know the xltabular package, but from the error message I guess that this package needs to read the whole table into a macro for further processing. If I am right, then it is not compatible with csvsimple.

But, you could try your luck at https://tex.stackexchange.com. Maybe, somebody there can help (?).

mikkelee commented 5 years ago

Aha, that could be it; tabularx which it is based on defines a column type X that adjusts to the width of the table, that might explain why it would need to read the whole table. I will experiment with running a \csvloop instead.

mikkelee commented 5 years ago

Alright, I got the table working with \csvloop, but if I use an external file with tab separators, it only reads the first line. Do I need to do something special regarding linefeeds? The file has Unix/LF endings. It's available here: https://github.com/mikkelee/latex-pua/blob/master/latex/unicode-alphabets.mufi.tsv

Skærmbillede 2019-07-17 kl  10 36 01

\begingroup
\scriptsize
\begin{xltabular}{\linewidth}{r l X l}
\toprule
Code Point & Entity Name & Unicode Name & Version\\
\midrule
\endhead
\bottomrule
\endfoot
\csvloop{
    file={unicode-alphabets.mufi.tsv},
    separator=tab,
    late after line=\\,
    command=\csvlinetotablerow,
}
\end{xltabular}
\endgroup

Also, it seems to get stuck in an infinite loop if I use command=\csvcoli & \csvcolii & \csvcoliii & \csvcoliv instead of \csvlinetotablerow (my goal is inserting an extra column with each glyph using \mufi{\csvcoli} from my above package)

T-F-S commented 5 years ago

I have no clue, why tab separators do not work in combination with xltabular. They do work inside a normal tabular.

With commas, your example compiles as expected.

You can replace \csvlinetotablerow with an own command, but this has to be definded where xltabular does no 'see' it, e.g.

\def\mycommand{\csvcoli & \csvcolii & \csvcoliii & \csvcoliv}

\begingroup
\scriptsize
\begin{xltabular}{\linewidth}{r l X l}
\toprule
Code Point & Entity Name & Unicode Name & Version\\
\midrule
\endhead
\bottomrule
\endfoot
\csvloop{
    file={unicode-alphabets.mufi4.tsv},% <- with commas
    %separator=tab,
    late after line=\\,
    command=\mycommand,
    %command=\csvlinetotablerow,
}
\end{xltabular}
\endgroup
mikkelee commented 5 years ago

Many thanks for the help, I guess I'll have to change my .tsv files to .csv