4

The package csvsimple is a nice tool to import large tables that were generated externally. However, it fails to interpret files with symbols or math in the header. It does neither work with tabular nor longtable or any other csvsimple variant. Why is that so and how to get it solved?

The question from Math mode for csvsimple Import "solves" it by putting "no head" as an option, but then, well, there is no head.

file.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}

Error:

! Missing \endcsname inserted.
<to be read again> 
                   \delta 
l.9 ...t.csv}{}{\csvcoli & \csvcolii & \csvcoliii}
5

If you do not set no head, the entries of the header line are used as macro names for the columns.

If they contain spaces or other special characters, they cannot be used as feasible LaTeX macro names, that's why you get the error.

However, you can set no head (or the equivalent head=false) and use the option late after first line to treat the first line as if it was a header.

Edit: with some filtering, you can manage also a 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}

enter image description here

enter image description here

  • Marked as a correct solution, although it is not directly solving the question. Because now the table is designed like a normal table, but semantically it has no head. I.e., the head is not formatted as table heads, and the head does not reappear on the next page if the table goes through a pagebreak. Still, it looks like there is just no other way to solve it. – Martin Apr 29 at 8:46
  • @Martin With some filtering, you can manage also a long table, see my edit. – CarLaTeX Apr 29 at 19:12

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.