2

I’m trying to migrate to tabularray and I’m stuck with caption formatting. I need the caption to be justified, except for the last line, which should be centered; if there is only one line, it should be centered. Also the caption tag should be flushed right on its own line.

I’m using memoir document class, so before I was following its manual and achieving what I need like this:

\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}

enter image description here

Notice that memoir provides (1) \captionstyle with an optional argument, which is used exactly when the caption consists of a single line; and (2) \centerlastline, which does exactly what I need.

Now I’m trying to get the same result from tabularray, and I’m failing miserably:

\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}

enter image description here

Obviously there is some procedure here that I’m not getting. I was sure that I should use \SetTblrStyle for this; something along the lines of

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

except (1) tabularray provides only l (left), c (center), r (right) or j (justify) for this key, and (2) as far as I understand styles do not apply if you redefine the whole caption template (instead of its separate parts). (And in turn, I can’t redefine separate parts because writing \DefTblrTemplate{caption-sep}{default}{\newline} does not work).

I saw this and it made my hair stand on end, is there any other way?

ScumCoder
  • 1,639

1 Answers1

1

In the course of trying to solve this problem, I stumbled upon a very interesting comment by the author of the memoir class:

I maintain memoir and regularly use caption with memoir as it has more features than what memoir will ever support out of the box.

...which instantly reminded me of Gaben more or less recommending Unity instead of Source.

So I bit the bullet and made the transition from built-in memoir captions to caption.

After that, integrating said caption into tabularray was straightforward:

\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}

enter image description here

I can’t help but think that for all the insane work that went into captions support in tabularray – all those Templates, Styles and Themes – maybe following the footsteps of memoir’s author and just adding deep integration with the caption package would’ve been a better choice.

ScumCoder
  • 1,639