当 Tabularray 表格被 tcolorbox 包裹时,其右边距会覆盖页面边框。我该如何修复它?

当 Tabularray 表格被 tcolorbox 包裹时,其右边距会覆盖页面边框。我该如何修复它?

请考虑以下代码。下面的表格完全相同。其中一个由tcbboxoftcolorbox包包裹。如您所见,它在右边距内扩展,打破了页边距。另一个未包裹的表格具有正确的大小。在 中使用 X 类型列时会发生这种情况tabularray。如何在不更改 X 列类型的情况下修复此问题?我包括页面框架以获得更好的可视化效果。在我看来,左边距的缩进长度被添加到右侧,可能是通过tcolorbox?当包裹的表格不在任何问题或子问题下时,这意味着左边距缩进为零,它不会扩展到右边距。

\documentclass[12pt]{article}
\usepackage[a4paper, total={150mm,257mm},left=15mm,top=20mm,showframe]{geometry}
\usepackage{tabularray}
\usepackage[most]{tcolorbox}
\usepackage{enumitem}
\begin{document}
    \begin{enumerate}[label=\arabic*.]
        \item  This is question 1
        
            \tcbox[left=0mm,right=0pt,top=0mm,bottom=0mm,boxsep=0mm]{%
                
                \begin{tblr}{colspec={Q[5cm,c]X[c]},hlines,vlines}
                    text in cell 11 & text in cell 12 \\
                    text in cell 21 & text in cell 22 \\
                    text in cell 31 & text in cell 32 \\
                \end{tblr}}\hfill
        
                \begin{tblr}{colspec={Q[5cm,c]X[c]},hlines,vlines}
                    text in cell 11 & text in cell 12 \\
                    text in cell 21 & text in cell 22 \\
                    text in cell 31 & text in cell 32 \\
                \end{tblr}\hfill
        
            \begin{enumerate}[label=(\alph*)]
                \item Sub question 1
                
                    \tcbox[left=0mm,right=0pt,top=0mm,bottom=0mm,boxsep=0mm]{%
                        
                        \begin{tblr}{colspec={Q[5cm,c]X[c]},hlines,vlines}
                            text in cell 11 & text in cell 12 \\
                            text in cell 21 & text in cell 22 \\
                            text in cell 31 & text in cell 32 \\
                        \end{tblr}}\hfill
            
        \item  This is question 2
                
                        \begin{tblr}{colspec={Q[5cm,c]X[c]},hlines,vlines}
                            text in cell 11 & text in cell 12 \\
                            text in cell 21 & text in cell 22 \\
                            text in cell 31 & text in cell 32 \\
                    \end{tblr}
                \end{enumerate}
    \end{enumerate}
\end{document}

在此处输入图片描述

答案1

\tcbox创建

与给定⟨box content⟩的宽度相符的彩色框。

将它与试图拉伸列以适合表格外部的 X 列结合起来是没有意义的\linewidth:X 列只有在知道框的宽度后才能拉伸,但框只有在 X 完成其工作后才能知道它的宽度。

使用 tcolorbox

\documentclass[12pt]{article}
\usepackage[a4paper, total={150mm,257mm},left=15mm,top=20mm,showframe]{geometry}
\usepackage{tabularray}
\usepackage[most]{tcolorbox}
\usepackage{enumitem}
\begin{document}
    \begin{enumerate}[label=\arabic*.]
        \item  This is question 1
        
          \begin{tcolorbox}[left=0mm,right=0pt,top=0mm,bottom=0mm,boxsep=0mm]%               
                \begin{tblr}{colspec={Q[5cm,c]X[c]},hlines,vlines}
                    text in cell 11 & text in cell 12 \\
                    text in cell 21 & text in cell 22 \\
                    text in cell 31 & text in cell 32 \\
                \end{tblr}
           \end{tcolorbox}     
        
                \begin{tblr}{colspec={Q[5cm,c]X[c]},hlines,vlines}
                    text in cell 11 & text in cell 12 \\
                    text in cell 21 & text in cell 22 \\
                    text in cell 31 & text in cell 32 \\
                \end{tblr}\hfill
        
            \begin{enumerate}[label=(\alph*)]
                \item Sub question 1
                
                \begin{tcolorbox}[left=0mm,right=0pt,top=0mm,bottom=0mm,boxsep=0mm]%     
                        \begin{tblr}{colspec={Q[5cm,c]X[c]},hlines,vlines}
                            text in cell 11 & text in cell 12 \\
                            text in cell 21 & text in cell 22 \\
                            text in cell 31 & text in cell 32 \\
                        \end{tblr}
               \end{tcolorbox}         
            
        \item  This is question 2
                
                        \begin{tblr}{colspec={Q[5cm,c]X[c]},hlines,vlines}
                            text in cell 11 & text in cell 12 \\
                            text in cell 21 & text in cell 22 \\
                            text in cell 31 & text in cell 32 \\
                    \end{tblr}
                \end{enumerate}
    \end{enumerate}
