2

I have to import a csv table whose entries contain underscores. A simple example is the following table:

FirstCol, SecondCol
Item_A, Item_B

I am trying to solve my problem with the csvsimple package with the "respect underscore=true" option. Here is a minimal working example:

\documentclass[10pt,a4paper]{article}
\usepackage[utf8x]{inputenc}
\usepackage{longtable}
\usepackage{csvsimple}
\usepackage{array,booktabs}
\usepackage[T1]{fontenc}

\begin{document}

\begin{longtable}{|l|l|}
   \hline
   \csvreader[respect underscore=true, head to column names, 
    late after line=\\\hline]{Test.csv}{}{\FirstCol & \SecondCol}
\end{longtable}

\end{document}

This code produces the following result:

enter image description here

How do I get rid of the obviously wrong column separators just below the table? To be clear, what I would like is this:

enter image description here

Note that the two separators only appear if the "respect underscore=true" option is present. If I remove this option (and delete the underscores from the input file), then all works as expected.

  • This is not a duplicate of this question. The latter question was concerned with the underscore not being rendered properly; the present question is concerned with the unwanted column separators appended just under the table. – Alessandro Jan 23 '18 at 0:32
  • Apparently it doesn't like being inside a table. Try \csvreader[longtable={|l|l|},table head=\hline,respect underscore=true,head to column names,late after line=\\\hline]{test.csv}{}{\FirstCol & \SecondCol} instead. – Henri Menke Jan 23 '18 at 1:04
2

A solution could be pgfplotstable with the verb string type option.

\documentclass[10pt,a4paper]{article}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{longtable}
\usepackage{array,booktabs}
\renewcommand*{\arraystretch}{1.2}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.15}

\usepackage{filecontents}
\begin{filecontents}{Test.csv}
    FirstCol, SecondCol
    Item_A, Item_B
\end{filecontents}

\begin{document}

    \pgfplotstableread[col sep=comma]{Test.csv}{\mytable}

    \pgfplotstabletypeset[
        begin table={\begin{longtable}},
        every head row/.append style={before row={%
                \hline
                \endfirsthead
                \hline
                FirstCol & SecondCol\\
                \hline
                \endhead
            },
        },      
        after row=\hline,       
        end table={\end{longtable}},
        every first column/.style={column type={|l|}},
        every column/.style={column type={l|}},
        verb string type
        ]{\mytable}

\end{document}

enter image description here

  • If at all possible, I would prefer to stay with the csvsimple package. We are using it in all our documents and we have developed macros and commands around it. Also, I do not see why it should not be possible to make it work (unless there is a bug in the package, of course). – Alessandro Jan 23 '18 at 20:17

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.