# csvreader and respect all (special characters in the csv file)

I'm trying to use csvsimple to read in a file with elements that contain special characters (underscores in this example) and create a table that only has a some of the columns. I can read in the file (test.csv below) and produce a table of its contents using auto tabular and the 'respect all' option (This works). However when I try do create the table with specific column names, the 'respect all' option doesn't seem to work. Bot examples below work if I remove the underscores from the csv file. Do I need to place the contents of \a into some type of verbatim environment?

\documentclass[]{article}
\usepackage{csvsimple}
\usepackage{booktabs}
\usepackage[T1]{fontenc}
\begin{document}

% This works
respect all,%
autotabular%
]{test.csv}{}{\csvlinetotablerow}%

% This does not
\begin{tabular}{|l|c|}\hline%
\bfseries Col A     & \bfseries Col B
respect all,%
]{test.csv}{}%
{\\\a & \b}%
\\\hline
\end{tabular}
\end{document}


test.csv:

a,b,c,d
b_1,2,3,4
b_5,6,7,8
b_9,10,11,12

• I think both the answer from Peter using data tool and the answer from Thomas using csvreader will solve my problem. I appreciate the help and I learned a bit as well. – John Harrold Sep 28 '15 at 15:55

\documentclass[]{article}
% \usepackage{csvsimple} % remove this for second solution
\usepackage{datatool} % add this for second solution
\usepackage{booktabs}
\usepackage[T1]{fontenc}
% \usepackage{underscore} %<- This solves your problem % remove this for second solution
\begin{document}
% This works
% respect all,%
% autotabular%
% ]{test.csv}{}{\csvlinetotablerow}%
\section{datatool simple}

\begin{table}[htbp]
\centering
\begin{tabular}{|c|c|c|c|}\hline
\DTLdisplaydb{t2g}
\\ \hline
\end{tabular}
\end{table}

% \DTLdisplaylongdb{iris}

\section{datatool not simple}

% This does not

\begin{table}[htbp]
\centering
\begin{tabular}{|c|c|c|c|}\hline
\DTLforeach{t2g}{\ca=a, \cb=b, \cc=c,\cd=d}%
{%
\DTLiffirstrow{}%
{%
\\ \hline
}
\ca&%
\cb&%
\cc&%
\cd%
}%
\\ \hline
\end{tabular}
\end{table}

\end{document}


• I think that works. One last question. How would I filter out the last line? I had it down for the csvsimple package, but I'm not sure how I would do it here. Thanks for your help. I'm just about there ;) – John Harrold Sep 26 '15 at 4:51

The following should work:

\documentclass[]{article}
\usepackage{csvsimple}
\usepackage{booktabs}
\usepackage[T1]{fontenc}
\begin{document}

% This works
respect all,%
autotabular%
]{test.csv}{}{\csvlinetotablerow}%

respect all,%
tabular=|l|c|,
table head=\hline\bfseries Col A  & \bfseries Col B\\,
late after last line=\\\hline,
]{test.csv}{}%
{\a & \b}%

\end{document}


Your attempt did not work, because the respect ... options change the catcode for underscore and so on at the \csvreader spot in your code, but after the next & these catcode settings are reversed back.

The solution is to change the catcode before the table starts. This is done by the code in my answer. Alternatively, the inclusion of the underscore package from Peter Ebelsberger's answer also changes the catcode before the table starts.

\documentclass[]{article}
\usepackage{csvsimple}
\usepackage{booktabs}
\usepackage[T1]{fontenc}
\usepackage{underscore} %<- This solves your problem
\begin{document}

% This works
respect all,%
autotabular%
]{test.csv}{}{\csvlinetotablerow}%

% This does not
\begin{tabular}{|l|c|}\hline%
\bfseries Col A     & \bfseries Col B