![自动格式化 csv 文件中的表格单元格](https://linux22.com/image/370493/%E8%87%AA%E5%8A%A8%E6%A0%BC%E5%BC%8F%E5%8C%96%20csv%20%E6%96%87%E4%BB%B6%E4%B8%AD%E7%9A%84%E8%A1%A8%E6%A0%BC%E5%8D%95%E5%85%83%E6%A0%BC.png)
我尝试改编以下例子5.4 带数字格式的表格从csvsimple
包中,但没有成功。
我的文件中每一行csv
有 20 个字段,他们全部格式如下x.xxxxxx
。第一行如下:
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
我想创建一个表格来打印数据,但是截断,我的意思是只打印 3 位小数,就像x.xxx
。
我知道有指定格式的\csvloop{}
关键(如上面引用的例子)。tabular={}
tabular={lSS[table-format=2.2e1]}
由于我的表格应该有 20 列,是否可以自动格式化全部?
格式应该是什么
S[table-format=1.3]
??
平均能量损失
下面的代码会产生错误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
\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}