如何将 \usepackage{caption} 的格式自动复制到 tabularray 的标题?

如何将 \usepackage{caption} 的格式自动复制到 tabularray 的标题?

对于我正在从事的一个项目,tabularray已在之前的问题的答案中提出建议(请参阅这里这里)。

但是,其他浮动元素(图形、列表等)的标题样式是通过 设置的\usepackage{caption}。问题是 提供的标题tabularray不会对 所做的设置作出反应\usepackage{caption}

由于设计要求所有标题必须看起来相同,因此需要重新设置标题的样式longtblr

初始点

tabularray通过宏提供\DefTblrTemplate一种手动重新设计宏样式的可能方法。

\documentclass{scrreprt}

\usepackage{tabularray} % for table typesetting

% modifying the captions of tabularray
\DefTblrTemplate{caption-tag}{default}{\small \bfseries Table\hspace{0.25em}\thetable}
\DefTblrTemplate{caption-sep}{default}{\small \bfseries :\enskip}
\DefTblrTemplate{caption-text}{default}{\small\InsertTblrText{caption}}

% setting the captions for all floats except tabularray
\usepackage{caption}
\captionsetup{format=plain,labelformat=simple,labelfont=bf,font=small,labelsep = colon}

\begin{document}

\begin{longtblr}[
    caption = {The Caption of the {\ttfamily longtblr} environment.},
  ]{
    colspec = {rccl},
    hline{Z} = {0.08em},
    hline{2} = {0.05em},
    row{1} = {font=\bfseries},
    rowhead = 1,
  }
    date       & time  & time zone & event \\
    2019/01/01 & 00:00 & CET & server installation finished\\
    2019/01/01 & 00:05 & CET & server successfully booted\\
    2019/01/01 & 00:06 & CET & starting xyz daemon\\
\end{longtblr}
    
\begin{table}[h!]
    \centering
    \caption{Caption of a standard table styled with {\ttfamily captions}.}\vspace{0.25cm}
    \begin{tabular}{rccl}
        \textbf{date} & \textbf{time} & \textbf{time zone} & \textbf{event} \\
        \hline
        2019/01/01 & 00:00 & CET & server installation finished\\
        2019/01/01 & 00:05 & CET & server successfully booted\\
        2019/01/01 & 00:06 & CET & starting xyz daemon\\
        \hline
    \end{tabular}
\end{table}

\end{document}

这会产生非常相似的标题样式;除了“表格”和数字之间的空格(这对我来说不是什么大问题,可以轻松修复)。但是,用户必须进行两次设置。

问题

  1. 有没有简单的方法可以将字幕设置复制\usepackage{caption}tabularray
  2. 或者重新定义标准更容易\caption宏比依赖于更容易吗\usepackage{caption}

我尝试使用 if 语句和辅助宏,但这当然会导致丢失\csname或扩展错误。说实话,代码变得相当混乱。

答案1

其实,caption包中提供了\captionof命令,包可以使用这些命令tabularray

\documentclass{scrreprt}

% setting the captions for all floats except tabularray
\usepackage{caption}
\captionsetup{format=plain,labelformat=simple,labelfont=bf,font=small,labelsep = colon}

\usepackage{tabularray}

\ExplSyntaxOn
\prg_generate_conditional_variant:Nnn \tl_if_empty:n { e } { TF }
\let \IfTokenListEmpty = \tl_if_empty:eTF
\ExplSyntaxOff

% modifying the captions of tabularray
\DefTblrTemplate{firsthead}{default}{%
  \addtocounter{table}{-1}%
  \IfTokenListEmpty{\InsertTblrText{entry}}{%
    \captionof{table}{\InsertTblrText{caption}}%
  }{%
    \captionof{table}[\InsertTblrText{entry}]{\InsertTblrText{caption}}%
  }%
}
\DefTblrTemplate{middlehead,lasthead}{default}{%
  \addtocounter{table}{-1}%
  \captionof{table}[]{\InsertTblrText{caption}(Continued)}%
}
\SetTblrTemplate{caption-lot}{empty}

\begin{document}

\listoftables

\begin{longtblr}[
    caption = {The Caption of {\ttfamily longtblr} environment.},
  ]{
    colspec = {rccl},
    hline{Z} = {0.08em},
    hline{2} = {0.05em},
    row{1} = {font=\bfseries},
    rowhead = 1,
  }
    date       & time  & time zone & event \\
    2019/01/01 & 00:00 & CET & server installation finished\\
    2019/01/01 & 00:05 & CET & server successfully booted\\
    2019/01/01 & 00:06 & CET & starting xyz daemon\\
\end{longtblr}

\begin{longtblr}[
    caption = {The Caption of {\ttfamily longtblr} environment.},
    entry = {The Caption in LOT},
  ]{
    colspec = {rccl},
    hline{Z} = {0.08em},
    hline{2} = {0.05em},
    row{1} = {font=\bfseries},
    rowhead = 1,
  }
    date       & time  & time zone & event \\
    2019/01/01 & 00:00 & CET & server installation finished\\
    2019/01/01 & 00:05 & CET & server successfully booted\\
    2019/01/01 & 00:06 & CET & starting xyz daemon\\
\end{longtblr}
    
\begin{table}[h!]
    \centering
    \caption{Caption of a standard table styled with {\ttfamily captions}.}\vspace{0.25cm}
    \begin{tabular}{rccl}
        \textbf{date} & \textbf{time} & \textbf{time zone} & \textbf{event} \\
        \hline
        2019/01/01 & 00:00 & CET & server installation finished\\
        2019/01/01 & 00:05 & CET & server successfully booted\\
        2019/01/01 & 00:06 & CET & starting xyz daemon\\
        \hline
    \end{tabular}
\end{table}

\end{document}

在此处输入图片描述

答案2

从 2024 年 2 月开始(当然从那时开始),你就可以使用这个tblr-extras包了(我是它的作者)

只需加载包并加载caption库。

\documentclass{scrartcl}
\usepackage{caption}
\captionsetup{format=plain,labelformat=simple,labelfont=bf,font=small,labelsep=colon}
\usepackage{tabularray,tblr-extras}
\UseTblrLibrary{caption}

\begin{document}
\listoftables

\begin{longtblr}[
    caption = {The Caption of {\ttfamily longtblr} environment.},
    entry = {The Caption in LOT},
  ]{
    colspec = {rccl},
    hline{Z} = {0.08em},
    hline{2} = {0.05em},
    row{1} = {font=\bfseries},
    rowhead = 1,
  }
    date       & time  & time zone & event \\
    2019/01/01 & 00:00 & CET & server installation finished\\
    2019/01/01 & 00:05 & CET & server successfully booted\\
    2019/01/01 & 00:06 & CET & starting xyz daemon\\
\end{longtblr}

\begin{longtblr}[
    caption = {The Caption of {\ttfamily longtblr} environment.},
    entry = {The Caption in LOT},
  ]{
    colspec = {rccl},
    hline{Z} = {0.08em},
    hline{2} = {0.05em},
    row{1} = {font=\bfseries},
    rowhead = 1,
  }
    date       & time  & time zone & event \\
    2019/01/01 & 00:00 & CET & server installation finished\\
    2019/01/01 & 00:05 & CET & server successfully booted\\
    2019/01/01 & 00:06 & CET & starting xyz daemon\\
\end{longtblr}
\end{document}

相关内容