3

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.

2
  • 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
    Commented Jan 23, 2018 at 0:32
  • 1
    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. Commented Jan 23, 2018 at 1:04

1 Answer 1

3

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

1
  • 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
    Commented Jan 23, 2018 at 20:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .