1

I have a csv that contains column names with digits. Somehow this seems to be a problem for csvsimple, but I can't quite figure out why and how to deal with it correctly.

Here's what I want to achieve:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage{csvsimple}
\usepackage{filecontents}

\begin{filecontents*}{ndcgs.csv}
system;ndcg5;ndcg10;ndcg15;ndcg20;ndcg30;ndcg100;ndcg200;ndcg500;ndcg1000
A;0.632;0.566;0.531;0.533;0.511;0.475;0.396;0.301;0.301
B;0.646;0.615;0.598;0.588;0.563;0.536;0.517;0.401;0.363
C;0.557;0.567;0.589;0.576;0.573;0.511;0.478;0.429;0.380
D;0.644;0.577;0.538;0.540;0.518;0.473;0.398;0.303;0.302
E;0.291;0.304;0.302;0.330;0.351;0.361;0.281;0.238;0.236
\end{filecontents*}

\begin{document}

\csvstyle{myTableStyle}{tabular=lc,
table head=\toprule system & ndcg5\\\midrule,
table foot=\bottomrule,
head to column names,
separator=semicolon}

\csvreader[myTableStyle]{ndcgs.csv}{}%
    {\system & \ndcg5}

\end{document}

The error message complains about an undefined control sequence, related to the column name "ndcg5":

! Undefined control sequence.
\csv@tablehead ->\toprule 
                      system & ndcg5\\\midrule 
l.25         {\system & \ndcg5}

I tried to prevent this by using \csvcolii etc but still the same error.

I found that the following works:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage{csvsimple}
\usepackage{filecontents}

\begin{filecontents*}{ndcgs.csv}
system;ndcg5;ndcg10;ndcg15;ndcg20;ndcg30;ndcg100;ndcg200;ndcg500;ndcg1000
A;0.632;0.566;0.531;0.533;0.511;0.475;0.396;0.301;0.301
B;0.646;0.615;0.598;0.588;0.563;0.536;0.517;0.401;0.363
C;0.557;0.567;0.589;0.576;0.573;0.511;0.478;0.429;0.380
D;0.644;0.577;0.538;0.540;0.518;0.473;0.398;0.303;0.302
E;0.291;0.304;0.302;0.330;0.351;0.361;0.281;0.238;0.236
\end{filecontents*}

\begin{document}

\begin{tabular}{lc}%
\bfseries system & \bfseries ndcg-5%
    \csvreader[separator=semicolon]{ndcgs.csv}{}%
     {\\ \csvcoli & \csvcolii}%
 \end{tabular}

\end{document}

But I don't see how this approach would allow me to define the table layout (regarding toprule, midrule etc.) in the same way as in the example above.

Can anyone help?

  • "I tried to prevent this by using \csvcolii etc but still the same error." -> To be more precise, I can press enter a few times when the error appears in this case and at some point, the pdf finally gets generated, but this is not really helpful when using make tools like arara. – codebat Jan 23 at 16:28
0

You can't use names with numbers as a macro to indicate the column names simply because you can't use numbers in TeX macros, see here: https://texfaq.org/FAQ-linmacnames.

Your problem, however, is easily solvable with \csvcoli..., two examples follow.

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage{csvsimple}
\usepackage{booktabs}

\usepackage{filecontents}
\begin{filecontents*}{ndcgs.csv}
system;ndcg5;ndcg10;ndcg15;ndcg20;ndcg30;ndcg100;ndcg200;ndcg500;ndcg1000
A;0.632;0.566;0.531;0.533;0.511;0.475;0.396;0.301;0.301
B;0.646;0.615;0.598;0.588;0.563;0.536;0.517;0.401;0.363
C;0.557;0.567;0.589;0.576;0.573;0.511;0.478;0.429;0.380
D;0.644;0.577;0.538;0.540;0.518;0.473;0.398;0.303;0.302
E;0.291;0.304;0.302;0.330;0.351;0.361;0.281;0.238;0.236
\end{filecontents*}

\begin{document}

First method, table formatting as \verb|\csvreader| options:

\csvstyle{myTableStyle}{tabular=lc,
table head=\toprule \bfseries system & \bfseries ndcg-5\\\midrule,
table foot=\bottomrule,
head to column names,
separator=semicolon}
\begin{center}
\csvreader[myTableStyle]{ndcgs.csv}{}%
    {\csvcoli & \csvcolii}
\end{center}    

Second method, \verb|tabular| with a \verb|\csvreader| inside:

\begin{center}
\begin{tabular}{lc}
\toprule
\bfseries system & \bfseries ndcg-5 \\
\midrule\csvreader[separator=semicolon, late after line=\\]{ndcgs.csv}{}%
     {\csvcoli & \csvcolii}
\bottomrule
\end{tabular}
\end{center} 

\end{document}

enter image description here

  • Thank you. As I said in the comment, I already tried to use \csvcoli... and also got the "Undefined control sequence" error. Comparing with your example I saw that you also imported the booktabs package which I didn't. After importing it, it works. – codebat Jan 30 at 15:36
  • @codebat Yes, your error probably was only that! – CarLaTeX Jan 30 at 15:39
  • Sometimes it's the small things... But I also learned through your second example how to achieve the table layout I wanted in this case. Thanks. – codebat Jan 30 at 15:41
  • @codebat You're welcome! – CarLaTeX Jan 30 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.