不能使用带有“\char"FB”的前缀:将 csv 导入 latex 时出现错误

不能使用带有“\char"FB”的前缀:将 csv 导入 latex 时出现错误

我正在尝试使用 csvsimple 包从 csv 文件创建表格。到目前为止,这种方法有效,但当我尝试添加新的图形/表格时,我收到多个错误。看来我的表格的 \end 语句无法正确识别。

\documentclass{article}
\usepackage{graphicx}
\usepackage[textwidth=15cm,textheight=20cm]{geometry}
\usepackage{csvsimple}


\begin{document}

\begin{table}
\centering
\begin{tabular}{|c|c|c|} \hline col1 & col2 & col3 \\ \hline
\csvreader[head to column names, late after line=\\\hline]{myCSV.csv}{}{\count & \columnii & \columniii}
\end{tabular}
\caption{myCaption}
\label{tbl:top40}
\end{table}

\begin{figure}
% any figure here
\end{figure}

\end{document}

这给了我以下错误消息:

./test2.tex:22: You can't use a prefix with `\char"FB'. [\end]
./test2.tex:22: Missing number, treated as zero. [\end]
./test2.tex:22: Improper \prevdepth. [\end{figure}]
./test2.tex:22: You can't use `\prevdepth' in horizontal mode. [\end{figure}]
./test2.tex:22: Missing number, treated as zero. [\end{figure}]
./test2.tex:22: Illegal unit of measure (pt inserted). [\end{figure}]
./test2.tex:24: LaTeX Error: Float(s) lost. [\end{document}]

非常感谢您的帮助。

答案1

我为将来可能遇到同样问题的其他读者添加了这个答案。

问题的原因在于重新定义了 TeX 的一个基本原始命令,正如 egreg 所评论的那样。这是由 完成的head to column names。此选项将标题内容映射到宏名称。如果宏存在,则它会被重新定义。在这里,\count宏被重新定义。

如果标题的关键名称无法在 CSV 文件中更改,则不应使用head to column names。可以执行以下操作:

1.默认名称:

\csvcoli可以通过默认宏名称、等来寻址列\csvcolii

\documentclass{article}
\usepackage{filecontents}
\usepackage{csvsimple}

\begin{filecontents*}{myCSV.csv}
count,columnii,columniii
alpha,beta,gamma
blue,red,green
\end{filecontents*}

\begin{document}

\begin{table}
\centering
\begin{tabular}{|c|c|c|} \hline col1 & col2 & col3 \\ \hline
\csvreader[late after line=\\\hline]{myCSV.csv}{}{\csvcoli & \csvcolii & \csvcoliii}
\end{tabular}
\caption{myCaption}
\label{tbl:top40}
\end{table}

\begin{figure}
% any figure here
\end{figure}

\end{document}

2.定义自己的宏名(A):

可以定义自己的宏名称,例如count=\mycount通过以下方式处理计数列\mycount

\documentclass{article}
\usepackage{filecontents}
\usepackage{csvsimple}

\begin{filecontents*}{myCSV.csv}
count,columnii,columniii
alpha,beta,gamma
blue,red,green
\end{filecontents*}

\begin{document}

\begin{table}
\centering
\begin{tabular}{|c|c|c|} \hline col1 & col2 & col3 \\ \hline
\csvreader[late after line=\\\hline]{myCSV.csv}{
    count=\mycount,columnii=\mytwo,columniii=\mythree}
  {\mycount & \mytwo & \mythree}
\end{tabular}
\caption{myCaption}
\label{tbl:top40}
\end{table}

\begin{figure}
% any figure here
\end{figure}

\end{document}

3.定义自己的宏名(B):

可以定义自己的宏名称,例如1=\mycount通过以下方式处理第一列\mycount

\documentclass{article}
\usepackage{filecontents}
\usepackage{csvsimple}

\begin{filecontents*}{myCSV.csv}
count,columnii,columniii
alpha,beta,gamma
blue,red,green
\end{filecontents*}

\begin{document}

\begin{table}
\centering
\begin{tabular}{|c|c|c|} \hline col1 & col2 & col3 \\ \hline
\csvreader[late after line=\\\hline]{myCSV.csv}{
    1=\mycount,2=\mytwo,3=\mythree}
  {\mycount & \mytwo & \mythree}
\end{tabular}
\caption{myCaption}
\label{tbl:top40}
\end{table}

\begin{figure}
% any figure here
\end{figure}

\end{document}

相关内容