我必须显示来自多个 CSV 文件的数据,这些数据包含按列分组的平均值和不确定值对,第一列包含平均值,第二列包含不确定值,依此类推。
CSV 文件中的所有条目都有六位小数,但为了在表格中显示,数据需要截断为具有 1、2 或 3 位小数的对。我尝试实现的格式显示在表 1 中。
原则上,我可以使用 readarray 包创建我需要的字符串,但我缺少一种将字符串自动放入表中的方法。我还尝试截断并将列加载到禁忌表的单独列中,但结果缺少加减号,间距也不理想(表 2)。
我想我所缺少的是某种查找循环,用于将新对存储在新矩阵中,然后从新矩阵中将数据读取到表中?
我的 MWE:
\documentclass[margin=1cm,preview]{standalone}
\usepackage[textwidth=11cm]{geometry}
\usepackage{tabu}
\usepackage{booktabs}
\usepackage[justification=centering]{caption}
\usepackage{siunitx}
\usepackage{csvsimple}
\usepackage{readarray}
\readarraysepchar{,}
\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 :
\readdef{data.csv}\data
\readarray\data\names[-,\ncols]
\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
\csvreader[head to column names, late after line=\\]
{data.csv}{}{ \N & \A & \B & \C & \D & \E & \F}
\bottomrule
\end{tabu}
\end{table}
\end{document}
任何帮助都将不胜感激。如果我在回答这个问题时忽略了某个问题,请指出来。
答案1
您的尝试变得复杂,因为siunitx
不会对带有不确定性成分的数字进行舍入(请参阅https://tex.stackexchange.com/a/127964/105447,虽然很旧,但似乎仍然有效)。
你可以通过单独构建每个列来作弊siunitx
,如下所示。但我必须说我不太喜欢到处走,siunitx
因为通常情况下,它会让你的文档规范化,以正确的方式获取信息。
\documentclass[margin=1cm,preview]{standalone}
\usepackage[textwidth=11cm]{geometry}
\usepackage{tabu}
\usepackage{booktabs}
\usepackage[justification=centering]{caption}
\usepackage{siunitx}
\usepackage{csvsimple}
\usepackage{readarray}
\readarraysepchar{,}
\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 :
\readdef{data.csv}\data
\readarray\data\names[-,\ncols]
\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
\csvreader[head to column names, late after line=\\]
{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
\csvreader[head to column names, late after line=\\]
{data.csv}{}{\N & \A & \B & \C & \D & \E & \F}
\bottomrule
\end{tabular}
\end{table}
\end{document}