5

I have a table with a relatively long heading of one to two digit numbers. Here is my initial code:

\documentclass{article}
\usepackage{booktabs}
\usepackage{textcomp}
\begin{document}
\begin{tabular}{ccr}
\toprule
\textnumero & Symbol & Number of Units \\ \midrule
1 & A & 9  \\
2 & B & 10 \\
3 & C & 7  \\
4 & D & 12 \\
5 & E & 2  \\
6 & F & 3  \\
7 & G & 4  \\
[![8 & H & 5  \\][1]][1]
9 & I & 5  \\
\bottomrule
\end{tabular}
\end{document}

enter image description here

As you can see, the whole table ends up looking rather unbalanced. Now, obviously, I could just center the whole column, like so:

...
\begin{tabular}{ccc}
...

enter image description here

The column is centered under the heading, but of course the numbers lose their right justification, which is less than ideal. My next intuition was to try to use the \multicolumn to try and get the heading centered, while leaving the rest right justifed:

...
\begin{tabular}{ccr}
\toprule
\textnumero & Symbol & \multicolumn{1}{c}{Number of Units} \\ \midrule
...

Unfortunately, this resulted in the same output as the first. I'm really not sure what I should do here. Is there any way to accomplish what I'm looking for? Ideally, the end result would look something like this (manually edited the screenshot in GIMP):

enter image description here

  • use dcolumn or siunitx packages and their decimal alignment columns (D or S respectively) – David Carlisle Sep 19 '16 at 16:30
5

siunitx S-column can align content in the way you want. Below I've used table-format=2 to centre 2-digit integers:

enter image description here

\documentclass{article}

\usepackage{booktabs,textcomp,siunitx}

\begin{document}

\begin{tabular}{ c c S[table-format=2] }
  \toprule
  \textnumero & Symbol & \multicolumn{1}{c}{Number of Units} \\
  \midrule
  1 & A & 1  \\
  2 & B & 22 \\
  3 & C & 3  \\
  4 & D & 44 \\
  5 & E & 5  \\
  6 & F & 66 \\
  7 & G & 7  \\
  8 & H & 88 \\
  9 & I & 9  \\
  \bottomrule
\end{tabular}

\end{document}

Non-integer headers should be set using \multicolumn{1}{<colspec>}{<header>}.


Here is a comparable manual alternative, using \phantoms:

\documentclass{article}

\usepackage{booktabs,textcomp}

\begin{document}

\begin{tabular}{ *{3}{c} }
  \toprule
  \textnumero & Symbol & Number of Units \\
  \midrule
  1 & A & \phantom{0}1  \\
  2 & B & 22 \\
  3 & C & \phantom{0}3  \\
  4 & D & 44 \\
  5 & E & \phantom{0}5  \\
  6 & F & 66 \\
  7 & G & \phantom{0}7  \\
  8 & H & 88 \\
  9 & I & \phantom{0}9  \\
  \bottomrule
\end{tabular}

\end{document}

Centered left-alignment is possible, but with a different approach. Below I've used collcell to collect each cell entry within the last column and examine it. If the number is less than 10, we add a \phantom{0}:

\documentclass{article}

\usepackage{booktabs,textcomp,collcell}

\newcommand{\padright}[1]{#1\ifnum#1<10 \phantom{0}\fi}

\begin{document}

\begin{tabular}{ c c >{\collectcell\padright}c<{\endcollectcell} }
  \toprule
  \textnumero & Symbol & \multicolumn{1}{c}{Number of Units} \\
  \midrule
  1 & A & 1  \\
  2 & B & 22 \\
  3 & C & 3  \\
  4 & D & 44 \\
  5 & E & 5  \\
  6 & F & 66 \\
  7 & G & 7  \\
  8 & H & 88 \\
  9 & I & 9  \\
  \bottomrule
\end{tabular}

\end{document}

One would have to expand this manage wider elements as needed. For example, here's how to manage 100's:

\newcommand{\padright}[1]{#1%
  \ifnum#1<10 \phantom{0}\fi
  \ifnum#1<100 \phantom{0}\fi}

or 1000's:

\newcommand{\padright}[1]{#1%
  \ifnum#1<10 \phantom{0}\fi
  \ifnum#1<100 \phantom{0}\fi
  \ifnum#1<1000 \phantom{0}\fi}
  • Awesome! Just for future reference, is there any way to use the S column type to get centered left alignment? – Kevin Keith Sep 19 '16 at 16:38
  • 1
    @KevinKeith: Something like this? – Werner Sep 19 '16 at 16:45
  • yeah, exactly like that – Kevin Keith Sep 19 '16 at 16:47
5

Depending on the full extent of the actual table, using right-aligned \Longstacks for the data may suffice.

Note version [2016-07-28] of the package, which uses \normalbaselineskip, which is impervious to the tabular environment's redefinition of \baselineskip. With an older version of the package, just add \setstackgap{L}{\normalbaselineskip} in the preamble.

\documentclass{article}
\usepackage{booktabs}
\usepackage{textcomp}
\usepackage{stackengine}[2016-07-28]
\begin{document}
\begin{tabular}{ccc}
\toprule
\textnumero & Symbol & Number of Units \\ \midrule
\Longstack[r]{1  2 3  4 5 6 7 8 9}&
\Longstack[c]{A  B C  D E F G H I}&
\Longstack[r]{9 10 7 12 2 3 4 5 5}\\
\bottomrule
\end{tabular}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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