longtblr 标题随着表格宽度减小而缩小

longtblr 标题随着表格宽度减小而缩小

我发现使用“重新定义的 TblrTemplates”时出现了一些小问题。这个重新定义的模板用于将长 Tblr 标题左对齐。

如果表格的标题足够长,我的格式要求它挂在下一行。如果表格宽度等于文本宽度,表格标题将完美地挂在下一行。当表格宽度不等于文本宽度时,就会出现问题。如您所见,随着表格宽度的减小,标题也会缩小。

我确实尝试过使用 caption 包的解决方案,

\captionsetup{width=1\textwidth}

但它无法工作,因为 longtblr 在其自己的环境中运行。

我做了一些研究并发现了一些答案这里。此解决方案似乎解决了字幕宽度的问题,但由于某种原因,字幕不再挂起。此外,此答案这里解决了字幕挂起的问题,但也缩小了字幕。我尝试将这两种方法结合起来,但失败了。

我没有足够的编程知识来修改 Tabularray 源代码。为了实现表格标题挂在下一行并且宽度与文本宽度相同,我非常需要帮助。任何建议或答案都值得赞赏。

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

在此处输入图片描述

答案1

这是一个保持表格居中的解决方案。基于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}

在此处输入图片描述

相关内容