5

I'm trying to import data from a csv file and using the S column type from siunitx to format the numbers with this code:

\documentclass{article}
\usepackage{siunitx}
\usepackage{csvsimple}

\begin{filecontents*}{input_data.csv}
month, dogs, cats
January, 12.50,12.3e5
February, 3.32, 8.7e3
\end{filecontents*}

\begin{document}
\section{c}
\csvloop{
file=input_data.csv,
tabular={SS},
command=\csvcolii & \csvcoliii}
\end{document}

That gives me the following errors:

You can't use a prefix with `\hskip'. command=\csvcolii & \csvcoliii}
Missing number, treated as zero. command=\csvcolii & \csvcoliii}
Illegal unit of measure (pt inserted). command=\csvcolii & \csvcoliii}
Extra }, or forgotten \endgroup. command=\csvcolii & \csvcoliii}
Missing } inserted. command=\csvcolii & \csvcoliii}

If I change the first column to e.g. c it typesets.

I have found a workarund, by making an invisible column, but that feels quite ugly and I don't know if it have any side effects that I'm not aware of.

\documentclass{article}
\usepackage{siunitx}
\usepackage{array}
\usepackage{csvsimple}
\newcolumntype{H}{>{\setbox0=\hbox\bgroup}c<{\egroup}@{}}

\begin{filecontents*}{input_data.csv}
month, dogs, cats
January, 12.50,12.3e5
February, 3.32, 8.7e3
\end{filecontents*}

\begin{document}
\csvloop{
file=input_data.csv,
tabular={HSS},
command={}&\csvcolii & \csvcoliii}
\end{document}

So the question is if there is a more clean way to do this.

7

It seems that having S as the first column type is not appreciated by cvssimple. But there are simpler ways for adding a dummy column and circumvent the issue:

\documentclass{article}
\usepackage{siunitx}
\usepackage{csvsimple}

\begin{filecontents*}{\jobname.csv}
month, dogs, cats
January, 12.50,12.3e5
February, 3.32, 8.7e3
\end{filecontents*}

\begin{document}

X\csvloop{
file=\jobname.csv,
tabular={c@{}SS},
command=\empty & \csvcolii & \csvcoliii}X

\medskip

X\csvreader[
  tabular={c@{}SS},
]{\jobname.csv}{}{& \csvcolii & \csvcoliii}X

\medskip

X\begin{tabular}{SS}
12.50&12.3e5\\
3.32& 8.7e3
\end{tabular}X

\end{document}

The third tabular is “hand made”, just for comparison.

enter image description here

  • (+1) for that workaround. I have not stumbled upon this problem yet. It seems that the processing of an S column by siuntix is disturbed by the actions csvsimple does before each line. I would not know how to avoid this automatically. – Thomas F. Sturm Nov 9 '15 at 7:58
  • @ThomasF.Sturm Annoying, but not really difficult to work around. I didn't look at the internals; maybe you can add a caveat to the manual, if you're not able to solve the issue. – egreg Nov 9 '15 at 8:37
2

Here is another workaround. Instead of adding a dummy column, you could change the column type of the first column to l, c or r. The siunitx package provides a macro \tablenum which allows to format the numbers directly.

I use egreg's method to display the code (the third table is for comparison):

\documentclass{article}
\usepackage{siunitx}
\usepackage{csvsimple}

\begin{filecontents*}{\jobname.csv}
month, dogs, cats
January, 12.50,12.3e5
February, 3.32, 8.7e3
\end{filecontents*}

\begin{document}

X\csvloop{
file=input_data.csv,
tabular={cS},
command=\tablenum{\csvcolii} & \csvcoliii}X

\medskip

X\csvloop{
file=input_data.csv,
tabular={cc},
command=\tablenum{\csvcolii} & \tablenum{\csvcoliii}}X

\medskip

X\begin{tabular}{SS}
12.50&12.3e5\\
3.32& 8.7e3
\end{tabular}X

\end{document}

enter image description here

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.