21

Suppose we have a table like this:

\begin{tabular}{c}
Heading \\
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?

4 Answers 4

18

Using siunitx you'd do

\documentclass{article}
\usepackage{booktabs,siunitx}
\begin{document}
\begin{tabular}{S[table-format=3.0]}
\toprule
{Heading} \\
\midrule
1 \\
10 \\
100 \\
\bottomrule
\end{tabular}
\end{document}
5
  • I get the error table-format undefined in families key. Commented Jan 18, 2011 at 11:26
  • maybe you need to update your system to get the newest siunitx? Commented Jan 18, 2011 at 11:41
  • @Space: It works correctly for me. Maybe you have an old version of siunitx.
    – TH.
    Commented Jan 18, 2011 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
    Commented Jan 18, 2011 at 12:48
  • @Juan: You were right, I was still running Tex Live 2009. After updating, it works like a charm. Commented Jan 18, 2011 at 13:18
6

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}
\multicolumn{1}{c}{Heading} \\
1 \\
10 \\
100 \\
\end{tabular}

\end{document}
1
  • 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
    Commented Apr 18, 2013 at 11:48
0

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.

enter image description here

\documentclass{article}

\usepackage{eqparbox,collcell}

\newcommand{\columnalign}{}
\newcommand{\columnalignment}[2]{\renewcommand{\columnalign}{\eqmakebox[#1][#2]}}

\begin{document}

\columnalignment{tag1}{r}

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

\columnalignment{tag2}{r}

\begin{tabular}{>{\collectcell\columnalign}c<{\endcollectcell}}
  \multicolumn{1}{c}{Heading} \\
       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>.

-2

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@{\ \ \ \ \ }}
\multicolumn{1}{c}{Heading} \\
1 \\
10 \\
100 \\
\end{tabular}

\end{document}

The drawback is that you need to fiddle with the number of spaces you add to the @-expression.

1
  • 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
    Commented Sep 16, 2019 at 18:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .