0

I found some small problems when using "Redefined TblrTemplates". This redefined template was used to align the longtblr captions to the left.

If the table's caption is long enough, my formatting requires it to hang on the next line. If the table width is equal to the text width, the table caption will hang perfectly on the next line. The problem occurred when the width of the table didn't equal the text width. As you can see, the caption shrunk as the table width decreased.

I did try the solution with the caption package,

\captionsetup{width=1\textwidth}

but it wouldn't work because longtblr operated in its own environment.

I did some research and discovered some answers here. This solution appears to solve the problem with caption width, but for some reason the captions no longer hang. Also, this answer here solved the problem with the caption hanging but also shrank the caption. I failed when I attempted to combine these two methods.

I have insufficient knowledge of programming to modify the Tabularray source code. To achieve a table caption that hung on the next line and had the width of the text width, I'm in great need of help. Any suggestion or answer is appreciated.

\documentclass{article}
\usepackage{tabularray}
\usepackage[singlelinecheck=off]{caption}
    \captionsetup[table]{format=hang}

% Redefined TblrTemplates
\ExplSyntaxOn
\DefTblrTemplate{caption}{normal}{
    \hbox_set:Nn \l__tblr_caption_box{
        \UseTblrTemplate{caption-tag}{default}
        \UseTblrTemplate{caption-sep}{default}
        \UseTblrTemplate{caption-text}{default}
        }
    \dim_compare:nNnTF{\box_wd:N \l__tblr_caption_box}>{\hsize}{
        \UseTblrAlign{caption}
        \UseTblrIndent{caption}
        \hbox_set:Nn \l__tblr_caption_left_box{
            \UseTblrTemplate{caption-tag}{default}
            \UseTblrTemplate{caption-sep}{default}
            }
        \hangindent=\box_wd:N \l__tblr_caption_left_box
        \hangafter=1
        \UseTblrHang{caption}
        \leavevmode
        \hbox_unpack:N \l__tblr_caption_box
        \par
        }{
        \centering
        \makebox[\hsize][l]{\box_use:N \l__tblr_caption_box}
        \par
        }}
  
\DefTblrTemplate{capcont}{normal}{
    \hbox_set:Nn \l__tblr_caption_box{
        \UseTblrTemplate{caption-tag}{default}
        \UseTblrTemplate{caption-sep}{default}
        \UseTblrTemplate{caption-text}{default}
        \space
        \UseTblrTemplate{conthead-text}{default}
        }
    \dim_compare:nNnTF{\box_wd:N \l__tblr_caption_box}>{\hsize}{
        \UseTblrAlign{capcont}
        \UseTblrIndent{capcont}
        \hbox_set:Nn \l__tblr_caption_left_box{
            \UseTblrTemplate{caption-tag}{default}
            \UseTblrTemplate{caption-sep}{default}
            }
        \hangindent=\box_wd:N \l__tblr_caption_left_box
        \hangafter=1
        \UseTblrHang{capcont}
        \leavevmode
        \hbox_unpack:N \l__tblr_caption_box
        \par
        }{
        \centering
        \makebox[\hsize][l]{\box_use:N \l__tblr_caption_box}
        \par
        }}
  
\SetTblrTemplate{caption}{normal}
\SetTblrTemplate{capcont}{normal}
\ExplSyntaxOff

\begin{document}

