TransWikia.com

csvsimple inside tabular environment, but csv data includes LaTeX arrays/tables/aligned etc

TeX - LaTeX Asked by TheMathGuyd on September 5, 2021

I am trying to import a csv using csvsimple, and I have already overcome the challenge of the csv delimiter (by adding curly braces around every field), but I am now stuck with a admittedly weird problem. I would explain in more detail, but a simplified example shows this issue nicely enough.

I’ve adapted the example information from an answer found here, but separated the csv data to a standalone file.

let jobname.csv be:

11.0; 12.3; $x^2$; seven$
1.2; 33.11; five; six

let our .tex file be:

documentclass{article}
usepackage{booktabs}
usepackage{csvsimple}

begin{document}
begin{tabular}{cccc}
toprule
1 & 2 & 3 & 4 
midrule
csvreader[
  separator=semicolon,
  no head,
  late after line=,
]{jobname.csv}{}{%
  csvcoli & csvcolii & csvcoliii & csvcoliv
}
bottomrule
end{tabular}

bigskip

end{document}

When I run this everything is fine.

The issue happens when I include an array or some similar environment that uses & and .

Change contents of jobname.csv to:

11.0; 12.3; $x^2$; begin{array}{cc}2 & 4  6 & 8end{array}
1.2; 33.11; five; six

When I run this, everything is not fine. From what I can tell, the causes the problem, and then TeX tries to interpret the & as part of the tabular environment.

I understand this is similar to this question, but I fear automating the conversion of my csv data to fit this data would be quite difficult, since my actual project already contains some 300 cells of LaTeX snippets.

One Answer

First you need to add $'s around array environment in jobname.csv file:

11.0; 12.3; $x^2$; $begin{array}{cc}2 & 4  6 & 8end{array}$
1.2; 33.11; five; six

With csvsimple-l3 (2.1.0 2021-07-06) you can get what you want:

documentclass{article}
usepackage{booktabs}
usepackage[l3]{csvsimple}

begin{document}

csvreader[
  tabular=cccc,
  table head=toprule 1 & 2 & 3 & 4  midrule,
  separator=semicolon,
  no head,
  late after line=,
  late after last line=bottomrule,
]{jobname.csv}{}{%
  csvcoli & csvcolii & csvcoliii & expandafterunexpandedexpandafter{csvcoliv}
}

end{document} 

enter image description here

Answered by L.J.R. on September 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP