Graphics, Figures & TablesHow to align numbers under a centered header?

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
grey
Posts: 5
Joined: Wed Dec 09, 2009 5:53 am

How to align numbers under a centered header?

Post by grey »

This seems like a simple question but I've read/searched a lot and haven't found the answer yet. I would like to display columns of numbers (which should line up) under a header (which should be centered). Here's my problem:

Code: Select all

\begin{table}
\caption{Wrong---numbers in wider column aren't centered}
\begin{tabular}{rr}
\multicolumn{1}{c}{Header} & \multicolumn{1}{c}{Longer Col Header}\\
\midrule
7,287,372 & 375   \\
33        & 8,838 \\
93,131    & 43    \\
\end{tabular}
\end{table}

\begin{table}
\caption{Looks OK but extremely fiddly and error-prone}
\begin{tabular}{rrr}
\multicolumn{1}{c}{Header} & \multicolumn{2}{c}{Longer Col Header}\\
\midrule
7,287,372 & \hspace{3em} 375   & \\
33        & 8,838              & \\
93,131    & 43                 & \\
\end{tabular}
\end{table}
Here's a PDF version of the above:
table4.pdf
compiled version of code
(11.75 KiB) Downloaded 656 times
Any suggestions? I would think this would come up often, but I don't see anything on it, so hopefully I'm just missing something basic. Oh, and I should mention the commas above are thousands separators, not decimal points.

Thanks for any help,
Ben

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

fatra2
Posts: 126
Joined: Fri May 01, 2009 1:43 pm

How to align numbers under a centered header?

Post by fatra2 »

Hi there,

I tried a bit of your code, to see what results could work best. Why don't you mix both solution that you gave.

Code: Select all

\begin{table}
\caption{Wrong---numbers in wider column aren't centered}
\begin{tabular}{rr}
\multicolumn{1}{c}{Header} & \multicolumn{1}{c}{Longer Col Header}\\
\midrule
7,287,372 & 375 \hspace{3em}  \\
33        & 8,838 \hspace{3em} \\
93,131    & 43   \hspace{3em} \\
\end{tabular}
\end{table}
Seems to work for me.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

How to align numbers under a centered header?

Post by localghost »

For this kind of alignment there are the two packages dcolumn and siunitx. But both of them seem to have problems. These are shown in the code below.

Code: Select all

\listfiles
\documentclass[11pt,a4paper,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage[font=small,labelfont=bf,tableposition=top]{caption}
\usepackage[%
  decimalsymbol=fullstop,
  digitsep=comma
]{siunitx}
\usepackage{dcolumn,booktabs}

\newcolumntype{d}[1]{D{.}{.}{#1}}

\begin{document}
  \begin{table}[!ht]
    \caption{Looks ugly with \texttt{dcolumn}}
    \centering
    \begin{tabular}{%
      d{7.0}
      d{4.0}
    }\toprule
      \multicolumn{1}{c}{Header} & \multicolumn{1}{c}{Longer Column Header} \\ \midrule
      7,287,372 & 375   \\
      33        & 8,838 \\
      93,131    & 43    \\ \bottomrule
    \end{tabular}
  \end{table}

  \begin{table}[!ht]
    \caption{Looks OK but seems to reveal a bug in \texttt{siunitx}}
    \centering
    \begin{tabular}{%
      S[tabnumalign=centre,tabformat=7.0]
      S[tabnumalign=centre,tabformat=4.0]
    }\toprule
      {Header} & {Longer Column Header} \\ \midrule
      7287372 & 375  \\
      33      & 8838 \\
      93131   & 43   \\ \bottomrule
    \end{tabular}
  \end{table}
\end{document}
The result with dcolumn looks ugly here and is out of the question because of bad alignment. The siunitx package (v1.3d) seems to reveal a bug in case a number has less than five digits. Perhaps Joseph reads this and has an explanation or can offer a workaround. If not, I'm sure he will fix this very soon and provide a new version.


Best regards and welcome to the board
Thorsten
josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

How to align numbers under a centered header?

Post by josephwright »

localghost wrote: The siunitx package (v1.3d) seems to reveal a bug in case a number has less than five digits. Perhaps Joseph reads this and has an explanation or can offer a workaround. If not, I'm sure he will fix this very soon and provide a new version.
I'm struggling to see what is up here: could you give me a clue!
Joseph Wright
grey
Posts: 5
Joined: Wed Dec 09, 2009 5:53 am

How to align numbers under a centered header?

Post by grey »

josephwright wrote:I'm struggling to see what is up here: could you give me a clue!
By that, do you mean you don't see what is wrong with Thorsten's example? The problem is that, despite specifying digitsep=comma, the "8838" in his second table has no separator.

Thorsten, thank you very much for your examples. I had read about dcolumn before but assumed that I couldn't use it because my numbers had no separators. I didn't realize that if no separator were found it would know the implicit separator was at the end.

I modified your dcolumn table to use "d{9.0}" and "d{5.0}" as the decimal specification and it seems to work. I think the issue was that you have to include the commas when counting the number of digits. It's too bad though that dcolumn (and siunitx apparently) don't work with tabularx.

In case anyone is curious, attached is what my table currently looks like with dcolumn (page 2, I didn't know how to get it on the previous page :-P). It's been taking an incredible amount of work, but in my profession everyone is extremely anal about what tables look like.

Thanks again,
Ben
Attachments
table6.pdf
loss development triangle
(18.55 KiB) Downloaded 426 times
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

How to align numbers under a centered header?

Post by localghost »

grey wrote:
josephwright wrote:I'm struggling to see what is up here: could you give me a clue!
By that, do you mean you don't see what is wrong with Thorsten's example? The problem is that, despite specifying digitsep=comma, the "8838" in his second table has no separator. […]
That's exactly the result I'm getting, too. Output and log file are attached.
grey wrote:[…] I modified your dcolumn table to use "d{9.0}" and "d{5.0}" as the decimal specification and it seems to work. I think the issue was that you have to include the commas when counting the number of digits. It's too bad though that dcolumn (and siunitx apparently) don't work with tabularx. […]
Oh, I see. I didn't take the separators into account, with your settings it works.
grey wrote:[…] In case anyone is curious, attached is what my table currently looks like with dcolumn (page 2, I didn't know how to get it on the previous page :-P). It's been taking an incredible amount of work, but in my profession everyone is extremely anal about what tables look like. […]
I'm interested in the complete code of this very nice table and I would appreciate if you could post it.
Attachments
aligntable.pdf
Resulting output of the given code.
(82.96 KiB) Downloaded 420 times
aligntable.log
The according log with file list.
(11.2 KiB) Downloaded 407 times
grey
Posts: 5
Joined: Wed Dec 09, 2009 5:53 am

How to align numbers under a centered header?

Post by grey »

localghost wrote:I'm interested in the complete code of this very nice table and I would appreciate if you could post it.
Thanks! I made a few additions in the interim to get the code below. BTW, for the TikZ stuff I started off of Stefan Kottwitz's example at http://texblog.net/latex-archive/graphics/tikz-table/.

Code: Select all

\documentclass{article}
\pagestyle{empty}

\usepackage{booktabs}
\usepackage{colortbl}
\usepackage{dcolumn}
\usepackage{multirow}

\usepackage{tabularx}
\newcolumntype{R}{>{\raggedleft\arraybackslash}X} % column right-aligned
\usepackage{rotating}

\usepackage{tikz}
\usetikzlibrary{calc}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\definecolor{brown1}{RGB}{244,226,208}
\definecolor{brown2}{RGB}{244,210,175}
\definecolor{brown3}{RGB}{215,166,118}
\definecolor{brown4}{RGB}{124,108,92}
\definecolor{brown5}{RGB}{88,48,8}

\begin{document}

\newcolumntype{d}[1]{D{\&}{\&}{#1}}

\begin{tikzpicture}
\node (tbl) {
\begin{sideways}
\begin{tabular}{%
    p{17ex}
    d{5.0}
    d{6.0}d{6.0}d{6.0}
    d{6.0}d{6.0}d{6.0}
    d{6.0}d{6.0}d{6.0}
}
\multicolumn{1}{c}{\multirow{2}{9ex}{\centering Accident Year}} &
     \multicolumn{10}{c}{Incurred Loss by Development Age} \\
\cmidrule{2-11}
 & \multicolumn{1}{c}{9} & \multicolumn{1}{c}{21} &
    \multicolumn{1}{c}{33} & \multicolumn{1}{c}{45} &
    \multicolumn{1}{c}{57} & \multicolumn{1}{c}{69} &
    \multicolumn{1}{c}{81} & \multicolumn{1}{c}{93} &
    \multicolumn{1}{c}{105} & \multicolumn{1}{c}{117}\\
\rowcolor{white}
\centering 1981 & 5,012 & 8,269 & 10,907 &  11,805
                & 13,539 & 16,181 & 18,009 & 18,608 & 18,662 & 18,834\\
\rowcolor{white}
\centering 1982 &  106 &  4,285 &  5,396 & 10,666
                & 13,782 & 15,599 & 15,496 & 16,169 & 16,704 &\\
\rowcolor{white}
\centering 1983 & 3,410  & 8,992  & 13,873 & 16,141
                & 18,735 & 22,214 & 22,863 & 23,466  & &\\
\rowcolor{brown1}
\centering 1984 & 5,655  & 11,555 & 15,766 & 21,266
                & 23,425 & 26,083 & 27,067 &        & &\\
\rowcolor{brown1}
\centering 1985 & 1,092  &  9,565 & 15,836 & 22,169
                & 25,955 & 26,180 &        &        & &\\
\rowcolor{brown1}
\centering 1986 & 1,513  & 6,445  & 11,702 & 12,935
                & 15,852 &        &        &        & &\\
\rowcolor{white}
\centering 1987 &  557   &  4,020 & 10,946 & 12,314 & & & & & &\\
\rowcolor{white}
\centering 1988 & 1,351  &  6,947 & 13,112 &        & & & & & &\\  
\rowcolor{white}
\centering 1989 & 3,133  & 5,395  &        &        & & & & & &\\
\rowcolor{brown1}
\centering 1990 & 2,063  &        &        &        & & & & & &\\
\rowcolor{brown1}
& & & & & & & & & &\\
\addlinespace
& \multicolumn{9}{c}{Age-to-Age LDFs} & \\
\cmidrule{2-10}
& \multicolumn{1}{c}{9 $\rightarrow$ 21} &
       \multicolumn{1}{c}{21 $\rightarrow$ 33} &
       \multicolumn{1}{c}{33 $\rightarrow$ 45} &
       \multicolumn{1}{c}{45 $\rightarrow$ 57} &
       \multicolumn{1}{c}{57 $\rightarrow$ 69} &
       \multicolumn{1}{c}{69 $\rightarrow$ 81} &
       \multicolumn{1}{c}{81 $\rightarrow$ 93} &
       \multicolumn{1}{c}{93 $\rightarrow$ 105} &
       \multicolumn{1}{c}{105 $\rightarrow$ 117}\\
\rowcolor{white}\centering
1981 & 1.65  & 1.32 & 1.08 & 1.15 & 1.20 & 1.11 & 1.03 & 1.00 & 1.01&\\
\rowcolor{white}\centering
1982 & 40.42 & 1.26 & 1.98 & 1.29 & 1.13 & 0.99 & 1.04 & 1.03 &     &\\
\rowcolor{white}\centering
1983 &  2.64 & 1.54 & 1.16 & 1.16 & 1.19 & 1.02 & 1.03 &      &     &\\
\rowcolor{brown1}\centering
1984 &  2.04 & 1.36 & 1.35 & 1.10 & 1.11 & 1.03 &      &      &     &\\
\rowcolor{brown1}\centering
1985 &  8.76 & 1.66 & 1.40 & 1.17 & 1.01 &      &      &      &     &\\
\rowcolor{brown1}\centering
1986 &  4.26 & 1.82 & 1.11 & 1.23 &      &      &      &      &     &\\
\rowcolor{white}\centering
1987 &  7.22 & 2.72 & 1.12 &      &      &      &      &      &     &\\
\rowcolor{white}\centering
1988 &  5.14 & 1.89 &      &      &      &      &      &      &     &\\
\rowcolor{white}\centering
1989 &  1.72 &      &      &      &      &      &      &      &     &\\
\addlinespace
& \multicolumn{9}{c}{Averaged Age-to-Age LDFs}\\
\rowcolor{white}
Average &         8.21 & 1.70 & 1.31 & 1.18 & 1.13 & 1.04 & 1.03 &
                  1.02 & 1.01 &\\
\rowcolor{white}
Avg xHi,Lo &      4.54 & 1.60 & 1.23 & 1.18 & 1.14 & 1.03 & 1.03 &
                  1.02 & 1.01 &\\
\rowcolor{white}
Avg Last 5 &      5.42 & 1.89 & 1.23 & 1.19 & 1.13 & 1.04 & 1.03 &
                  1.02 & 1.01 &\\ 
\rowcolor{white}
Weighted Avg &    3.00 & 1.62 & 1.27 & 1.17 & 1.11 & 1.04 & 1.03 &
                  1.02 & 1.01 &\\ 
\rowcolor{white}
Weighted Last 5 & 4.23 & 1.75 & 1.25 & 1.18 & 1.11 & 1.04 & 1.03 &
                  1.02 & 1.01 &\\
\end{tabular}
\end{sideways}};

\begin{pgfonlayer}{background}
\draw[rounded corners,top color=brown3,bottom color=brown2,draw=brown5]
    ($(tbl.north west)+(-0.00,-0.00)$) rectangle
           ($(tbl.south east)-(-0.00,-0.00)$);
\end{pgfonlayer}

\end{tikzpicture}



\end{document}
josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

How to align numbers under a centered header?

Post by josephwright »

grey wrote:
josephwright wrote:I'm struggling to see what is up here: could you give me a clue!
By that, do you mean you don't see what is wrong with Thorsten's example? The problem is that, despite specifying digitsep=comma, the "8838" in his second table has no separator.
This is not a bug, it's what I'd expect. Normally, you don't separate 4 digit numbers. If you want that on, turn on the sepfour option:

Code: Select all

    \listfiles
    \documentclass[11pt,a4paper,english]{article}
    \usepackage[T1]{fontenc}
    \usepackage[utf8]{inputenc}
    \usepackage{babel}
    \usepackage[font=small,labelfont=bf,tableposition=top]{caption}
    \usepackage[%
      decimalsymbol=fullstop,
      digitsep=comma,
      sepfour
    ]{siunitx}
    \usepackage{booktabs}

    \begin{document}

      \begin{table}[!ht]
        \caption{Looks OK}
        \centering
        \begin{tabular}{%
          S[tabnumalign=centre,tabformat=7.0]
          S[tabnumalign=centre,tabformat=4.0]
        }\toprule
          {Header} & {Longer Column Header} \\ \midrule
          7287372 & 375  \\
          33      & 8838 \\
          93131   & 43   \\ \bottomrule
        \end{tabular}
      \end{table}
    \end{document}
grey wrote:It's too bad though that dcolumn (and siunitx apparently) don't work with tabularx.
Could you provide an siunitx example demonstrating the problem.
Joseph Wright
grey
Posts: 5
Joined: Wed Dec 09, 2009 5:53 am

How to align numbers under a centered header?

Post by grey »

josephwright wrote:
grey wrote:It's too bad though that dcolumn (and siunitx apparently) don't work with tabularx.
Could you provide an siunitx example demonstrating the problem.
I just meant that siunitx uses column type "S", while tabularx uses column type "X". There isn't a way for a column to be both. I wrote that after searching the siunitx documentation (which is excellent BTW) for tabularx and not finding anything, and also after reading the sentence that said siunitx and dcolumn use a similar methodology internally (I'm already under the impression that dcolumn and tabularx are incompatible).

In my example table attached above in PDF form, I would like columns 2 and following to all have the same width in addition to having the numbers aligned correctly. This is impossible because each column would have to have both column type S and column type X.

Thank you for pointing out the sepfour option. You may be right about the separator being unnecessary in that case, but in my industry everyone uses Excel so anything that doesn't match Excel formatting will look weird to most people. Anyway, in the future perhaps I will try using siunitx to save time instead of rounding before latex.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

How to align numbers under a centered header?

Post by localghost »

grey wrote:Thanks! I made a few additions in the interim to get the code below. BTW, for the TikZ stuff I started off of Stefan Kottwitz's example […]
Thanks for that. I know Stefan's blog and this specific entry but was not sure if you did it this way.
josephwright wrote:[…] This is not a bug, it's what I'd expect. Normally, you don't separate 4 digit numbers. […]
You are right. My expectations went into the wrong direction. I looked into a document (»typokurz«, German) and according to German typography rules it is allowed to separate four digits [1]. Usually they are not separated but this is not a must. Thanks for the hint to the additional package option.

[1] Downloads « Zvisionwelt
Post Reply