# How to use siunitx and tabularx together?

I'm using the tabular features of siunitx (for what it is worth: v1.3 since I'm on TexLive 2009) to align numbers in tables, eg.

\begin{tabular}{ p{1.5cm} S S S }
bla & 1.23 & 4.5 & 67.89 \tabularnewline
\end{tabular}


and that works perfectly fine.

Now I would like to have this table span a specific width, or rather to have several tables to have the same width. For this I would normally use tabularx

\begin{tabularx}{\textwidth}{ p{1.5cm} X X X }
bla & 1.23 & 4.5 & 67.89 \tabularnewline
\end{tabular}


Now, is there a way to combine the two? To have the number formatting and alignment features of siunitx inside an automatically resizing table?

-
The X column in tabularx is then converted to p{<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 of siunitx 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) –  Martin Scharrer Mar 4 '11 at 14:29
There is a feature request for fixed-width columns for siunitx, which I plan to do for version 2.2 (aiming to work on it next month). –  Joseph Wright Mar 4 '11 at 14:47
@Joseph: Could you have a look on the solution I came up with now. It seems to work, but I'm not very good in understanding LaTeX3 code. –  Martin Scharrer Mar 4 '11 at 14:52
@Martin: Usually I'd ask people to 'keep away' from 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. In siunitx, 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! –  Joseph Wright Mar 13 '11 at 12:18

The X column in tabularx is then converted to p{<width>} where width is automatically calculated. You can change this by redefining the \tabularxcolumn macro like described in the package manual:

\renewcommand{\tabularxcolumn}[1]{<column definition where #1 is the width>}


The S column uses the c column internally. To replace this with p{<width>} you have to manually place the internal column definition of S into \tabularxcolumn. The following code worked in my tests.

\documentclass{article}

\usepackage{array}
\usepackage{tabularx}
\usepackage{siunitx}

\begingroup
% Allow _ and : in macro names (LaTeX3 style)
\catcode\_=11
\catcode\:=11
% Internal code of S
\gdef\tabularxcolumn#1{%
>{\__siunitx_table_collect_begin:Nn S{} }%
p{#1}%  <- this is different (is c in normal S)
<{\__siunitx_table_print:}%
}
\endgroup

\begin{document}

\begin{tabularx}{\textwidth}{p{1.5cm} XXX}
bla & 1.23 & 4.5  & 67.89 \         bla & 1.2  & 4.50 &  7.89 \         bla &  .2  &  .50 & 67.8 \    \end{tabularx}
\end{document}

-
This seems to work. However only with version 2.x of siunitx, not with 1.x due to substantial differences in the syntax. –  janitor048 Mar 4 '11 at 17:00
@janitor048: Yes, such "patches" are always version dependent. I don't have siunitx 1.x right now here, so I can't tell you the right code for it, but the principle should be the same. –  Martin Scharrer Mar 4 '11 at 17:31
Yeah sure. I mainly wanted to mentioned it for other readers.. I tried to modify your solution for version 1 but the package looks very different from version 2 and I couldn't really find my way through the code. But then, I'm not really familiar with the inner structure of latex packages anyways. –  janitor048 Mar 4 '11 at 18:12
@janitor048: I've added a v1 answer using the same approach. –  Joseph Wright Mar 13 '11 at 12:24
This code doesn't work for me. :( –  Sumit May 31 '14 at 11:02

Taking Martin's solution and re-coding for version 1 of siunitx, you'd do something like

\documentclass{article}

\usepackage{siunitx,tabularx}

\makeatletter
\def\tabularxcolumn#1{%
>{\si@tab@begin@S[]}%
p{#1}%  <- this is different (is c in normal S)
<{\si@tab@end@S}%
}
\makeatother

\begin{document}

\begin{tabularx}{\textwidth}{p{1.5cm} XXX}
bla & 1.23 & 4.5  & 67.89 \\
bla & 1.2  & 4.50 &  7.89 \\
bla &  .2  &  .50 & 67.8 \\
\end{tabularx}
\end{document}

-
Yes, that works. Thank you very much! I'll mark Martin's reply as accepted answer though, because it relates to the more recent version of your package. Hope that's fine with you. –  janitor048 Mar 16 '11 at 11:50

The quite new tabu package seems to be very encouraging. Section 1.5 of its manual introduces explicitly an example for how to embed siunitx's »S« columns in »X« columns. Regarding tables this package seems to be an allrounder.

-
Sounds interesting. I'll have a look at it.. Thanks for the suggestion! –  janitor048 Mar 16 '11 at 11:51