# How to align integers on the right, but still center them

Suppose we have a table like this:

\begin{tabular}{c}
1 \\
10 \\
100 \\
\end{tabular}


This will center all the numbers. If I want to align them to the right, I could change the column definition to r, but that would but the numbers on the right end of the table. What I would like is for the widest number (in this case 100) to be centered with respect to the column, and the others to be right-aligned to that number. There seem to be a bajillion packages out there that deal in one way or another with aligning numbers by their decimal points, but none seem to offer a simple way to pad numbers to a fixed width. Does anyone have a suggestion?

Using siunitx you'd do

\documentclass{article}
\usepackage{booktabs,siunitx}
\begin{document}
\begin{tabular}{S[table-format=3.0]}
\toprule
\midrule
1 \\
10 \\
100 \\
\bottomrule
\end{tabular}
\end{document}

• I get the error table-format undefined in families key. – Björn Pollex Jan 18 '11 at 11:26
• maybe you need to update your system to get the newest siunitx? – Juan A. Navarro Jan 18 '11 at 11:41
• @Space: It works correctly for me. Maybe you have an old version of siunitx. – TH. Jan 18 '11 at 11:42
• @Space_C0wb0y. I've used the settings appropriate for v2 of siunitx. If you have version 1 (which sounds likely) replace table-format by tabformat and use \multicolumn for the heading. – Joseph Wright Jan 18 '11 at 12:48
• @Juan: You were right, I was still running Tex Live 2009. After updating, it works like a charm. – Björn Pollex Jan 18 '11 at 13:18

If you want it less complicated then use the \makebox directly for the numbers and \multicolumn for the header.

\documentclass[a5paper]{article}
\usepackage{array,calc}

\newcolumntype{C}{>{\FormatNo} c }
\def\FormatNo\ignorespaces#1\\{%
\ignorespaces\makebox[\widthof{000}][r]{#1}\tabularnewline}
\begin{document}

\begin{tabular}{C}
1 \\
10 \\
100 \\
\end{tabular}

\end{document}

• Just to be picky: \ignorespaces in the replacement text of the \FormatNo macro does nothing (apart expanding \makebox; its place should be before #1. – egreg Apr 18 '13 at 11:48

The process can be somewhat automated using eqparbox's \eqmakebox[<tag>][<align>]{<stuff>} and the help of collcell to collect the contents - <stuff> of each cell and pass that to \eqmakebox.

\documentclass{article}

\usepackage{eqparbox,collcell}

\newcommand{\columnalign}{}

\begin{document}

\columnalignment{tag1}{r}

\begin{tabular}{>{\collectcell\columnalign}c<{\endcollectcell}}
1000 \\
1 \\
10 \\
100
\end{tabular}

\columnalignment{tag2}{r}

\begin{tabular}{>{\collectcell\columnalign}c<{\endcollectcell}}
words \\
of \\
differing \\
length
\end{tabular}

\end{document}


Note that this requires at least two compilations with every change in the largest element associated with any specific <tag>.

There is no need to use a special LaTeX package. You can use the following standard LaTeX construction with an @-expression:

\documentclass{article}
\begin{document}
\begin{tabular}{r@{\ \ \ \ \ }}

• This is very tedious, needs controlling every time the table, the font or even \tabcolsep is changed and only allows approximate centering. All in all, this doesn't really seem like a (viable) solution. – schtandard Sep 16 at 18:57