我有一个 csv,其中包含带数字的列名。不知何故,这似乎是 csvsimple 的问题,但我不太清楚为什么以及如何正确处理它。
这是我想要实现的目标:
\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}
错误消息抱怨未定义的控制序列,与列名“ndcg5”相关:
! Undefined control sequence.
\csv@tablehead ->\toprule
system & ndcg5\\\midrule
l.25 {\system & \ndcg5}
我尝试使用\csvcolii
等来防止这种情况,但仍然出现相同的错误。
我发现以下方法有效:
\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}
但是我不明白这种方法如何允许我以与上述示例相同的方式定义表格布局(关于toprule,midrule等)。
有人可以帮忙吗?
答案1
您不能使用带有数字的名称作为宏来指示列名,因为您不能在 TeX 宏中使用数字,请参见此处:https://texfaq.org/FAQ-linmacnames。
不过,您的问题可以通过 轻松解决\csvcoli...
,以下是两个例子。
\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}
答案2
当然可以这样做:你只需要\csuse
使用电子工具箱包裹:
\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 & ndcg5\\\midrule,
table foot=\bottomrule,
head to column names,
separator=semicolon}
\csvreader[myTableStyle]{ndcgs.csv}{}%
{\system & \csuse{ndcg5}}
\end{document}
生产: