Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 11 down vote accepted

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}
share|improve this answer
    
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
add comment

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}
share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.