2

I can make a simple table like this:

\documentclass{article}
\usepackage{csvsimple}

\begin{filecontents*}{words.csv}
num, word
1, apple
2, dog
3, pizza
\end{filecontents*}

\begin{document}

\begin{tabular}{cl}
\bfseries Num & \bfseries Text 
\csvreader[head to column names]{words.csv}{}{\\\num & \word}
\\\hline
\end
{tabular}

\end{document}

but when I try to add a line underneath the header like this:

\documentclass{article}
\usepackage{csvsimple}

\begin{filecontents*}{words.csv}
num, word
1, apple
2, dog
3, pizza
\end{filecontents*}

\begin{document}

\begin{tabular}{cl}
\bfseries Num & \bfseries Text 
\hline
\csvreader[head to column names]{words.csv}{}{\\\num & \word}
\\\hline
\end
{tabular}

\end{document}

I get this error:

./csv-simple-error.tex:16: Misplaced \noalign.
\hline ->\noalign 
              {\ifnum 0=`}\fi \hrule \@height \arrayrulewidth \futurelet...
 l.16 \hline

What does the error mean? and how can I add a horizontal line under the header?

3

\hline can only appear immediately after the format declaration or after a line change command \\; since this is not the case for the first \hline in your example code, and error is triggered. Here's one possible solution:

\documentclass{article}
\usepackage{csvsimple}

\begin{filecontents*}{words.csv}
num, word
1, apple
2, dog
3, pizza
\end{filecontents*}

\begin{document}

\begin{tabular}{cl}
\bfseries Num & \bfseries Text  \\
\hline
\csvreader[head to column names]{words.csv}{}{\num & \word\\}
\\[-\normalbaselineskip]\hline
\end
{tabular}

\end{document}

enter image description here

2

You can use late after line:

\begin{filecontents*}{\jobname.csv}
num, word
1, apple
2, dog
3, pizza
\end{filecontents*}

\documentclass{article}
\usepackage{csvsimple}

\begin{document}

\begin{tabular}{cl}
\bfseries Num & \bfseries Text \\ \hline
\csvreader[
  head to column names,
  late after line=\\,
]{\jobname.csv}{}{\num & \word}
\hline
\end{tabular}

\end{document}

enter image description here

The alternative way is to use the tabular option to \csvreader, which implies \\ at the end of rows:

\begin{filecontents*}{\jobname.csv}
num, word
1, apple
2, dog
3, pizza
\end{filecontents*}

\documentclass{article}
\usepackage{csvsimple}

\begin{document}

\csvreader[
  tabular=cl,
  head to column names,
  table head=\bfseries Num & \bfseries Text \\ \hline,
  table foot=\hline,
]{\jobname.csv}{}{\num & \word}

\end{document}
3
  • the documentation says "Note that table options like /csv/tabular→ P. 16 set this key to \\ automatically" ctan.sharelatex.com/tex-archive/macros/latex/contrib/csvsimple/… -- confused as to why we need to set it explicitly in this case. Oct 24 '15 at 17:41
  • 1
    @Ultrasaurus You're not using the tabular option to \csvreader. I added the alternative input style.
    – egreg
    Oct 24 '15 at 17:55
  • if tabular=cl isn't the tabular option, what is? Oct 24 '15 at 21:23

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.