\centerlastline 用于 tabularray 中的标题

\centerlastline 用于 tabularray 中的标题

我正在尝试迁移tabularray,但字幕格式却无法解决。我需要字幕对齐,除了对于最后一行,应将其居中;如果只有一行,则应将其居中。此外,标题标签应在其自己的行上刷新。

我正在使用memoir文档类,因此在我按照其手册进行操作并实现我需要的操作之前,如下所示:

\documentclass[a4paper,14pt,openany]{memoir}

\usepackage{tabularx}

\newcommand{\SCTableFont}{\small} % 12pt
\newcommand{\SCTableLabelFont}{\SCTableFont\itshape}
\newcommand{\SCTableCaptionFont}{\SCTableFont\bfseries}

\setfloatadjustment{table}{
\captionnamefont{\hfill\SCTableLabelFont}
\captiontitlefont{\SCTableCaptionFont}
\captiondelim{}
\captionstyle[\centering\\]{\centerlastline\\} % <--- !!!
\SCTableFont
}

\begin{document}

\begin{table} [htbp]%
\caption{Not too long caption should be centered}%
\label{tbl:not-too-long}
\begin{tabularx}{\textwidth}{|X|X|}
\hline
first & second \\
\hline
\end{tabularx}
\end{table}

\begin{table} [htbp]%
\caption{If a caption is long enough to not fit in a single line, it should be rendered as a block paragraph but with the last line centered. If a caption is long enough to not fit in a single line, it should be rendered as a block paragraph but with the last line centered.}%
\label{tbl:long}
\begin{tabularx}{\textwidth}{|X|X|}
\hline
first & second \\
\hline
\end{tabularx}
\end{table}

\ref{tbl:not-too-long}, \ref{tbl:long}

\end{document}

在此处输入图片描述

请注意,它memoir提供了 (1)\captionstyle一个可选参数,当标题由单行组成时才使用;以及 (2) \centerlastline,它完全满足我的需要。

现在我试图从中获得相同的结果tabularray,但却失败了:

\documentclass[a4paper,14pt,openany]{memoir}

\usepackage{tabularray}

\newcommand{\SCTableFont}{\small} % 12pt
\newcommand{\SCTableLabelFont}{\SCTableFont\itshape}
\newcommand{\SCTableCaptionFont}{\SCTableFont\bfseries}

\DefTblrTemplate{caption}{default}{%
{\hfill % <--- \raggedleft does not work
\SCTableLabelFont
Table\hspace{0.25em}\thetable}%
\newline
{\centering % <--- does not work; \centerlastline does not work either
\SCTableCaptionFont
\InsertTblrText{caption}}}

\begin{document}

\begin{table} [htbp]%
\begin{talltblr}[
  caption = {Not too long caption should be centered},
  label = {tbl:not-too-long}
  ]{|X|X|}
\hline
first & second \\
\hline
\end{talltblr}
\end{table}

\begin{table} [htbp]%
\begin{talltblr}[
  caption = {If a caption is long enough to not fit in a single line, it should be rendered as a block paragraph but with the last line centered. If a caption is long enough to not fit in a single line, it should be rendered as a block paragraph but with the last line centered.},
  label = {tbl:long}
  ]{|X|X|}
\hline
first & second \\
\hline
\end{talltblr}
\end{table}

\ref{tbl:not-too-long}, \ref{tbl:long}

\end{document}

在此处输入图片描述

显然,这里有一些我不明白的程序。我确信我应该使用\SetTblrStyle;类似于

\SetTblrStyle{caption-text}{halign=???}

除了 (1)tabularray仅提供l (left), c (center), r (right) or j (justify)此键之外,(2) 据我所知,如果您重新定义整个标题模板(而不是其各个部分),则样式不适用。 (反过来,我无法重新定义单独的部分,因为书写\DefTblrTemplate{caption-sep}{default}{\newline}不起作用)。

我看见这让我毛骨悚然,还有其他办法吗?

答案1

在尝试解决这个问题的过程中,我偶然发现了一个非常有趣的评论本课程的作者memoir

