# Automated truncating and compilation of mean and uncertainty pairs for display in table from csv file

I have to display data from a number of CSV files containing pairs of mean and uncertainty values grouped in columns, the first column containing the mean values, the second the uncertainty values and so on.

All entries in the CSV file have six decimal places, but for the display in the table, the data needs to be truncated to pairs with 1, 2 or 3 decimal places. The format I am trying to achieve is displayed in Table 1.

I am able to create the string I need in principle, using readarray package, but I am missing an automated way to put the strings into the table. I also tried to truncate and load the columns into spearate columns of a tabu table but then I am missing the plusminus sign and the spacing is not ideal (Table 2).

I guess what I am missing is some kind of lookup loop to store the new pairs in a new matrix and then read the data to the table from the new matrix ?

My MWE:

\documentclass[margin=1cm,preview]{standalone}
\usepackage[textwidth=11cm]{geometry}
\usepackage{tabu}
\usepackage{booktabs}
\usepackage[justification=centering]{caption}
\usepackage{siunitx}
\usepackage{csvsimple}

\begin{filecontents*}{data.csv}
N,A,B,C,D,E,F
1,3.035399,0.103098,1.649978,0.049118,1.649978,0.049118
2,0.497626,0.064312,1.391432,0.049175,1.391432,0.049175
3,0.757157,0.112648,0.720012,0.046933,0.720012,0.046933
4,0.529376,0.123559,0.940044,0.048333,0.940044,0.048333
\end{filecontents*}

\begin{document}

Truncated string I am trying to put together from the data csv :

\num[round-mode=places,round-precision=2]{\names[2,2]}$\:\pm\:$\num[round-mode=places,round-precision=2]{\names[2,3]}

\sisetup{separate-uncertainty}
\begin{table}
\centering
\caption{Format I am trying to achieve, using the data from the csv file}
\medskip
\begin{tabular}{
S
S[table-format = 1.1(1)]
S[table-format = 1.2(2)]
S[table-format = 1.3(2)]
}
\toprule
{N} & {one} & {two} & {three} \\
\midrule
{1} & 3.0\pm0.1 & 1.65\pm0.02 & 1.650\pm0.049 \\
{2} & 0.5\pm0.1 & 1.39\pm0.05 & 1.391\pm0.049 \\
\bottomrule
\end{tabular}
\end{table}
\newcolumntype{Z}{S[tight-spacing=true,round-mode=places,round-precision=1]}
\newcolumntype{Q}{S[tight-spacing=true,round-mode=places,round-precision=2]}
\newcolumntype{Y}{S[tight-spacing=true,round-mode=places,round-precision=3]}

\begin{table}
\centering
\caption{Loading from csv works, truncating decimal places works, but $\pm$ is missing, spacing is getting to big}
\medskip
\begin{tabu} to .8\textwidth {XZZQQYY}
\toprule
{N} & \multicolumn{2}{c}{one} & \multicolumn{2}{c}{two} & \multicolumn{2}{c}{three} \\
\midrule
{data.csv}{}{ \N & \A & \B & \C & \D & \E & \F}
\bottomrule
\end{tabu}
\end{table}

\end{document}


Any help would be very much appreciated. If i have overlooked a question answering this, please point it out to me.

• welcome to tex.se! in your mwe are some errors. please test it again and try to eliminate them (... Invalid token '\names ' in numerical input. ...) – Zarko Feb 5 '19 at 10:59
• I am sorry, but I have tested the MWE again (Texmaker 5.0.2 and Overleaf) and I do not get this error but only a warning concerning the writing of the "data.csv" file – kurechan Feb 5 '19 at 12:58
• Welcome to TeX.SX! I can get some things to work, not rounding with uncertainty though. See tex.stackexchange.com/a/127964/105447, which is old, but probably up to date, considering siunitx's documentation states "No rounding will take place if the number contains an uncertainty component." – gusbrs Feb 5 '19 at 13:32

Your attempt gets complicated, because siunitx does not round numbers with uncertainty components (see https://tex.stackexchange.com/a/127964/105447, which is old, but seems to be still valid).

You can cheat siunitx by building each column individually, as shown below. But I must say I'm not very fond of going around siunitx because, usually, it disciplines your document to get things in a proper way.

\documentclass[margin=1cm,preview]{standalone}
\usepackage[textwidth=11cm]{geometry}
\usepackage{tabu}
\usepackage{booktabs}
\usepackage[justification=centering]{caption}
\usepackage{siunitx}
\usepackage{csvsimple}

\begin{filecontents*}{data.csv}
N,A,B,C,D,E,F
1,3.035399,0.103098,1.649978,0.049118,1.649978,0.049118
2,0.497626,0.064312,1.391432,0.049175,1.391432,0.049175
3,0.757157,0.112648,0.720012,0.046933,0.720012,0.046933
4,0.529376,0.123559,0.940044,0.048333,0.940044,0.048333
\end{filecontents*}

\begin{document}

Truncated string I am trying to put together from the data csv :

\num[round-mode=places,round-precision=2]{\names[2,2]}$\:\pm\:$\num[round-mode=places,round-precision=2]{\names[2,3]}

\sisetup{separate-uncertainty}
\begin{table}
\centering
\caption{Format I am trying to achieve, using the data from the csv file}
\medskip
\begin{tabular}{
S
S[table-format = 1.1(1)]
S[table-format = 1.2(2)]
S[table-format = 1.3(2)]
}
\toprule
{N} & {one} & {two} & {three} \\
\midrule
{1} & 3.0\pm0.1 & 1.65\pm0.02 & 1.650\pm0.049 \\
{2} & 0.5\pm0.1 & 1.39\pm0.05 & 1.391\pm0.049 \\
\bottomrule
\end{tabular}
\end{table}
\newcolumntype{Z}{S[tight-spacing=true,round-mode=places,round-precision=2,table-format = 1.6(7)]}
\newcolumntype{Q}{S[tight-spacing=true,round-mode=places,round-precision=2,table-format = 1.6(7)]}
\newcolumntype{Y}{S[tight-spacing=true,round-mode=places,round-precision=3,table-format = 1.6(7)]}

\begin{table}
\centering
\caption{Loading from csv works, $\pm$ works, but rounding doesn’t}
\medskip
\begin{tabu} to .8\textwidth {XZQY}
\toprule
{N} & {one} & {two} & {three} \\
\midrule
{data.csv}{}{\N & \A\pm\B & \C\pm\D & \E\pm\F}
\bottomrule
\end{tabu}
\end{table}

\newcolumntype{T}{S[tight-spacing=true,round-mode=places,round-precision=1,table-format = 1.1]}
\newcolumntype{R}{S[tight-spacing=true,round-mode=places,round-precision=2,table-format = 1.2]}
\newcolumntype{P}{S[tight-spacing=true,round-mode=places,round-precision=3,table-format = 1.3]}

\begin{table}
\centering
\caption{Loading from csv works, truncating decimal places works, $\pm$ works}
\medskip
\begin{tabular}{XT@{\pm}TR@{\pm}RP@{\pm}P}
\toprule
{N} & \multicolumn{2}{c}{one} & \multicolumn{2}{c}{two} & \multicolumn{2}{c}{three} \\
\midrule