lvjr / tabularray

Typeset tabulars and arrays with LaTeX3
https://ctan.org/pkg/tabularray
247 stars 22 forks source link

\linewidth does not consider colsep and border width in multi-column cells #316

Open jasperhabicht opened 1 year ago

jasperhabicht commented 1 year ago

It seems that inside a tabu environment, \linewidth behaves differently depending on whether it is used in a cell that spans multiple columns or not.

Consider the following MWE where rules with the width of \linewidth are placed into each of the cells and the column padding is set to 5pt. In the first example, \linewidth obviously considers the column padding and the rules exactly fit the cells.

In the second example, An multi-column cell is added that spans over both columns. Here we get a warning regarding an overfull box that is exactly twice the column padding.

The same seems to hold for the width of vertical borders: If \linewidth is used inside a multi-column cell, an overfull-box warning is issued where the sum of the column padding and the border width is reported.

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

\begin{document}

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

\bigskip

\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

\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}
jasperhabicht commented 1 year ago

I just realised that the above behaviour applies to tabulars in LaTeX in general. So, it is probably not a bug. However, it would be nice if the tabularray would allow the user to easily use \linewidth (or a similar macro) inside multi-column cells as well without the need of manually calculating the width of the spanning cell.

lvjr commented 1 year ago

You may try hspan=minimal option (see the manual).

jasperhabicht commented 1 year ago

Actually, I found that one can probably use the \l__tblr_cell_wd_dim macro, which seems to store the inner width of a cell. One only needs to make it accessible for use outside of Expl3 syntax. Maybe the package could already define a user macro to access the value stored in this macro?


\ExplSyntaxOn
\NewDocumentCommand{\cellwidth}{ }{
    \dim_use:N \l__tblr_cell_wd_dim
}
\ExplSyntaxOf