[Tex/LaTex] csvreader and respect all (special characters in the csv file)

csvcsvsimple

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
\csvreader[%
  respect all,%
  autotabular%
]{test.csv}{}{\csvlinetotablerow}%


% This does not
\begin{tabular}{|l|c|}\hline%
\bfseries Col A     & \bfseries Col B
\csvreader[%
  respect all,%
  head to column names
]{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

Best Answer

\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}
\section{csvreader}
% This works
% \csvreader[%
  % respect all,%
  % autotabular%
% ]{test.csv}{}{\csvlinetotablerow}%
\section{datatool simple}
\DTLloadrawdb[noheader,keys={a,b,c,d},headers={a,b,c,d}]{t2g}{test.csv}


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

What about this solution? ;)