\end{document}

在此处输入图片描述

答案2

这是一个临时的解决方案,我认为我们可以做得更好。

设置环境中的宽度,从环境中tabularray移除。leftmarginenumitem

  • first=\setlength{\mylength}{\the\leftmargin}获取第一级的\mylength 中的长度
  • first=\addtolength{\mylength}{\the\leftmargin}对于第二级
  • 默认为箱形规则(0.5 毫米)tcolorbox

\DrawEnumitemLabel为了更好地看到leftmargin

代码

    \documentclass[12pt]{article}
    %https://tex.stackexchange.com/questions/668168/tabularray-table-when-is-wraped-by-a-tcolorbox-spreads-inside-right-margin-overr
    \usepackage[a4paper, total={150mm,257mm},left=15mm,top=20mm,showframe]{geometry}
    \usepackage{tabularray}
    \usepackage[most]{tcolorbox}
    \usepackage{enumitem}
    \newlength{\mylength}
    \newlength{\myboxrule}
    \setlength{\myboxrule}{0.5mm}%<-- default value in tcolorbox
    \begin{document}
    \begin{enumerate}[label=\arabic*.,first=\setlength{\mylength}{\the\leftmargin}]
        \DrawEnumitemLabel
        \item  \the\leftmargin

            \tcbox[left=0mm,right=0pt,top=0mm,bottom=0mm,boxsep=0mm]{%             
                \begin{tblr}{width=\linewidth-\mylength-2\myboxrule,colspec={Q[5cm,c]X[c]},hlines,vlines}
                    text in cell 11 & text in cell 12 \\
                    text in cell 21 & text in cell 22 \\
                    text in cell 31 & text in cell 32
                \end{tblr}}

            \begin{tblr}{colspec={Q[5cm,c]X[c]},hlines,vlines}
                text in cell 11 & text in cell 12 \\
                text in cell 21 & text in cell 22 \\
                text in cell 31 & text in cell 32
            \end{tblr}

            \begin{enumerate}[label=(\alph*),first=\addtolength{\mylength}{\the\leftmargin}]

                \DrawEnumitemLabel
                \item \the\leftmargin

                        \tcbox[left=0mm,right=0pt,top=0mm,bottom=0mm,boxsep=0mm]{%

                            \begin{tblr}{width=\linewidth-\mylength-2\myboxrule,colspec={Q[5cm,c]X[c]},hlines,vlines}
                                text in cell 11 & text in cell 12 \\
                                text in cell 21 & text in cell 22 \\
                                text in cell 31 & text in cell 32
                            \end{tblr}}

                \item  This is question 2

                        \begin{tblr}{colspec={Q[5cm,c]X[c]},hlines,vlines}
                            text in cell 11 & text in cell 12 \\
                            text in cell 21 & text in cell 22 \\
                            text in cell 31 & text in cell 32
                        \end{tblr}
            \end{enumerate}
    \end{enumerate}
    \end{document}

在此处输入图片描述

答案3

通过对您的文档的几何形状进行两个小小的改变就可以解决您的问题:

\documentclass[12pt]{article}
\usepackage[a4paper, total={150mm,257mm},left=15mm,top=20mm,showframe]{geometry}
\usepackage{tabularray}
\usepackage[most]{tcolorbox}
\usepackage{enumitem}
\begin{document}
    \begin{enumerate}[label=\arabic*.]
        \item  This is question 1
        
        \tcbox[left=0mm,right=0pt,top=0mm,bottom=0mm,boxsep=0mm]{%
            
            \begin{tblr}{colspec={Q[5cm,c]Q[8cm,c]},hlines,vlines}
                text in cell 11 & text in cell 12 \\
                text in cell 21 & text in cell 22 \\
                text in cell 31 & text in cell 32 \\
        \end{tblr}}\hfill
        
        \begin{tblr}{colspec={Q[5cm,c]X[c]},hlines,vlines}
            text in cell 11 & text in cell 12 \\
            text in cell 21 & text in cell 22 \\
            text in cell 31 & text in cell 32 \\
        \end{tblr}\hfill
        
        \begin{enumerate}[label=(\alph*)]
            \item Sub question 1
            
            \tcbox[left=0mm,right=0pt,top=0mm,bottom=0mm,boxsep=0mm]{%
                
                \begin{tblr}{colspec={Q[4cm,c]Q[8.1cm,c]},hlines,vlines}
                    text in cell 11 & text in cell 12 \\
                    text in cell 21 & text in cell 22 \\
                    text in cell 31 & text in cell 32 \\
            \end{tblr}}\hfill
            
            \item  This is question 2
            
            \begin{tblr}{colspec={Q[4cm,c]X[c]},hlines,vlines}
                text in cell 11 & text in cell 12 \\
                text in cell 21 & text in cell 22 \\
                text in cell 31 & text in cell 32 \\
            \end{tblr}
        \end{enumerate}
    \end{enumerate}
\end{document}

输出:

在此处输入图片描述

相关内容