0

Maybe I am overlooking something, but it seems that in a tblr environment, \linewidth behaves differently depending on whether it is used in a cell that spans multiple columns or not.

Consider the following MWE: I placed a rule with the width of \linewidth into each of the cells. Also, I set the column padding to 5pt. In the first example, \linewidth obviously takes the column padding into account and the rules exactly fit into the cells.

In the second example, I added a multi-column cell that spans over both columns. Here we get a warning regarding an overfull box that is exactly twice the column padding. Hence, it seems that \linewidth in this context does not take the column padding into account.

The same seems to hold for the width of vertical borders: If \linewidth is used in a multi-column cell, we get an overfull-box warning for the sum of the column paddings and the border widths.

\documentclass{article}
\usepackage{tabularray, xcolor}

\begin{document}

\noindent
\begin{tblr}{
    colspec = { X X },
    columns = {colsep = 5pt, lightgray},
}
\rule{\linewidth}{2pt} & \rule{\linewidth}{2pt} \\ 
\end{tblr}

\bigskip

% multi-column cell
\noindent
\begin{tblr}{
    colspec = { X X },
    columns = {colsep = 5pt, lightgray},
    cell{2}{1} = {c = 2}{},
}
\rule{\linewidth}{2pt} & \rule{\linewidth}{2pt} \\ 
\rule{\linewidth}{2pt} \\ % Overfull \hbox (10.0pt too wide)
\end{tblr}

\bigskip

% multi-column cell plus borders
\noindent
\begin{tblr}{
    colspec = { | X X | },
    columns = {colsep = 5pt, lightgray},
    cell{2}{1} = {c = 2}{},
}
\rule{\linewidth}{2pt} & \rule{\linewidth}{2pt} \\ 
\rule{\linewidth}{2pt} \\ % Overfull \hbox (10.8pt too wide)
\end{tblr}

\end{document}

enter image description here

I wonder whether this a bug or intended behaviour. Anyways, the only way to solve this that comes to my mind, would be to explicitly set cell{2}{1} = {c = 2}{wd=...}. Does the package provide a more elegant solution?

7
  • the elegant solution is to make a bug report ;-) Oct 5, 2022 at 12:34
  • but actually I'm not sure if \linewidth is really supported in a multicolumn cell, if one restrict the tabular it is still equal to the outside linewidth. Oct 5, 2022 at 12:42
  • 1
    The first row is set in paragraph mode by the X column type and the width of the paragraph sets \linewidth, whereas the multicolumn cell is not set in paragraph mode (which you can verify just by typing a lot of text instead: it won't wrap), so \linewidth remains that of the surrounding paragraph environment. You could set the width cell{2}{1} = {c = 2}{wd=3in} and then it would be in paragraph mode too, but knowing what to set the width isn't any easier than knowing what \linewidth should be.
    – frabjous
    Oct 5, 2022 at 12:44
  • @UlrikeFischer True! If you have three columns a cell spanning two columns with the width of \linewidth will still be as wide as the outside \linewidth pushing the third column outside the surrounding text frame. Oct 5, 2022 at 12:50
  • 1
    but you could open an issue anyway. tabularray should be able to offer an interface for the width. Oct 5, 2022 at 13:07

1 Answer 1

3

After skimming through the package code, I found that the inner width of the current box is stored in a macro named \l__tblr_cell_wd_dim. So, literally one single line of code is actually needed to make the value stored in this macro accessible outside of expl3 syntax:

\documentclass{article}
\usepackage{tabularray, xcolor}

\ExplSyntaxOn
\NewDocumentCommand{\cellwidth}{ }{
    \l__tblr_cell_wd_dim
}
\ExplSyntaxOff

\begin{document}

\noindent
\begin{tblr}{
        colspec      = { X X X }, 
        columns      = { colsep = 5pt },
        cell{3-4}{1} = { c = 3 }{},
        cell{5-6}{1} = { c = 2 }{},
        hlines, 
        vlines
    }
    
    \rule{\cellwidth}{2pt} & \rule{\cellwidth}{2pt} & \rule{\cellwidth}{2pt} \\
    \the\cellwidth         & \the\cellwidth         & \the\cellwidth         \\
    
    \rule{\cellwidth}{2pt}                                                   \\
    \the\cellwidth                                                           \\
    
    \rule{\cellwidth}{2pt}                        & & \rule{\cellwidth}{2pt} \\
    \the\cellwidth                                & & \the\cellwidth         \\
    
\end{tblr}

\end{document}

enter image description here

You must log in to answer this question.

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