我维护回忆录并定期使用带有回忆录的标题,因为它具有比回忆录开箱即用所支持的功能更多的功能。

...这让我立刻想起了 Gaben或多或少推荐 Unity 而不是 Source

所以我咬紧牙关,从内置memoir字幕转变为caption

之后,将上述caption内容整合到以下内容tabularray就很简单了:

\documentclass[a4paper,14pt,openany]{memoir}

\usepackage{tabularray}
\usepackage{caption}

\DeclareCaptionStyle{SCTableCaptionStyle}{%
font=small,% affecting the whole caption
labelfont=it,% affects the caption label and separator
textfont=bf,% only affects the caption text
labelsep=period,%
skip=2pt,%
position=above,%
parskip=0pt,%
indention=0cm%
}

\DeclareCaptionFormat{SCTableCaptionFormat}{\raggedleft#1\par\centerlast#3}

\captionsetup[table]{%
style=SCTableCaptionStyle,%
format=SCTableCaptionFormat%
}


% https://tex.stackexchange.com/a/628973

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

\DefTblrTemplate{firsthead}{default}{%
  \addtocounter{table}{-1}%
  \captionsetup{type=table}%
  \SCIfTokenListEmpty{\InsertTblrText{entry}}{%
    \caption{\InsertTblrText{caption}}%
  }{%
    \caption[\InsertTblrText{entry}]{\InsertTblrText{caption}}%
  }%
}


% While we're at it, let's make continuation/conclusion captions for longtblr too

\DefTblrTemplate{middlehead}{default}{%
  \addtocounter{table}{-1}%
  \captionsetup{type=table,style=SCContTableCaptionStyle}%
  \caption[]{\InsertTblrText{caption}}%
}

\DefTblrTemplate{lasthead}{default}{%
  \addtocounter{table}{-1}%
  \captionsetup{type=table,style=SCConclTableCaptionStyle}%
  \caption[]{\InsertTblrText{caption}}%
}

% Allows to avoid duplicate entries in the LoT.
% https://tex.stackexchange.com/questions/628900/628973#comment1572626_628973.
\SetTblrTemplate{caption-lot}{empty}

\DeclareCaptionLabelFormat{SCTableContConclCaptionLabelFormat}{tbl. #2}
\DeclareCaptionFormat{SCTableContCaptionFormat}{\raggedleft Continuation of #1}
\DeclareCaptionFormat{SCTableConclCaptionFormat}{\raggedleft Conclusion of #1}

\DeclareCaptionStyle{SCContTableCaptionStyle}{
style=SCTableCaptionStyle,%
font+=it,
format=SCTableContCaptionFormat,%
labelformat=SCTableContConclCaptionLabelFormat%
}

\DeclareCaptionStyle{SCConclTableCaptionStyle}{
style=SCTableCaptionStyle,%
font+=it,
format=SCTableConclCaptionFormat,%
labelformat=SCTableContConclCaptionLabelFormat%
}

% Remove "Continued on next page" at table bottom
\SetTblrTemplate{contfoot}{empty}

\begin{document}

\begin{table} [htbp]%
\begin{talltblr}[
  caption = {Not too long caption should be centered},
  label = {tbl:not-too-long}
  ]{|X|X|}
\hline
first & second \\
\hline
\end{talltblr}
\end{table}

\begin{table} [htbp]%
\begin{talltblr}[
  caption = {If a caption is long enough to not fit in a single line, it should be rendered as a block paragraph but with the last line centered. If a caption is long enough to not fit in a single line, it should be rendered as a block paragraph but with the last line centered.},
  label = {tbl:long}
  ]{|X|X|}
\hline
first & second \\
\hline
\end{talltblr}
\end{table}

\ref{tbl:not-too-long}, \ref{tbl:long}

\end{document}

在此处输入图片描述

我禁不住想,对于在字幕支持方面投入的所有疯狂工作tabularray——所有那些Templates、Styles 和s——也许追随作者Theme的脚步并仅添加与包的深度集成会是一个更好的选择。memoircaption

相关内容