如何在 nicematrix 包中的 NiceTabular 中使用 csvsimple?

如何在 nicematrix 包中的 NiceTabular 中使用 csvsimple?

我是 Latex 的新手,对于从 csv 中获取我想要的表格有点不知所措。

  • 到目前为止,我已经使用以下代码在乳胶中显示带有灰色和白色行的 csv 表:
\documentclass{article}

\usepackage[l3]{csvsimple}
\usepackage[table]{xcolor}
\usepackage{nicematrix}% for later

\usepackage{filecontents}
\begin{filecontents*}[overwrite]{data.csv}
Pair,Estimate,SE,df,z.ratio,p.value
Control - Decomposed,-2.868,0.434,Inf,-6.61,0.001
Control - Low kelp,-2.596,0.428,Inf,-6.065,0.001
Control - High kelp,-2.533,0.429,Inf,-5.91,0.001
Decomposed - Low kelp,0.272,0.171,Inf,1.591,0.384
Decomposed - High kelp,0.335,0.178,Inf,1.877,0.238
Low kelp - High kelp,0.063,0.171,Inf,0.366,0.983
\end{filecontents*}

\csvstyle{mystyle}{
  no head,
  table head = \\\rowcolor{gray!50},
  late after line = \ifcsvoddrow{\\\rowcolor{gray!15}}{\\\rowcolor{white}}
}

\begin{document}

\begin{table}
\centering
\caption{Post-hoc test of GLMM\label{phoctab}}
\csvreader[tabular = lcccc, mystyle]{data.csv}{}{\csvcoli & \csvcolii & \csvcoliii & \csvcolv & \csvcolvi}
\end{table}

\end{document}

结果表:

唯一的问题是列之间的空白非常小。我尝试应用@F. Pantigny 的使用包 nicematrix (NiceTabular) 解决这个问题。

  • 但是我无法将 csvsimple 和 nicematrix 结合起来,因此结果就是像上面那样没有空白的表格……

  • 以下是我的开始:

\begin{NiceTabular}{lcccc}
\CodeBefore
    \rowcolor{gray!50}{1}
    \rowcolors{2}{gray!25}{white}
\Body
\csvreader[no head]{data.csv}{}{\csvcoli & \csvcolii & \csvcoliii & \csvcolv & \csvcolvi}
\end{NiceTabular}
  • 无法编译:
Fatal Package nicematrix Error: Too much columns.
(nicematrix)                      In the row 1, you try to use more columns
(nicematrix)                      than allowed by your environment
(nicematrix)                      {NiceTabular}. The maximal number of
(nicematrix)                      columns is 5 (plus the potential exterior
(nicematrix)                      ones).This error is fatal.
  • 这是我经过 2 小时的实验和研究得出的最好的结果:
\begin{NiceTabular}{lcccc}
\csvreader[mystyle]{data.csv}{}{\csvcoli & \csvcolii & \csvcoliii & \csvcolv & \csvcolvi}
\end{NiceTabular}

结果表: 这确实可以编译,并且列之间没有空白,但看起来很糟糕。

我该如何让它工作?抱歉,如果某个地方已经有这样的问题,我确实试图找到一个!

答案1

您忘记添加late after line = \\

\documentclass{article}
\usepackage[l3]{csvsimple}
\usepackage{xcolor}
\usepackage{nicematrix}

\begin{filecontents*}[overwrite]{data.csv}
Pair,Estimate,SE,df,z.ratio,p.value
Control - Decomposed,-2.868,0.434,Inf,-6.61,0.001
Control - Low kelp,-2.596,0.428,Inf,-6.065,0.001
Control - High kelp,-2.533,0.429,Inf,-5.91,0.001
Decomposed - Low kelp,0.272,0.171,Inf,1.591,0.384
Decomposed - High kelp,0.335,0.178,Inf,1.877,0.238
Low kelp - High kelp,0.063,0.171,Inf,0.366,0.983
Low kelp - High kelp,0.063,0.171,Inf,0.366,0.983
\end{filecontents*}

\begin{document}

\begin{NiceTabular}{lcccc}
\CodeBefore
  \rowcolor{gray!50}{1}
  \rowcolors{2}{gray!15}{}
\Body
\csvreader[no head,late after line = \\]{data.csv}{}%
  {\csvcoli & \csvcolii & \csvcoliii & \csvcolv & \csvcolvi } 
\end{NiceTabular}

\end{document}

与往常一样nicematrix,您需要进行多次编译。

上述代码的输出

相关内容