这软件包 csvsimple是一款不错的工具,可用于导入外部生成的大型表格。但是,它无法解释标头中带有符号或数学符号的文件。它不适用于 tabular 或 longtable 或任何其他 csvsimple 变体。为什么会这样,如何解决?
来自的问题csvsimple 导入的数学模式通过将“无头”作为选项来“解决”它,但是,嗯,没有头。
文件.csv:
a, $\delta$, \euro
b, c, x
Tex MWE:
\documentclass{book}
\usepackage[english]{babel}
\usepackage{csvsimple,longtable,booktabs}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{eurosym,textcomp}
\begin{document}
% this fails:
\csvautotabular{file.csv}
% and this fails:
\csvreader[tabular=lll]{file.csv}{}{\csvcoli & \csvcolii & \csvcoliii}
\end{document}
错误:
! Missing \endcsname inserted.
<to be read again>
\delta
l.9 ...t.csv}{}{\csvcoli & \csvcolii & \csvcoliii}
答案1
如果不设置no head
,则标题行的条目将用作列的宏名称。
如果它们包含空格或其他特殊字符,则它们不能用作可行的 LaTeX 宏名称,这就是出现错误的原因。
但是,您可以设置no head
(或等效的head=false
)并使用该选项late after first line
将第一行视为标题。
编辑:通过一些过滤,您还可以管理longtable
。
\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{caption}
\usepackage{csvsimple,longtable,booktabs}
\usepackage{eurosym,textcomp}
% this code is only to create file.csv, you don't need it:
\usepackage{filecontents}
\begin{filecontents*}{file.csv}
a, $\delta$, \euro
b, c, x
d, e, y
f, g, z
\end{filecontents*}
\begin{filecontents*}{longfile.csv}
a, $\delta$, \euro
b, c, x
d, e, y
f, g, z
This, is, a
long, table, !
This, is, a
long, table, !
This, is, a
long, table, !
This, is, a
long, table, !
This, is, a
long, table, !
This, is, a
long, table, !
This, is, a
long, table, !
This, is, a
long, table, !
This, is, a
long, table, !
This, is, a
long, table, !
This, is, a
long, table, !
This, is, a
long, table, !
This, is, a
long, table, !
This, is, a
long, table, !
This, is, a
long, table, !
This, is, a
long, table, !
This, is, a
long, table, !
This, is, a
long, table, !
This, is, a
long, table, !
This, is, a
long, table, !
This, is, a
long, table, !
This, is, a
long, table, !
This, is, a
long, table, !
This, is, a
long, table, !
This, is, a
long, table, !
This, is, a
long, table, !
This, is, a
long, table, !
This, is, a
long, table, !
\end{filecontents*}
\begin{document}
\begin{table}[htb]
\centering
\caption{A short table}
\csvreader[tabular=lll,
head=false,
table head=\toprule,
late after line=\\,
late after first line=\\\midrule,
table foot=\bottomrule,
]{file.csv}{}{\csvcoli & \csvcolii & \csvcoliii}
\end{table}
\begin{longtable}{lll}
\caption{A long Table}\\
\toprule
\csvreader[
head=false,
late after line=\\,
filter equal={\thecsvinputline}{1},
]{longfile.csv}{}{\csvcoli & \csvcolii & \csvcoliii}
\midrule
\endfirsthead
\toprule
\csvreader[
head=false,
late after line=\\,
filter equal={\thecsvinputline}{1},
]{longfile.csv}{}{\csvcoli & \csvcolii & \csvcoliii}
\midrule
\endhead
\midrule
\endfoot
\bottomrule
\endlastfoot
\csvreader[
head=false,
late after line=\\,
filter not equal={\thecsvinputline}{1},
]{longfile.csv}{}{\csvcoli & \csvcolii & \csvcoliii}
\end{longtable}
\end{document}