Tables – How to Fix Tabularray Table Wrapped by Tcolorbox Spreading Inside Right Margin

marginstablestabularraytcolorbox

Please consider the following code. The tables below are exactly the same. One is wrapped by a tcbbox of tcolorbox package. As you can see, it spreads inside the right margin, breaking page margins. The other one which is not wrapped has the right size. This happens when using an X type column in tabularray. How can I fix this without changing the type of X column? I include page frames for better visualization. It seems to me that the indentation length of left margin is added to the right, probably by tcolorbox? When the wrapped table in not under any question or subquestion which means left margin indentation is zero it does not spread to the right margin.

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

enter image description here

Best Answer

\tcbox creates

a colored box which is fitted to the width of the given ⟨box content⟩.

It doesn't make sense to combine this with a X column which tries to stretch a column to fit a table to the outside \linewidth: The X-column can only stretch after it knows the width of the box, but the box can only know its width after the X has done its work.

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

enter image description here

Related Question