# using csvreader to read from csv giving invalid output

I am trying to output a *.csv file to LaTeX, but I keep on getting invalid output. I am using mactex on a yosemite environment.

code.csv:

code,subcd
CITY,LIMA
CITY,PUEBLOLIBRE
CITY,JESUSMARIA
CITY,TARMA


preamble:

\documentclass[10pt]{article}
\usepackage{graphicx,fancyhdr,import,hyperref}
\usepackage{longtable,csvsimple}
\usepackage[margin=.75in]{geometry}
\usepackage[toc,page]{appendix}


using csvreader:

\begin{document}

\section{System Constants}
The following table contains the constants used throughtout the system.

\begin{tabular}{|c|c|}
\csvreader[head to column names]{code.csv}{}{}
\end{tabular}

\end{document}


my output:

I am not sure what I'm doing wrong. Also, I saw in Importing CSV file as a table in Latex but file too long that one can split a table that is too long into multiple tables with the same header (my table eventually will grow in size and I thought it would be a good idea to account for it). However, after trying the samples in the post above, I seem to get errors.

.tex file:

\section{System Constants}
The following table contains the constants used throughtout the system.

%\csvautotabular{tables/code.csv}
%\begin{tabular}{|c|c|}
%\csvreader[head to column names]{code.csv}{}{}
%\end{tabular}

\csvreader[
longtable=lrrrr,
table head=
\toprule\bfseries Code & \bfseries Sub Code \\
\midrule\endhead
\bottomrule\endfoot,
late after line=\\,
before reading={\catcode\#12},
after reading={\catcode\#12}
]{code.csv}{}{}


Any ideas on how to use csvsimple with longtable?

Any help would be appreciated!

EDIT: after some research on csvsimple and pointed on the right direction by Thomas F. Sturm, I found out that I was using the tool incorrectly. The following is an updated version of the .tex code and also note that I've updated the .csv file to subcd

\csvreader[
longtable=|l|l|,
table head=
\toprule
\bfseries Code & \bfseries Sub Code\\
\midrule
\endhead
\bottomrule,
table foot=\bottomrule
]{code.csv}{1=\code, 2=\subcd}{\code & \subcd}


However, I seem to get a compilation error on the last line {\code & \subcd}

## 2 Answers

\documentclass[10pt]{article}
\usepackage{graphicx,fancyhdr,import,hyperref}
\usepackage{longtable}
\usepackage{datatool}
\usepackage[margin=.75in]{geometry}
\usepackage[toc,page]{appendix}

\begin{document}

\section{System Constants}
The following table contains the constants used throughtout the system.

\DTLsetseparator{,}
\DTLloadrawdb[noheader,keys={code,sub_cd}]{ex}{code.csv} %took more robost loaddb
\begin{longtable}%
{%
|c|c|
}%
A & B \\
\endfirsthead
A & B \\
\endhead
\multicolumn{2}{r}
{
next page
} \\
\endfoot
\multicolumn{2}{r}
{
last page
} \\
\endlastfoot
\DTLforeach{ex}{\code=code, \subcd=sub_cd}%
{%
\DTLiffirstrow{}%
{%
\\%
}%
\protect\code&%
\protect\subcd%
}%
\end{longtable}

\end{document}


Take datatool. :)

• thanks for introducing me to datatool, I found it useful to look at this documentation (Not all of it works but gives you an idea). The only problem that I have with your solution is that if my table grows, it will not divide the table. Is there any way to use DataTool for long .csv files? – m4rk4l May 6 '15 at 18:27
• Yes. You can use longtable in combination. I have added the commands for endfirsthead, endhead, endlastfoot, lendfoot for example, you can remove all between }% and \DTL and you have the same example as previously posted. I really suggest to switch to datatool in combination with longtable. – Peter Ebelsberger May 6 '15 at 20:44
• It works! =) Thank you. One last question. What are the pro and cons of datatool compared to \csvreader. It seems to me both can accomplish the same. – m4rk4l May 6 '15 at 21:20
• As I can see csvreader has improved a lot. But it seems to me looking at the manual, that mathoperations is an advantage of datatool. I am using datatool in combination with longtable exzessiv. I can access single elements if i want and I am still trying to improve my tablereadercode in commands..... For example, I have written a commands which enables me to go through a csv file, make automatic pagebreaks and I can define the fieldtype. Also, datatool can be used to log single steps in your code and export it to csv, which I cannot found in csvreader. Datatool can also write csv...(dis:slow) – Peter Ebelsberger May 6 '15 at 21:30
• marking this as an answer because not only it worked for me, but it also gives you more control over what to display on the table/long table. However, I like the answer given by @ThomasF.Sturn for simple displaying of .csv; unfortunately I couldn't make it work. – m4rk4l May 7 '15 at 13:36

There are two problems to solve. You need the booktabs package for the rules and you have to replace head to column names to avoid problems with the subscript in your heading.

With these alterations, you get the following:

\begin{filecontents*}{code.csv}
code,sub_cd
CITY,LIMA
CITY,PUEBLOLIBRE
CITY,JESUSMARIA
CITY,TARMA
\end{filecontents*}

\documentclass[10pt]{article}
\usepackage{graphicx,fancyhdr,import,hyperref}
\usepackage{longtable,csvsimple,booktabs}
\usepackage[margin=.75in]{geometry}
\usepackage[toc,page]{appendix}

\begin{document}

\section{System Constants}
The following table contains the constants used throughtout the system.

\csvreader[
longtable=ll,
table head=
\toprule\bfseries Code & \bfseries Sub Code \\
\midrule\endhead
\bottomrule\endfoot,
late after line=\\,
before reading={\catcode\#12},
after reading={\catcode\#12}
]{code.csv}{1=\code,2=\subcode}{%
\code & \subcode
}

\end{document}


The output is:

The log file should contain a line like the following to check the version number (1.12 or better):

Package: csvsimple 2014/07/14 version 1.12 LaTeX CSV file processing

• Unfortunately I don't get the same output. I noticed that I have a compilation error when I execute the {command list} of csvreader. I have also tried to use \csvcoli & \csvcolii with no success. Please review my updated question. – m4rk4l May 6 '15 at 20:09
• @m4rk4l With the code.csv from your example, I have no clue what is going wrong. The only thing would be that you use another csv file which contains e.g. texts with _ or similar... – Thomas F. Sturm May 7 '15 at 6:04
• @ThomasF.Sturm Can you explain the differences from your package compaired to datatool in more detail? – Peter Ebelsberger May 7 '15 at 6:39
• It seems that I have trouble when I try to execute \csvreader[<options>]{<file name>}{<assignments>}{<command list>} I've even modified the .csv file not to contain an - or _. – m4rk4l May 7 '15 at 13:31
• @PeterEbelsberger I see csvsimple as a lightweight alternative to datatool. It has considerable less features, but is faster. If one looks for basic operations like reading data into a table, csvsimple is not a bad choice. If more database-like features are needed (and from your comments to your own answer I see that you need them), datatool is should be used. I do not have plans to extend csvsimple in database direction, because datatool provides that. But I use csvsimple myself everyday in applications where speed matters (with external sorting). – Thomas F. Sturm May 7 '15 at 13:37