我正在尝试使用 csvsimple 读取包含特殊字符(本例中为下划线)元素的文件,并创建仅包含部分列的表格。我可以读取文件(下面的 test.csv)并使用自动制表和“尊重所有”选项生成其内容的表格(这有效)。但是,当我尝试创建具有特定列名的表格时,“尊重所有”选项似乎不起作用。如果我从 csv 文件中删除下划线,下面的机器人示例就可以正常工作。我需要将 \a 的内容放入某种逐字环境中吗?
\documentclass[]{article}
\usepackage{csvsimple}
\usepackage{booktabs}
\usepackage[T1]{fontenc}
\begin{document}
% This works
\csvreader[%
respect all,%
autotabular%
]{test.csv}{}{\csvlinetotablerow}%
% This does not
\begin{tabular}{|l|c|}\hline%
\bfseries Col A & \bfseries Col B
\csvreader[%
respect all,%
head to column names
]{test.csv}{}%
{\\\a & \b}%
\\\hline
\end{tabular}
\end{document}
测试.csv:
a,b,c,d
b_1,2,3,4
b_5,6,7,8
b_9,10,11,12
答案1
\documentclass[]{article}
% \usepackage{csvsimple} % remove this for second solution
\usepackage{datatool} % add this for second solution
\usepackage{booktabs}
\usepackage[T1]{fontenc}
% \usepackage{underscore} %<- This solves your problem % remove this for second solution
\begin{document}
\section{csvreader}
% This works
% \csvreader[%
% respect all,%
% autotabular%
% ]{test.csv}{}{\csvlinetotablerow}%
\section{datatool simple}
\DTLloadrawdb[noheader,keys={a,b,c,d},headers={a,b,c,d}]{t2g}{test.csv}
\begin{table}[htbp]
\centering
\begin{tabular}{|c|c|c|c|}\hline
\DTLdisplaydb{t2g}
\\ \hline
\end{tabular}
\end{table}
% \DTLdisplaylongdb{iris}
\section{datatool not simple}
% This does not
\begin{table}[htbp]
\centering
\begin{tabular}{|c|c|c|c|}\hline
\DTLforeach{t2g}{\ca=a, \cb=b, \cc=c,\cd=d}%
{%
\DTLiffirstrow{}%
{%
\\ \hline
}
\ca&%
\cb&%
\cc&%
\cd%
}%
\\ \hline
\end{tabular}
\end{table}
\end{document}
这个解决方案怎么样?;)
答案2
以下应该有效:
\documentclass[]{article}
\usepackage{csvsimple}
\usepackage{booktabs}
\usepackage[T1]{fontenc}
\begin{document}
% This works
\csvreader[%
respect all,%
autotabular%
]{test.csv}{}{\csvlinetotablerow}%
\csvreader[%
respect all,%
head to column names,
tabular=|l|c|,
table head=\hline\bfseries Col A & \bfseries Col B\\,
late after last line=\\\hline,
]{test.csv}{}%
{\a & \b}%
\end{document}
您的尝试没有成功,因为respect ...
选项会在您的代码中更改下划线等的 catcode \csvreader
,但下次再更改时&
这些 catcode 设置又会恢复原状。
解决方案是在表格开始前更改 catcode。这是我的答案中的代码完成的。或者,从 Peter Ebelsberger 的答案中包含包underscore
也会在表格开始前更改 catcode。
答案3
\documentclass[]{article}
\usepackage{csvsimple}
\usepackage{booktabs}
\usepackage[T1]{fontenc}
\usepackage{underscore} %<- This solves your problem
\begin{document}
% This works
\csvreader[%
respect all,%
autotabular%
]{test.csv}{}{\csvlinetotablerow}%
% This does not
\begin{tabular}{|l|c|}\hline%
\bfseries Col A & \bfseries Col B
\csvreader[%
respect all,%
head to column names
]{test.csv}{}%
{\\\a & \b}%
\\\hline
\end{tabular}
\end{document}