0

I am using csvsimple package to embed csv files as tables into my latex document.

It looks like long cell contents break the rendering:

tokens.csv:

method,corpus,tokens
MI,Quran,"allah, thou, thi, punish, believ, ye, thee, messeng, unbeliev, guid"
MI,OT,"allah, jesu, christ, thi, king, israel, believ, thou, lord, thee"
MI,NT,"jesu, christ, allah, ye, discipl, lord, thing, faith, israel, peter"
CHI²,Quran,"allah, punish, believ, messeng, unbeliev, guid, beli, disbeliev, vers, clear"
CHI²,OT,"allah, jesu, christ, thi, believ, king, israel, lord, thou, world"
CHI²,NT,"jesu, christ, discipl, ye, faith, thing, paul, peter, lord, allah"

main.tex:

\csvautotabular{tokens.csv}

Renders as enter image description here

I'm assuming because the third cell has long contents (other CSV embeds with shorter cell contents are rendering properly in my document)

How can I fix this?

The CSV files come straight out of other programs so I'd rather not have to edit them at all, or at least not make them incompatible with standard CSV format.

1

From the csvsimple manual:

Values are expected to be not quoted or quoted with curly braces {} of TEX groups. Other quotes like double quotes are not supported directly, but can be achieved with external tools, see Section 5.6 on page 41.

Thus, using a set of {} instead of " " to enclose the contents of the third cell should work:

enter image description here

\documentclass{article}
\usepackage{csvsimple}
\usepackage{tabularx}
\begin{filecontents*}{tokens.csv}
method,corpus,tokens
MI,Quran,{allah, thou, thi, punish, believ, ye, thee, messeng, unbeliev, guid}
MI,OT,{allah, jesu, christ, thi, king, israel, believ, thou, lord, thee}
MI,NT,{jesu, christ, allah, ye, discipl, lord, thing, faith, israel, peter}
CHI²,Quran,{allah, punish, believ, messeng, unbeliev, guid, beli, disbeliev, vers, clear}
CHI²,OT,{allah, jesu, christ, thi, believ, king, israel, lord, thou, world}
CHI²,NT,{jesu, christ, discipl, ye, faith, thing, paul, peter, lord, allah}
\end{filecontents*}

\begin{document}
\csvautotabular{tokens.csv}

\bigskip

\csvreader[
  tabular=|l | l | p{7cm}|,
  table head= \hline method & corpus & tokens \\ \hline,
  late after last line=\\\hline,
]{tokens.csv}{}%
{\csvcoli & \csvcolii & \csvcoliii}

\bigskip

\begin{tabularx}{\linewidth}{|l|l|X|}
\hline
method & corpus & tokens \\
\hline
\csvreader[late after line=\\, late after last line =\\\hline]
  {tokens.csv}
  {}
  {\csvcoli & \csvcolii & \csvcoliii}
\end{tabularx}


\end{document}
7
  • To use a fixed width column with automatic line breaking, you could give \csvreader[ tabular=|l | l | p{7cm}|, table head= \hline method & corpus & tokens \\ \hline, late after last line=\\\hline, ]{tokens.csv}{}% {\csvcoli & \csvcolii & \csvcoliii} a try. – leandriis Dec 13 '20 at 15:16
  • I wouldn't say I want fixed width columns, just a max table width being the linewidth, and the columns can sort themselves out within that – theonlygusti Dec 13 '20 at 15:23
  • @theonlygusti: I have just added a third example building upon tabularx in order to make sure the table is exactly as wide as the textwidth. The first and second column are as wide as the corresponding widest entries, while the width of the third column is calculated automatically. – leandriis Dec 13 '20 at 15:24
  • Thanks a lot. Is there a way to make it not break words and hyphenate? Only break on existing word separators? – theonlygusti Dec 13 '20 at 15:28
  • 1
    You could try \begin{tabularx}{\linewidth}{|l|l|>{\raggedright\arraybackslash}X|}. – leandriis Dec 13 '20 at 15: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.