如何为 csvreader 读取的长 tabularray 表添加标题?

如何为 csvreader 读取的长 tabularray 表添加标题?

我正在使用 csvsimple 包从不同的 CSV 文件中读取补充信息的数据。为了更好地显示内容,我编写了自己的 csvstyle,因为所有 CSV 文件的格式都相同。对于某些表格,数据太长,需要多页。只需将 csvstyle 更改为使用long tabularray而不是 ,即可轻松实现tabularray。现在出现问题,当我想为这个长表添加标题时:如果我将 和 换行,\csvreader例如\caption\begin{table}表格将不再进行分页;如果我使用例如\begin{longtable}而不是\begin{table},编译将不再起作用,每一行都会有另一个标题行,诸如此类,具体取决于包装器。

在此处添加标题的正确步骤是什么?

下面是一个带有 \begin{table} 的最小可重现示例,不进行分页。

\documentclass{article}
\pagestyle{empty}

\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage[table,xcdraw,dvipsnames]{xcolor}
\usepackage[labelfont={bf}, font=small, justification=justified]{caption}
\usepackage[version=4]{mhchem}
\usepackage{chemformula}
\usepackage[separate-uncertainty = true, multi-part-units=single]{siunitx} 
\usepackage{sectsty}
\usepackage{tabularx}
\usepackage{listings}
\usepackage{longtable}
\usepackage{array}
\usepackage{hhline}
\usepackage{booktabs}

% For including csv as tables
\usepackage{tabularray} 
\UseTblrLibrary{siunitx}
\usepackage{csvsimple-l3}

\NewTableCommand\toprule{\hline[0.08em]}
\NewTableCommand\midrule{\hline[0.05em]\vspace{1pt}}
\NewTableCommand\bottomrule{\hline[0.08em]}

\csvstyle{MyStyleLong}{
no head,
before reading=\centering\sisetup{table-number-alignment=center,table-format=3.3,table-auto-round},
long tabularray={
colspec={c@{}S[table-format=3.4]S[table-format=1.2]l},
row{1} = {guard},
column{4} = {cmd=\ch}
},
table head=\toprule\empty & m/z & {Relative Intensity} & Annotation
\\\midrule,
late after line=\\,
late after first line=\\\hline\endhead,
late after last line=,
table foot=\bottomrule,
late after line=\\,
}
 
\begin{filecontents*}[]{test-file-long.csv}
  mz, Intensity, annotation \\
  1.20, 56.01, CH3OH \\
  12.13, 1901.0, CH2OO- + H+\\
  1.20, 56.01, CH3OH \\
  1.20, 56.01, CH3OH \\
  12.13, 1901.0, CH2OO- + H+\\
  1.20, 56.01, CH3OH \\
  1.20, 56.01, CH3OH \\
  12.13, 1901.0, CH2OO- + H+\\
  1.20, 56.01, CH3OH \\
  1.20, 56.01, CH3OH \\
  12.13, 1901.0, CH2OO- + H+\\
  1.20, 56.01, CH3OH \\
  1.20, 56.01, CH3OH \\
  12.13, 1901.0, CH2OO- + H+\\
  1.20, 56.01, CH3OH \\
  1.20, 56.01, CH3OH \\
  12.13, 1901.0, CH2OO- + H+\\
  1.20, 56.01, CH3OH \\
  1.20, 56.01, CH3OH \\
  12.13, 1901.0, CH2OO- + H+\\
  1.20, 56.01, CH3OH \\
  1.20, 56.01, CH3OH \\
  12.13, 1901.0, CH2OO- + H+\\
  1.20, 56.01, CH3OH \\
  1.20, 56.01, CH3OH \\
  12.13, 1901.0, CH2OO- + H+\\
  1.20, 56.01, CH3OH \\
  1.20, 56.01, CH3OH \\
  12.13, 1901.0, CH2OO- + H+\\
  12.13, 1901.0, CH2OO- + H+\\
  1.20, 56.01, CH3OH \\
  1.20, 56.01, CH3OH \\
  12.13, 1901.0, CH2OO- + H+\\
  1.20, 56.01, CH3OH \\
  1.20, 56.01, CH3OH \\
  12.13, 1901.0, CH2OO- + H+\\
  1.20, 56.01, CH3OH \\
  1.20, 56.01, CH3OH \\
  12.13, 1901.0, CH2OO- + H+\\
  1.20, 56.01, CH3OH \\
  1.20, 56.01, CH3OH \\
  12.13, 1901.0, CH2OO- + H+\\
  12.13, 1901.0, CH2OO- + H+\\
  1.20, 56.01, CH3OH \\
  1.20, 56.01, CH3OH \\
  12.13, 1901.0, CH2OO- + H+\\
  1.20, 56.01, CH3OH \\
  1.20, 56.01, CH3OH \\
  12.13, 1901.0, CH2OO- + H+\\
  1.20, 56.01, CH3OH
\end{filecontents*}

\begin{document}

\blindtext

% the first empty is for using S as first column
\begin{table}[h]
\caption{Test}
\csvreader[MyStyleLong, filter expr={test{\ifnumgreater{\thecsvinputline}{1}}}]{test-file-long.csv}{}{\empty & \csvcoli & \csvcolii & \csvcoliii }
\end{table}

\end{document}

答案1

环境longtblr提供了密钥caption作为选项。据我所知,csvsimple不提供对这些选项的任何访问。但是,可以按以下方式重新定义模板的默认文本:

\begin{document}

\blindtext

% the first empty is for using S as first column
\DefTblrTemplate{caption-text}{default}{Test}
\csvreader[MyStyleLong, filter expr={test{\ifnumgreater{\thecsvinputline}{1}}}]{test-file-long.csv}{}{\empty & \csvcoli & \csvcolii & \csvcoliii }

\end{document}

相关内容