TransWikia.com

csvsimple - process column names with digits correctly

TeX - LaTeX Asked by codebat on July 8, 2021

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 & ndcg5midrule,
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 & ndcg5midrule 
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?

2 Answers

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-5midrule,
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 
midrulecsvreader[separator=semicolon, late after line=]{ndcgs.csv}{}%
     {csvcoli & csvcolii}
bottomrule
end{tabular}
end{center} 

end{document}

enter image description here

Correct answer by CarLaTeX on July 8, 2021

Of course it is possible to do this: you just need to use csuse from the etoolbox package:

documentclass{article}

usepackage[utf8]{inputenc}
usepackage{csvsimple}
usepackage{booktabs}
usepackage{filecontents}
usepackage{etoolbox}

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 & ndcg5midrule,
table foot=bottomrule,
head to column names,
separator=semicolon}

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

end{document}

to produce:

enter image description here

Answered by 31415926535897932384 on July 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP