2

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}

    \csvreader[tabular=l|r,
    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}

    \csvreader[tabular=l|r,
    respect underscore=true,
    table head=\caption{Sample table} 
    \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
1

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}
\csvreader[%
  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\}} 

\csvreader[
    longtable=l|r,
    respect underscore=true,
    table head=\caption{Sample table with \texttt{longtable}}\label{tab:test2}\\\hline Project & Size \\\hline\hline\endhead
        \hline\endfoot,
    late after line=\\
]{\jobname.csv}{}{\csvcoli & \csvcolii}

\end{document}

enter image description here

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

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} % <=============================
\csvreader[%
  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:

resulting pdf

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.

Please see the following code

\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} % <=============================
\csvreader[%
  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 % <==========================================================
\csvreader[%
  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:

result with fontenc and pdflatex

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:

result with lualatex

0

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}
\csvreader[tabular=l|r,
respect underscore=true,
table head=\hline Project & Size \\\hline\hline,
late after line=\\\hline]%
{\jobname.csv}{}%
{\csvcoli & \csvcolii}%
\end{minipage}
\end{document}
  • Why the minipage? Won't that affect floats? – Fuhrmanator May 8 at 20:01

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.