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


