No more need to complex hacking, use tabularray
package!
With tabularry
you can have columns that are at the same time X
type (option co=<n>
, n
is the coefficient for the extendable column, usually 1, but you can use 2 to have a column with double width, etc.) and S
type (option si=<settings>
).
You don't even have to worry about having non-numeric fields in the S
columns, using option guard
where needed.
In the following example, I created a new column type A
that is both X
and S
and has the table-format
as a parameter. Still, you can also use a Q
column directly, specifying all the parameters.
\documentclass[draft]{article}
\usepackage{tabularray}
\UseTblrLibrary{siunitx}
\sisetup{group-digits=false}
\NewColumnType{A}[1][2.2]{Q[si={table-format=#1},c,co=1]}
\usepackage{caption}
\begin{document}
With \texttt{tabularry} you can have columns that are at the same time \texttt{X} type (option \texttt{co=<n>}, where \texttt{n} is the coefficient for the extendable column, usually 1, but you can use 2 to have a column with double width, etc.) and \texttt{S} type (option \texttt{si=<settings>}).
You don't even have to worry about having non-numeric fields in the \texttt{S} columns, using option \texttt{guard} where needed.
\begin{table}[h]
\caption{A table with columns that are X and \texttt
{siunitx} at the same time}
\begin{tblr}{
colspec={Q[1.5cm]Q[si={table-format=1.2},c,co=1]A[1.1]A},
row{1}={guard}
}
Head 1 & Head 2 & Head 3 & Head 4\\
bla & 1.23 & 4.5 & 67.89\\
bla & 1.23 & 4.5 & 67.89\\
bla & 1.23 & 4.5 & 67.89\\
\end{tblr}
\end{table}
\begin{table}[h]
\caption{A table with vertical lines to show the dimension of the columns}
\begin{tblr}{
colspec={Q[1.5cm]*2{A[1.2]}A},
row{1}={guard},
vlines
}
Head 1 & Head 2 & Head 3 & Head 4\\
bla & 1.23 & 4.5 & 67.89 \\
bla & 1.2 & 4.50 & 7.89 \\
bla & .2 & .50 & 67.8 \\
\end{tblr}
\end{table}
\end{document}

X
column intabularx
is then converted top{<width>}
where width is automatically calculated. You can change this by redefining the\tabularxcolumn
macro like described in the package manual:\renewcommand{\tabularxcolumn}[1]{...}
. I'm sure Joseph Wright (author ofsiunitx
and moderator here) can tell you what to put for...
. I'm couldn't find it out for myself yet. (My LaTeX3-fu is still very weak)siunitx
, which I plan to do for version 2.2 (aiming to work on it next month).siunitx
internals - they are not documented and so are liable to arbitrary changes! Your solution is quite nice, with the only thing I'd say being to use\ExplSyntax(On|Off)
rather than the 'raw'\catcode
approach you've taken. Insiunitx
, I can't just use a 'p' column as I don't know what options are set when the column is defined - I have to wait until I'm inside the cell!tabularx
. Because some time has passed since your comment, I want to ask, whether by now there is a built in possibility to use the S cells, but get a similar behaviour as with X cells.