1

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
  • 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
2
\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? ;)

  • 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
3

The following should work:

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

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


\csvreader[%
  respect all,%
  head to column names,
  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.

1
\documentclass[]{article}
\usepackage{csvsimple}
\usepackage{booktabs}
\usepackage[T1]{fontenc}
\usepackage{underscore} %<- This solves your problem
\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}
  • This seems like a simple solution but adding the underscore package seems to have implications with my main document. I have many files referenced that have underscores in their file names. And this seems to break it. For example. If you rename test.csv test_file.csv, and change the references accordingly I get an error. However, this solution does seem to work when I just use test.csv. – John Harrold Sep 25 '15 at 21:58
  • This filename is very likely to produce errors, I suggest to avoid this filenames. I will try to provide a solution without this package... – Peter Ebelsberger Sep 25 '15 at 22:00

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.