# Basic S Column Alignment with siunitx package

OK, what am I missing here? Why is the S column not aligning on the decimal:

The desired result is the data aligned on the decimal and centered within the column:

## Code:

\documentclass{article}
\usepackage{siunitx}

\begin{document}
\begin{tabular}{S}
\multicolumn{1}{c}{Measured Values} \\
\SI{0.003 1}{\meter} \\
\SI{0.003 123}{\meter} \\
\SI{0.003 45}{\meter} \\
\end{tabular}
\end{document}

-

The S column type is meant to take inputs in the same format as the single mandatory argument to siunitx's \num{} command. Anything else is interpreted as text (for headings, etc.) and is centered about the decimal point location.

This is why you see the overall centering behavior: The \SI{}{} is interpreted as something that is "not a number" and is formatted as header text or other material.

To typeset the units, use an s column, which takes inputs in the same form as the mandatory argument to the \si{} command. This should only be used if the units are not the same for every row in the table.

Example:

\documentclass{article}
\usepackage{siunitx}

\begin{document}
\begin{tabular}{S[table-format=1.6]s}
\multicolumn{2}{c}{Measured Values} \\
0.0031   & \meter \\
0.003123 & \meter \\
0.00345  & \meter \\
\end{tabular}
\end{document}


If the units of a particular column are the same for every row (as is the case for most tables), use the recommendation of Section 9.3 of the siunitx manual; that is, represent the numbers using dimensionless ratios:

\documentclass{article}
\usepackage{siunitx}

\begin{document}
\begin{tabular}{S[table-format=1.6]}
{Measured Values/\si{\meter}} \\
0.0031   \\
0.003123 \\
0.00345  \\
\end{tabular}
\end{document}


Or (my preference):

\documentclass{article}
\usepackage{siunitx}

\begin{document}
\begin{tabular}{S[table-format=1.6]}
{Measured Values (\si{\meter})} \\
0.0031   \\
0.003123 \\
0.00345  \\
\end{tabular}
\end{document}


Notice how table-format=1.6 is used to specify the maximum number of integer and decimal digits in each column entry. This allows for proper centering of the data. Also, I used curly braces around the heading to "protect" it and mark it as non-numerical data. This is not required here, because siunitx is pretty smart about detecting these cases. The curly brace protection is useful in certain cases where siunitx gets confused (integer index headings, for example).

-
Hmmm, I just I just assumed that the S column type from the siunitx package would know about the \SI{}{} macro form the \siunitx package. :-) –  Peter Grill Aug 13 at 0:28
@PeterGrill Well \SI wouldn't make sense in any case: it's a quantity not a number, so what the logical outcome of 'align in a numerical column' is is undefined. –  Joseph Wright Aug 13 at 7:18

In case if you have to use different units for different numbers, you have these possibilities with table-space-text-post and table-align-text-post:

\documentclass{article}
\usepackage{siunitx}

\begin{document}
\begin{tabular}{S[table-format=1.6,table-space-text-post = \si{\meter}]}
{Measured Values} \\
0.0031\si{\meter}   \\
0.003123\si{\meter} \\
0.00345\si{\meter}  \\
\end{tabular}
\begin{tabular}{S[table-format=1.7,table-align-text-post = false]}  %% note 1.7 here just to align things at center
{Measured Values} \\
0.0031\,\si{\meter}   \\
0.003123\,\si{\meter} \\
0.00345\,\si{\meter}  \\
\end{tabular}
\end{document}


There is also pre version of above commands - table-align-text-pre which does the same before the number.

-
Very nice; I didn't know about these keys until reading your answer. Is it possible to add a thin space to obtain the proper spacing at the widest number? I tried table-space-text-post = {\,\si{\meter}} but the result did not change. –  Paul Gessler Aug 13 at 0:27
I think you should add a \, before the unit to get the same spacing (as the \SI{}{}) macro would produce) - especially in the second example. –  Peter Grill Aug 13 at 0:27
@PaulGessler: I agree, but it may be an extra work for Joseph :) –  Harish Kumar Aug 13 at 0:35
@PaulGessler If you want something to appear in every row, the array >{...} mechanism is the way to go. (I'd strongly prefer dividing through as you've said in your answer, otherwise there is repeated information for no reason.) –  Joseph Wright Aug 13 at 6:09
@HarishKumar BTW, you'd be better with \,\si{\metre} as this way the unit is in 'interpreted' mode (would make a difference if the unit was e.g. \per\metre). –  Joseph Wright Aug 13 at 6:10