4

I'm trying to use the package csvsimple to create a table from a csv file. This worked so far, but when I try to add new figures/tables afterwards I get multiple errors. It looks like the \end statement of my table is not properly recognized.

\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}

This gives me the following error messages:

./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}]

Help would be much appreciated.

  • Welcome to TeX.SX! Can you add a small CSV file that produces the error? – egreg Jan 14 '15 at 11:46
  • I just found the error myself. I had a column name called \count (instead of \coli as in my example above). As soon as I changed the \count to something like \countFoo it would work. Is \count a protected keyword? Just curious. I'm not able to find anything related to that keyword. – Martin Jan 14 '15 at 13:09
  • Note that \count is a fundamental primitive command of TeX; using it in the column name redefines it, which is not a good idea! ;-) If you add the small example, the question can still be useful for other readers; you can even self-answer it. – egreg Jan 14 '15 at 13:12
  • Yep, it's too bad latex wouldn't give me an error warning that is related to the changing of a primitive. Thanks for your help anyway! I changed the code in the example above. – Martin Jan 14 '15 at 14:31
4

I add this answer for other readers which may have the same problem in the future.

The cause of the problem is the redefinition of a fundamental primitive command of TeX as egreg commented. This was done by head to column names. This option maps the heading contents to macro names. If a macro is existing, it becomes redefined. Here, the \count macro was redefined.

If the critical name of the heading cannot be changed inside the CSV file, one should not use head to column names. One can do the following:

1. Default names:

The columns can be addressed by default macro names \csvcoli, \csvcolii, etc:

\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. Define own macro names (A):

One can define own macro names like count=\mycount to address the count column by \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. Define own macro names (B):

One can define own macro names like 1=\mycount to address the first column by \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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.