# csvreader with tabular (to generate entire table) with caption

I have the following MWE that works:

\begin{filecontents*}{\jobname.csv}
Project, Size
A_b,-0.1237466
b,0.04632737
\end{filecontents*}

\documentclass{article}
\usepackage{csvsimple}

\begin{document}

respect underscore=true,
table head=\hline Project & Size \\\hline\hline,
late after line=\\\hline]%
{\jobname.csv}{}%
{\csvcoli & \csvcolii}%

\end{document}


However, I'm unable to put a \caption on the table. I have tried

\begin{filecontents*}{\jobname.csv}
Project, Size
A_b,-0.1237466
b,0.04632737
\end{filecontents*}

\documentclass{article}
\usepackage{csvsimple}

\begin{document}

respect underscore=true,
\hline Project & Size \\\hline\hline,
late after line=\\\hline]%
{\jobname.csv}{}%
{\csvcoli & \csvcolii}%

\end{document}


but I get all kinds of errors I can't debug easily. Where does one put a \caption{} for a table generated this way?

Note: I don't want to use csvreader inside a \begin{table}... because the respect underscore=true doesn't seem to work properly when I do that, and I have underscores in my data.

• Can you please add a complete copyable document instead of code snippets? – user188485 May 8 at 15:38
• @MisoSoup OK - done – Fuhrmanator May 8 at 15:48

Sorry but respect underscore=true works perfectly within \begin{table} ... \end{table}.

However, there is also a solution using longtable, here them both:

\begin{filecontents*}{\jobname.csv}
Project, Size
A_b,-0.1237466
b,0.04632737
\end{filecontents*}

\documentclass{article}
\usepackage{longtable}
\usepackage{csvsimple}
\usepackage{float}
\usepackage{caption}

\begin{document}
\listoftables

\begin{table}[htb]\centering
\caption{Sample table}\label{tab:test}
tabular=l|r,
respect underscore=true,
table head=\hline Project & Size \\\hline\hline,
late after line=\\\hline
]%
{\jobname.csv}{}%
{\csvcoli & \csvcolii}%
\end{table}

As you can see in table~\ref{tab:test}, \texttt{respect underscore=true} works perfectly within \texttt{\textbackslash begin\{table\}\dots\textbackslash end\{table\}}

longtable=l|r,
respect underscore=true,
\hline\endfoot,
late after line=\\
]{\jobname.csv}{}{\csvcoli & \csvcolii}

\end{document}


• I was used to using longtable with no environment, but should have realized tabular only does the tabular part. This is the best solution in my case. – Fuhrmanator May 10 at 5:31
• @Fuhrmanator Thank you for accepting my answer! – CarLaTeX May 10 at 5:35

For example can you use environment center and add inside it \captionof (needs to load package caption). You need an environment to be able to use \captionof and in your case you can use environment center to get the table centered ...

See the following code

\begin{filecontents*}{\jobname.csv}
Project, Size
A_b,-0.1237466
b,0.04632737
\end{filecontents*}

\documentclass{article}

\usepackage{csvsimple}
\usepackage{caption} % <================================= for \captionof

\begin{document}

\listoftables

\begin{center} % <======================================================
\captionof{table}{Test}\label{tab:test} % <=============================
tabular=l|r,
respect underscore=true,
table head=\hline Project & Size \\\hline\hline,
late after line=\\\hline
]%
{\jobname.csv}{}%
{\csvcoli & \csvcolii}%
\end{center} % <========================================================

As you can see in table~\ref{tab:test} \dots

\end{document}


and its result if you compile it with pdflatex:

As you can see, the _ is not displayed correct in the table. Now add the line

\usepackage[T1]{fontenc} % <======================== needed for pdflatex


to the preamble and you get well printed _ in the table. You need [T1]{fontenc} for correct printed document with pdflatex. Then you also get the _ printed with environment table.

\begin{filecontents*}{\jobname.csv}
Project, Size
A_b,-0.1237466
b,0.04632737
\end{filecontents*}

\documentclass{article}

\usepackage[T1]{fontenc} % <======================== needed for pdflatex
\usepackage{csvsimple}
\usepackage{caption} % <================================= for \captionof

\begin{document}

\listoftables

\begin{center} % <======================================================
\captionof{table}{Test}\label{tab:test} % <=============================
tabular=l|r,
respect underscore=true,
table head=\hline Project & Size \\\hline\hline,
late after line=\\\hline
]%
{\jobname.csv}{}%
{\csvcoli & \csvcolii}%
\end{center} % <========================================================

As you can see in table~\ref{tab:test} \dots

\begin{table}[hb] % <===================================================
\caption{Test1}\label{tab:test1} % <====================================
\centering % <==========================================================
tabular=l|r,
respect underscore=true,
table head=\hline Project & Size \\\hline\hline,
late after line=\\\hline
]%
{\jobname.csv}{}%
{\csvcoli & \csvcolii}%
\end{table} % <========================================================

As you can see in table~\ref{tab:test1} \dots

\end{document}


and its result:

If you compile with lualatex or xelatex you do not need to call package fontenc. So comment the line \usepackage[T1]{fontenc} in the mwe above and compile with lualatex then you get the similar result:

workaround with \captionof from the caption package:

\begin{filecontents*}{\jobname.csv}
Project, Size
A_b,-0.1237466
b,0.04632737
\end{filecontents*}

\documentclass{article}
\usepackage{csvsimple}
\usepackage{caption}

\begin{document}

\begin{minipage}{\textwidth}
\captionof{table}{caption text}