1

I'm trying to use csvsimple to make a table with a tab-delimited input file.

Here is an tab-separated file generic.tsv to put into a table (these are tabs in my file, not spaces, although StackExchange seems to convert them):

r1c1    r1c2    r1c3
r2c1    r2c2    r2c3
r3c1    r3c2    r3c3

Here is an example tex file to read it in:

\documentclass{article}

\usepackage{csvsimple}

\begin{document}

\begin{tabular}{|l|l|l|}
\hline
Col 1 & Col 2 & Col 3 \\
\hline
\csvreader[no head,
    late after line=\\\hline,
    separator=tab
    ]{generic.tsv}{}{\csvcoli & \csvcolii & \csvcoliii}

\end{tabular}

\end{document}

Running this, I get the following:

Output with tab-delimited file

This is not what I was expecting. If I change the input file to replace the tabs with commas (and get rid of separator=tab in the tex file), then it works as expected:

Output with a comma-delimited file

What can I do to get this to work with a tab-delimited file?

1

This is basically the same issue as csvsimple respect dollar not working, but in that question the problem was with the dollar sign $ and in this question the problem is with the tab character. The issue seems to be that setting separator=tab inside of a tabular is reset after the first line because the separator setting is performed in a group. Using the \csvreader to set up the tabular itself solves this issue, because now the separator is set outside of the tabular and therefore the setting remains valid.

If you want to use the original approach then you can set the category code of the tab character manually to 12 (normal character) and reset it back to 10 (space character) after the tabular. This however has a side effect that the bottom of the table is not rendered correctly for some reason. In the MWE below I removed the line ending on the last line, which unfortunately means that you cannot print a \hline there.

\documentclass{article}

\usepackage{csvsimple}

\begin{document}

\csvreader[separator=tab,
    tabular={|l|l|l|},
    table head={\hline Col 1 & Col 2 & Col 3\\\hline},
    late after line=\\\hline,
    no head
    ]{generic.tsv}{}%
    {\csvcoli & \csvcolii & \csvcoliii}

Tabular in \texttt{csvreader}
\vspace{1cm}

\catcode`\^^I=12
\begin{tabular}{|l|l|l|}
\hline
Col 1 & Col 2 & Col 3 \\
\hline
\csvreader[no head,
    late after line=\\\hline,
    late after last line=\relax,
    separator=tab]{generic.tsv}{}{\csvcoli & \csvcolii & \csvcoliii}
\end{tabular}
\catcode`\^^I=10

Manual catcode change for tab character

with missing bottom line
\end{document}

enter image description here

1
  • Thank you, interesting regarding the tabular-inside. Useful to keep in mind for the future. – Tyler Mar 20 '20 at 17:33

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.