Aligning numbers on the decimal point with siunitx S columns
To align numbers on the decimal point in LaTeX, load siunitx and replace r or c with an S column that has a table-format, such as S[table-format=2.1]. Text in that column, including the header, must be wrapped in braces, and every number must fit the format. Without siunitx, giving each column the same number of decimals and right-aligning it works for simple cases.
Convert a table from a PDF or screenshot →By TableToLatex · · Examples tested with pdfLaTeX, TeX Live 2023, siunitx 3.3
What an S column does
In an ordinary r or c column each cell is just text, so 9.5 and 71.2 line up only when they happen to have the same number of digits. An S column from siunitx reads each cell as a number and aligns it on the decimal marker whatever the digit counts. table-format tells it how much room to reserve: S[table-format=2.1] means up to two digits before the marker and one after.
% preamble: \usepackage{booktabs} \usepackage{siunitx}
\begin{tabular}{l S[table-format=2.1] S[table-format=2.1] S[table-format=1.3]}
\toprule
{Method} & {Top-1 (\%)} & {Top-5 (\%)} & {Loss} \\
\midrule
Baseline & 71.2 & 90.1 & 1.254 \\
Ours & 74.8 & 92.0 & 1.102 \\
Ablation & 8.5 & 9.3 & 0.994 \\
\bottomrule
\end{tabular}Add room for a sign with a leading minus (-1.2) and for an exponent with e (1.2e-1); siunitx then prints a true minus sign and powers of ten. For mean ± error, use two S columns with the plus-minus sign between them so both halves stay aligned.
\begin{tabular}{l S[table-format=2.1] @{\,$\pm$\,} S[table-format=1.1]}
\toprule
{Method} & \multicolumn{2}{c}{Accuracy (\%)} \\
\midrule
Baseline & 71.2 & 0.4 \\
Ours & 74.8 & 0.3 \\
\bottomrule
\end{tabular}The format only reserves space; it never changes a value. Rounding is a separate option, round-mode=places with round-precision, and it changes what readers see, so apply it deliberately. Headers over such columns are built as in the guide to grouped headers, and marking the best number in a column is covered in bolding best results.
Switch a table to S columns in six steps
- 1
Load siunitx
Add \usepackage{siunitx} after booktabs. Version 3 is current; very old installations ship version 2, where some options differ.
- 2
Find the widest number
For each numeric column, count the most digits before and after the decimal point, and note whether any value is negative.
- 3
Swap the column letter
Replace r or c with S[table-format=2.1], using your own counts. Text columns keep l.
- 4
Brace every non-number
Header cells and placeholders inside an S column go in braces, such as {Top-1 (\%)} or {--}. The label column on the left needs nothing.
- 5
Move units out of the cells
Put % or units in the header so that every cell is a plain number.
- 6
Compile and check the PDF
"Invalid number" names a cell siunitx could not read. Fix that cell rather than loosening the format. Some stray characters print oddly instead of failing, so look at the output too.
table-format examples
| Values in the column | Column type | What you get |
|---|---|---|
| 71.2 and 8.5 | S[table-format=2.1] | Aligned on the point, one decimal |
| 0.50 and -0.25 | S[table-format=-1.2] | Room for a sign; a true minus is printed |
| 1.254 and 0.994 | S[table-format=1.3] | Three decimals |
| 1.50e-3 and 5.00e-4 | S[table-format=1.2e-1] | Printed as a power of ten |
| 71.2 ± 0.4 | S[table-format=2.1] @{\,$\pm$\,} S[table-format=1.1] | Two columns, both halves aligned |
What goes wrong
- A garbled header with no error. Plain text in an S column is parsed as a number. In a test, the header
Top-1was read as a minus sign and a digit and printed as overlapping glyphs. Put it in braces. - Invalid number '--'. A dash placeholder is not a number. Use
{--}or leave the cell empty. - Commas.
1,234.5is an Invalid number error, and1,234is quietly read as 1.234 because a comma is accepted as a decimal marker. Write 1234 and let siunitx format it. - Brackets.
(0.5), the accounting style for a negative, is not a number to siunitx. Rewrite it as-0.5. - Numbers spilling over. A number wider than the format pushes into the next column and raises an Overfull hbox warning. Widen the format.
- Options from version 2. Settings copied from old answers, such as
detect-inline-weight, produce a warning in version 3.
How TableToLatex helps
TableToLatex right-aligns a column with r when every cell looks like a number and left-aligns it otherwise. Signs, currency symbols, thousands separators, percent signs, brackets, bold numbers and a Unicode minus all count as numbers there, and placeholders such as --, n/a or an empty cell are ignored, but a ± value or a unit after the number leaves the whole column l. For true decimal alignment, change the letters to S yourself, brace the header cells, and clean any cell with brackets or commas first. Start from the LaTeX table generator. The table tool is free and runs in your browser. Paste a tab-separated range from a spreadsheet. See pricing.
Questions about siunitx columns
How do I align numbers on the decimal point in LaTeX?+
Load siunitx and use an S column with a table-format, for example S[table-format=2.1]. Each cell is read as a number and aligned on the decimal marker, whatever its digit count.
What does table-format mean in siunitx?+
It describes the widest number the column must hold: digits before the marker, a dot, digits after it. A leading minus reserves room for a sign, and e followed by digits reserves room for an exponent.
Why does siunitx say Invalid number?+
A cell in an S column is not a number. Wrap text, headers and placeholders in braces, for example {--}, remove thousands separators and brackets, and move percent signs and units into the header.
Can I align decimals without siunitx?+
Yes. Give every value in the column the same number of decimals and right-align it with r, or use the dcolumn package with a column such as D{.}{.}{2.1}. siunitx is more flexible for signs and exponents.
How do I show mean ± error values aligned?+
Use two S columns with the plus-minus sign between them, as in S[table-format=2.1] @{\,$\pm$\,} S[table-format=1.1], and a \multicolumn header spanning both. Both the means and the errors stay aligned.
Does siunitx round my numbers?+
Not by default. Rounding is a separate option, round-mode=places with round-precision, and it changes what readers see. Switch it on only when you mean to, and keep the unrounded values elsewhere.
Does an S column work with booktabs and grouped headers?+
Yes. The rules are unaffected, and a group header over two S columns is a normal \multicolumn{2}{c}{Group}. Header cells for a single S column go in braces.
Keep reading
- Bold best result
textbf, bfseries, siunitx detect-weight, a \best macro, ties and direction.
- Special characters
Escaping %, &, _, $, # and friends, inline math, and pasted Unicode errors.
- Grouped headers
Grouped headers with \multicolumn, \multirow and trimmed \cmidrule rules.
Have a table in a spreadsheet? Paste it into the table tool, or see all LaTeX table guides.