\begin{table}[h]
    \caption{This is a normal table caption using a table environment. If the table's caption is long enough, the formatting requires it to hang on the next line}
    \centering
\begin{tblr}{
    width=0.5\linewidth,
    colspec={XXX},
    hlines, vlines
    }
Head & Head & Head \\
Head & Data & Data \\
\end{tblr}
\end{table}

\begin{longtblr}[
    caption={This is a long table that uses "Redefined TblrTemplates" suggested by @samcarter\_is\_at\_topanswers.xyz. The table caption was hanging perfectly on the next line with no problems}
    ]{
    colspec={XXX},
    hlines, vlines,
    rowhead=1
    }
Head & Head & Head \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
\end{longtblr}

\begin{longtblr}[
    caption={The problem occurred when the width of the table didn't equal the text width. As you can see, the caption shrunk as the table width decreased}
    ]{
    colspec={*3{X[2cm]}},
    hlines, vlines,
    rowhead=1
    }
Head & Head & Head \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
\end{longtblr}


\end{document}

enter image description here

3
  • After the \leavevmode insert \hsize=\textwidth. Commented Oct 16, 2022 at 7:14
  • I greatly appreciate your advice. The code you provided is usable. It's only a minor point. The caption appears to expand to the width of the text, but the table, which should be centered, aligns to the left. I attempted to apply \centering or \begin{center}, but neither worked. Additional guidance would be appreciated.
    – cccn
    Commented Oct 16, 2022 at 10:14
  • Yes, I noticed that. I think something of the page layout leaked out of the caption. I found a better solution, see my answer. Commented Oct 16, 2022 at 15:40

1 Answer 1

1

Here is a solution that keeps the centering of the table. Based upon https://tex.stackexchange.com/a/630675/113546

\documentclass{article}
\usepackage{tabularray}
\usepackage[singlelinecheck=off]{caption}
    \captionsetup[table]{format=hang}

% Redefined TblrTemplates
\ExplSyntaxOn
\DefTblrTemplate{caption}{normal}{
\makebox[\hsize][c]{% see https://tex.stackexchange.com/a/630675/113546
  \parbox{\textwidth}{%
    \hbox_set:Nn \l__tblr_caption_box{
        \UseTblrTemplate{caption-tag}{default}
        \UseTblrTemplate{caption-sep}{default}
        \UseTblrTemplate{caption-text}{default}
        }
    \dim_compare:nNnTF{\box_wd:N \l__tblr_caption_box}>{\hsize}{
        \UseTblrAlign{caption}
        \UseTblrIndent{caption}
        \hbox_set:Nn \l__tblr_caption_left_box{
            \UseTblrTemplate{caption-tag}{default}
            \UseTblrTemplate{caption-sep}{default}
            }
        \hangindent=\box_wd:N \l__tblr_caption_left_box
        \hangafter=1
        \UseTblrHang{caption}
        \leavevmode
        \hbox_unpack:N \l__tblr_caption_box
        \par
        }{
        \centering
        \makebox[\hsize][l]{\box_use:N \l__tblr_caption_box}
        \par
        }}
    }
  }
  
\DefTblrTemplate{capcont}{normal}{
\makebox[\hsize][c]{% see https://tex.stackexchange.com/a/630675/113546
  \parbox{\textwidth}{%
    \hbox_set:Nn \l__tblr_caption_box{
        \UseTblrTemplate{caption-tag}{default}
        \UseTblrTemplate{caption-sep}{default}
        \UseTblrTemplate{caption-text}{default}
        \space
        \UseTblrTemplate{conthead-text}{default}
        }
    \dim_compare:nNnTF{\box_wd:N \l__tblr_caption_box}>{\hsize}{
        \UseTblrAlign{capcont}
        \UseTblrIndent{capcont}
        \hbox_set:Nn \l__tblr_caption_left_box{
            \UseTblrTemplate{caption-tag}{default}
            \UseTblrTemplate{caption-sep}{default}
            }
        \hangindent=\box_wd:N \l__tblr_caption_left_box
        \hangafter=1
        \UseTblrHang{capcont}
        \leavevmode
        \hbox_unpack:N \l__tblr_caption_box
        \par
        }{
        \centering
        \makebox[\hsize][l]{\box_use:N \l__tblr_caption_box}
        \par
        }}
    }
  }
  
\SetTblrTemplate{caption}{normal}
\SetTblrTemplate{capcont}{normal}
\ExplSyntaxOff

\begin{document}

\begin{table}[h]
    \caption{This is a normal table caption using a table environment. If the table's caption is long enough, the formatting requires it to hang on the next line}
    \centering
\begin{tblr}{
    width=0.5\linewidth,
    colspec={XXX},
    hlines, vlines
    }
Head & Head & Head \\
Head & Data & Data \\
\end{tblr}
\end{table}

\begin{longtblr}[
    caption={This is a long table that uses "Redefined TblrTemplates" suggested by @samcarter\_is\_at\_topanswers.xyz. The table caption was hanging perfectly on the next line with no problems}
    ]{
    colspec={XXX},
    hlines, vlines,
    rowhead=1
    }
Head & Head & Head \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
\end{longtblr}

\begin{longtblr}[
    caption={The problem occurred when the width of the table didn't equal the text width. As you can see, the caption shrunk as the table width decreased}
    ]{
    colspec={*3{X[2cm]}},
    hlines, vlines,
    rowhead=1
    }
Head & Head & Head \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
Head & Data & Data \\ Head & Data & Data \\
\end{longtblr}


\end{document}

enter image description here

1
  • This is truly remarkable. You saved my day in a big way. I appreciate your advice.
    – cccn
    Commented Oct 17, 2022 at 1:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .