Graphics, Figures & Tablesaligning numbers in tables

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
dkaplan
Posts: 6
Joined: Tue May 19, 2009 4:18 pm

aligning numbers in tables

Post by dkaplan »

Greetings all,

I'm fairly new to using tables in LaTeX. I am finding it difficult to align columns of numbers (particularly if they have negative signs). I've attached an example which I hope will make clear my issue. Any advice would be greatly appreciated.

Thanks in advance.

David

Code: Select all

\documentclass[12pt]{article}
\newcommand{\mb}[1]{\mbox{\boldmath $\bf#1$}}
\newcommand{\mbi}[1]{\mbox{\mathit{\boldmath $\bf#1$}}}
\setlength{\textwidth}{6.5in}\setlength{\textheight}{9.2in}
\setlength{\oddsidemargin}{-.05in} \setlength{\topmargin}{-.5in}
\usepackage{lscape}
\begin{document}
\pagestyle{empty}

\newpage
\begin{landscape}
\begin{table} [!hbp] \label{table3}
\begin{center} \small
 
\caption{$N_T$ = 1200} 
\bigskip


\begin{tabular}{lcccccccrrrrrrrrrr} 
\hline
 && $ICC_2$   &&  $ICC_2$   &&  $ICC_2$\\\\
 &&0.0 ~~~0.1~~~ 0.2 ~~~0.3 ~~~0.4 && 0.0~~~0.1~~~ 0.2~~~ 0.3 ~~~0.4 && 0.0~~~ 0.1~~~ 0.2 ~~~0.3 ~~~0.4\\
\hline\\

 && 15 W/80 B   &&  20 W/60 B   &&  30 W/ 40 B\\\\

    &&	0 ~~~~-4 ~~~~-9 ~~~~-12 ~~~~-15 &&0 ~~~~-3 ~~~~-7 ~~~~-11 ~~~~-14 && 0 ~~~~-3 ~~~~-8 ~~~~-11~~~~-15\\
   &&  	-3 ~~~~-8 ~~~~-11 ~~~~-13 ~~~~-16 && -2 ~~~~-7 ~~~~-10 ~~~~-13 ~~~~-15 &&-2 ~~~~-7 ~~~~-10 ~~~~-13 ~~~~-15\\
$ICC_1$&& -6 ~~~~-9 ~~~~-11 ~~~~-14 ~~~~-17  && -5 ~~~~-8 ~~~~-11 ~~~~-13 ~~~~-16 && -5 ~~~~-9 ~~~~-11 ~~~~-13 ~~~~-16\\ 
 &&-8 ~~~~-11 ~~~~-13 ~~~~-15 ~~~~-17 && -7 ~~~~-10 ~~~~-12 ~~~~-13 ~~~~-16  && -7 ~~~~-10 ~~~~-12 ~~~~-14 ~~~~-16\\ 
 &&6 ~~~~-10 ~~~~-13 ~~~~-14 ~~~~-16 ~~~~-17 && -9 ~~~~-11 ~~~~-13 ~~~~-14 ~~~~-16 && -9 ~~~~-12 ~~~~-13 ~~~~-14 ~~~~-16\\\\\\

 && 40 W/30 B   &&  60 W/20 B   &&  80 W/ 15 B\\\\
  
    &&  $0$ ~~~{-4} ~~~-8 ~~~-12 ~~~-15 && 0 ~~~-4 ~~~-8 ~~~-12 ~~~-15  && 0 ~~~-5 ~~~-8 ~~~-13 ~~~-15\\   
     &&  $-3$ ~~~{-8} ~~~-10 ~~~-14 ~~~-16  &&-3 ~~~-7 ~~~-11 ~~~-13 ~~~-16  &&-3 ~~~-8 ~~~-11 ~~~-14 ~~~-16\\ 
$ICC_1$&&      $-5$ ~~~{-9} ~~~-11 ~~~-14 ~~~-16 && -5 ~~~-9 ~~~-12 ~~~-14 ~~~-16 && -5 ~~~-10 ~~~-12 ~~~-14 ~~~-17\\ 
 && $-8$ ~~~-11 ~~~-13 ~~~-14 ~~~-16  &&-8 ~~~-11 ~~~-12 ~~~-15 ~~~-17 &&-8 ~~~-11 ~~~-13 ~~~-15 ~~~-16\\ 
&& $-9$ ~~~-12 ~~~-13 ~~~-15 ~~~-16 &&-10 ~~~-12 ~~~-13 ~~~-15 ~~~-17 &&-10 ~~~-12 ~~~-14 ~~~-15 ~~~-17\\\\ 


\hline 
  

 \end{tabular}
\end{center}
\end{table}
\end{landscape}


\newpage
\begin{landscape}
\begin{table} [!hbp] \label{table4}
\begin{center} \small
 
\caption{Class size (.33, .33, .33)} 
\bigskip


\begin{tabular}{lcccccccrrrrrrrrrr} 
\hline
 && $ICC_2$   &&  $ICC_2$   &&  $ICC_2$\\\\
 &&0.0 ~~~0.1~~~ 0.2 ~~~0.3 ~~~0.4 && 0.0~~~0.1~~~ 0.2~~~ 0.3 ~~~0.4 && 0.0~~~ 0.1~~~ 0.2 ~~~0.3 ~~~0.4\\
\hline\\

 && 15 W/80 B   &&  20 W/60 B   &&  30 W/ 40 B\\\\

    &&	0 ~~6 ~~17 ~~24 ~~35 && 0 ~~~4 ~~13 ~~21 ~~32 && 0 ~~4 ~~14 ~~23 ~~35\\
   &&  	4 ~~16 ~~22 ~~29 ~~43  && 3 ~~13 ~~22 ~~29 ~~39 && 3 ~~14 ~~21 ~~30 ~~41\\
$ICC_1$&&  9 ~~19 ~~26 ~~35 ~~49 &&8 ~~19 ~~26 ~~33 ~~44 && 9 ~~20 ~~26 ~~34 ~~45\\ 
 && 16 ~~25 ~~30 ~~41 ~~50 && 14 ~~23 ~~30 ~~37 ~~49 &&15 ~~24 ~~33 ~~39 ~~48\\ 
 && 23 ~~31 ~~35 ~~47 ~~58 && 21 ~~29 ~~35 ~~46 ~~58 && 23 ~~31 ~~38 ~~44 ~~57\\\\\\

 && 40 W/30 B   &&  60 W/20 B   &&  80 W/ 15 B\\\\
  
 &&  0~~ 6 ~~15 ~~23 ~~35&& 0 ~~6 ~~15 ~~25 ~~37 && 0 ~~7 ~~15 ~~27 ~~35\\
&& 4 ~~15 ~~21 ~~32 ~~41 &&4 ~~15 ~~22 ~~28 ~~44 &&4 ~~16 ~~22 ~~32 ~~45\\
 $ICC_1$ &&9 ~~20 ~~26 ~~36 ~~47 &&9 ~~19 ~~28 ~~36 ~~44 &&9 ~~21 ~~27 ~~36 ~~49\\
 &&16 ~~24 ~~33 ~~40 ~~52 &&16 ~~24 ~~31 ~~40 ~~51 &&16 ~~26 ~~32 ~~41 ~~50\\
 &&22 ~~30 ~~37 ~~46 ~~56 && 23 ~~29 ~~36 ~~46 ~~59 && 23 ~~31 ~~35 ~~46 ~~54\\


\hline 

 
 \end{tabular}
\end{center}
\end{table}
\end{landscape}



\end{document}

Recommended reading 2024:

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

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

phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

aligning numbers in tables

Post by phi »

Hi,

use the siunitx package for everything related to numbers or units.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

aligning numbers in tables

Post by localghost »

The most noticeable mistake you do is that you are declaring 1*l+7*c+10*r=18 columns in the head of your table but you only use seven of them. You need to restructure your table completely. The [url=hzttp://ctan.org/pkg/siunitx]siunitx[/url] package suggested by phi will help you.

Code: Select all

\documentclass[11pt,a4paper,english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin1]{fontenc}
\usepackage{babel}
\usepackage{includeheadfoot,margin=2cm]{geometry}
\usepackage[font=small,labelfont=bf,tableposition=top]{caption}
\usepackage{booktabs}
\usepackage{rotating}
\usepackage{siunitx}

\begin{document}
  \begin{sidewaystable}
    \caption{Table caption}\label{tab:icc}
    \centering\scriptsize
    \begin{tabular}{%
      c
      *{5}{S[tabnumalign=centre,tabformat=+2.1]}
      @{\qquad}
      *{5}{S[tabnumalign=centre,tabformat=+2.1]}
      @{\qquad}
      *{5}{S[tabnumalign=centre,tabformat=+2.1]}
    } \toprule
      & \multicolumn{5}{c}{ICC\textsubscript{2}} & \multicolumn{5}{c}{ICC\textsubscript{2}} & \multicolumn{5}{c}{ICC\textsubscript{2}} \\ \addlinespace
      & 0.0 & 0.1 & 0.2 & 0.3 & 0.4 & 0.0 & 0.1 & 0.2 & 0.3 & 0.4 & 0.0 & 0.1 & 0.2 & 0.3 & 0.4 \\ \midrule[0.8pt]
      & \multicolumn{5}{c}{15 W/80 B} & \multicolumn{5}{c}{20 W/60 B} & \multicolumn{5}{c}{30 W/ 40 B} \\ \addlinespace
      & 0 & -4 & -9 & -12 & -15 & 0 & -3 & -7 & -11 & -14 & 0 & -3 & -8 & -11 & -15 \\
      & -3 & -8 & -11 & -13 & -16 & -2 & -7 & -10 & -13 & -15 & -2 & -7 & -10 & -13 & -15\\
      ICC\textsubscript{1} & -6 & -9 & -11 & -14 & -17 & -5 & -8 & -11 & -13 & -16 & -5 & -9 & -11 & -13 & -16 \\
      & -8 & -11 & -13 & -15 & -17 & -7 & -10 & -12 & -13 & -16 & -7 & -10 & -12 & -14 & -16 \\
      & -10 & -13 & -14 & -16 & -17 & -9 & -11 & -13 & -14 & -16 & -9 & -12 & -13 & -14 & -16 \\ \midrule
      & \multicolumn{5}{c}{40 W/30 B} & \multicolumn{5}{c}{60 W/20 B} & \multicolumn{5}{c}{80 W/ 15 B} \\ \addlinespace
      & 0 & -4 & -8 & -12 & -15 & 0 & -4 & -8 & -12 & -15 & 0 & -5 & -8 & -13 & -15 \\
      & -3 & -8 & -10 & -14 & -16 & -3 & -7 & -11 & -13 & -16 & -3 & -8 & -11 & -14 & -16 \\
      ICC\textsubscript{1} & -5 & -9 & -11 & -14 & -16 & -5 & -9 & -12 & -14 & -16 & -5 & -10 & -12 & -14 & -17 \\
      & -8 & -11 & -13 & -14 & -16 & -8 & -11 & -12 & -15 & -17 & -8 & -11 & -13 & -15 & -16 \\
      & -9 & -12 & -13 & -15 & -16 & -10 & -12 & -13 & -15 & -17 & -10 & -12 & -14 & -15 & -17 \\ \bottomrule
    \end{tabular}
  \end{sidewaystable}
\end{document}
However, it seems that columns are a bit too wide.


Best regards
Thorsten
Post Reply