3

I tried to adapt examples from 5.4 Tables with Number Formatting from csvsimple package, but no success.

Each line in my csv file has 20 fields, all of them has format like x.xxxxxx. Here is the first line:

0.500000,0.614231,0.567667,0.580000,0.787222,0.510333,0.797333,0.732000,0.851228,0.804333,0.764000,0.730667,0.879667,0.778667,0.858333,0.726333,0.884000,0.772667,0.959545,0.919667

I'd like to create a tabular to print the data but truncated, I mean, print only 3 decimal places like x.xxx.

I know that \csvloop{} has the key tabular={} to specify the format (like tabular={lSS[table-format=2.2e1]} from the example cited above).

  1. Since my tabular should have 20 columns, is it possible to format all automatically?

  2. What should be the format? S[table-format=1.3]?

MWE

The code below produces the error You can't use a prefix with \hskip.}

\documentclass{report}
\usepackage{filecontents}
\usepackage{csvsimple}
\usepackage{siunitx}
\usepackage{array}
\begin{document}
\begin{filecontents*}{tab2.csv}
0.500000,0.614231,0.567667,0.580000,0.787222,0.510333,0.797333,0.732000,0.851228,0.804333,0.764000,0.730667,0.879667,0.778667,0.858333,0.726333,0.884000,0.772667,0.959545,0.919667
0.000000,0.500000,0.867308,0.891667,0.763889,0.694872,0.785256,0.729647,0.844298,0.797917,0.709455,0.675641,0.876282,0.741667,0.851442,0.710256,0.880128,0.694872,0.949519,0.912500
\end{filecontents*}
%
\csvloop{
file=tab2.csv,
tabular=*{20}{S[table-format=1.3,round-mode=places,round-precision=3]},
}
\end{document}
  • 1
    Would rounded instead of truncated also work? Then you could try something like *{20}{S[table-format=1.3,round-mode=places,round-precision=3]} – user36296 Feb 14 '18 at 13:30
  • 2
    if this does not solve your problem, could you please add a MWE? – user36296 Feb 14 '18 at 13:34
  • @samcarter, done! – Sigur Feb 14 '18 at 14:05
  • Maybe you could have look here: latex-tutorial.com/tutorials/pgfplotstable There's a explanation for using pgfsplot table for reading the csv file – TimK Mar 16 at 19:45
4
\documentclass{article}

\usepackage{csvsimple}
\usepackage{siunitx}

\usepackage{filecontents}
\begin{filecontents*}{BC14.csv}
0.500000,0.614231,0.567667,0.580000,0.787222,0.510333,0.797333,0.732000,0.851228,0.804333,0.764000,0.730667,0.879667,0.778667,0.858333,0.726333,0.884000,0.772667,0.959545,0.919667
0.000000,0.500000,0.867308,0.891667,0.763889,0.694872,0.785256,0.729647,0.844298,0.797917,0.709455,0.675641,0.876282,0.741667,0.851442,0.710256,0.880128,0.694872,0.949519,0.912500
\end{filecontents*}

\setlength{\tabcolsep}{1pt}

\begin{document}

{
\footnotesize
\csvreader[%
    no head,
  before reading=\sisetup{table-format=1.3,round-mode=places,round-precision=3},
  tabular={c@{} *{20}{S}},
]{BC14.csv}{}{%
& \csvcoli & \csvcolii & \csvcoliii & \csvcoliv & \csvcolv & \csvcolvi  & \csvcolvii & \csvcolviii & \csvcolix & \csvcolx & \csvcolxi & \csvcolxii & \csvcolxiii & \csvcolxiv & \csvcolxv & \csvcolxvi & \csvcolxvii & \csvcolxviii & \csvcolxix & \csvcolxx
} 
}

\end{document}

enter image description here

  • 1
    Now it works. Unfortunately not too automatically, since we have to type all those col commands with & separator. Thanks for helping. – Sigur Feb 14 '18 at 14:53
  • Is it possible to change cells font size? – Sigur Feb 14 '18 at 14:56
  • @Sigur You can change the font size before the table? – user36296 Feb 14 '18 at 14:57
  • If I use \small in table environment, it works. It is just a curiosity how to change font size inside the cells, for example. Or maybe change col space to avoid overfull. – Sigur Feb 14 '18 at 15:00
  • 1
    @Sigur For col space, see tex.stackexchange.com/a/16521/36296 But even with 0 colsep and \footnotesize the table will not fit on a portrait page. – user36296 Feb 14 '18 at 15:03

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.