调整 csv 表格中的小数

调整 csv 表格中的小数

我的问题是我正在从中导入一些数据csv并生成一个表。

表格很好,但我需要有 4 位小数的数字。我用以下代码生成了表格(可能不是最干净的,但它有效):

\documentclass{article}

\usepackage{booktabs}
\usepackage{csvsimple}

\newcommand{\dataa}{\begin{tabular}{c} \\  \\ \\ Var1 \\ \\  \\ \\Var2 \\  \end{tabular}}
\newcommand{\datab}{\begin{tabular}{c}  4\\ 8\\12\\16\\4\\ 8\\12\\16\\  \end{tabular}}
\newcommand{\datac}{\csvreader[separator=comma, no head, tabular={c},filter not strcmp={\boot}{}]{Tab_2.csv}{5=\expa,6=\rec,7=\dri,8=\boot}{\expa}}
\newcommand{\datad}{\csvreader[separator=comma, no head, tabular={c},filter not strcmp={\boot}{}]{Tab_2.csv}{5=\expa,6=\rec,7=\dri,8=\boot}{\rec}}
\newcommand{\datae}{\csvreader[separator=comma, no head, tabular={c},filter not strcmp={\boot}{}]{Tab_2.csv}{5=\expa,6=\rec,7=\dri,8=\boot}{\dri}}
\newcommand{\dataf}{\csvreader[separator=comma, no head, tabular={c},filter not strcmp={\boot}{}]{Tab_2.csv}{5=\expa,6=\rec,7=\dri,8=\boot}{\boot}}

\begin{document}
\begin{table}
\centering
\begin{tabular}{ l c c c c c} 
\hline \hline
& & \multicolumn{2}{c}{Regime} & \multicolumn{2}{c}{Significance} \\
\cmidrule(lr){3-4} \cmidrule(l){5-6} 
Impact & Horizon & State1 & State2 & CI & Bootstrap\\
\hline
\dataa & \datab & \datac & \datad & \datae & \dataf \\
\hline
\end{tabular}
\end{table}

\end{document}

在此处输入图片描述

我正在使用的文件csv具有这种格式(我无法更改它,它已经像这样处理了):

,,,,,,,
,,,,,,,
,,,,,,,
,,,,-0.010782,-0.010116,0.48334,0.1108
,,,,-0.041457,-0.015253,0.1942,0.0297
,,,,-0.075416,-0.026916,0.11368,0.0127
,,,,-0.083706,-0.028817,0.11713,0.0259
,,,,0.0018477,-0.0098031,0.20488,0.4541
,,,,-0.0097454,-0.018515,0.38455,0.2622
,,,,-0.025355,-0.0038709,0.2999,0.2734
,,,,-0.042006,0.014067,0.1024,0.283

我查找了类似的问题,但都没有解决,我也查看了软件包的帮助文件,但csvsimple还是没有成功。如果这个问题已经得到解决,我也很抱歉。

答案1

以下代码利用了siunitx。它基于答案展示了如何建立siunitx和之间的兼容性csvsimple

桌子

\documentclass{article}

\usepackage{booktabs}
\usepackage{csvsimple}
\usepackage[round-mode=places, round-integer-to-decimal, round-precision=4,
    table-format = 1.4, 
    table-number-alignment=center,
    round-integer-to-decimal]{siunitx}
\newcolumntype{N}{c@{}S}

\newcommand{\dataa}{\begin{tabular}{c} \\  \\ \\ Var1 \\ \\  \\ \\Var2 \\  \end{tabular}}
\newcommand{\datab}{\begin{tabular}{c}  4\\ 8\\12\\16\\4\\ 8\\12\\16\\  \end{tabular}}
\newcommand{\datac}{\csvreader[separator=comma, no head, tabular={N},filter not strcmp={\boot}{}]{test.csv}{5=\expa,6=\rec,7=\dri,8=\boot}{& \expa}}
\newcommand{\datad}{\csvreader[separator=comma, no head, tabular={N},filter not strcmp={\boot}{}]{test.csv}{5=\expa,6=\rec,7=\dri,8=\boot}{& \rec}}
\newcommand{\datae}{\csvreader[separator=comma, no head, tabular={N},filter not strcmp={\boot}{}]{test.csv}{5=\expa,6=\rec,7=\dri,8=\boot}{& \dri}}
\newcommand{\dataf}{\csvreader[separator=comma, no head, tabular={N},filter not strcmp={\boot}{}]{test.csv}{5=\expa,6=\rec,7=\dri,8=\boot}{& \boot}}

\begin{document}
    \begin{table}
        \centering
        \begin{tabular}{ l c c c c c } 
            \toprule
            & & \multicolumn{2}{c}{Regime} & \multicolumn{2}{c}{Significance}\\\cmidrule(lr){3-4} \cmidrule(l){5-6} 
            Impact & Horizon & State1 & State2 & CI & Bootstrap\\\midrule
            \dataa & \datab & \datac & \datad & \datae & \dataf \\\bottomrule
        \end{tabular}
    \end{table}
\end{document}

相关内容