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.

I am trying to make the following table look nice. With that I want the title to be centered horizontally, and the numbers to be alligned at comma.

Here is how it looks now:

Table

And my code.

\documentclass[11pt,a4paper]{article}       % Alternativet er å bruke {report}, men vi holder oss helst til {article} i labrapportene.
\usepackage{booktabs}  
\usepackage[norsk]{babel}
\usepackage{color}
\usepackage{graphicx} 
\usepackage{siunitx}

\sisetup{
table-number-alignment = right,
}

\begin{document}

\begin{table}[h!tb]
    \centering
        \caption{beregnede og målte verdier av magnetfeltet $B$ på aksen til en kort spole s,f.a. avstanden $x$ fra spolens midtplan.}
        \vspace{0.5cm}                      % Litt ekstra plass for å få det til å se penere ut.
        \label{Tabell2}\resizebox{15cm}{!}{
        \begin{tabular}{@{}*{10}{r@{}}} 
            \toprule                        % Horisontal linje.     &
\multicolumn{1}{p{1cm}}{\centering {\color{white}|}\vspace*{-0.3cm} \newline \hspace*{0.1cm} $x$     \newline ($m$) }                   &    
\multicolumn{1}{p{2cm}}{\centering $a=2R$ \newline \hspace*{0.05cm} Beregnet   \newline \hspace*{0.3cm}  $B_1$       \newline (gauss)}  &   
\multicolumn{1}{p{2cm}}{\centering $a= R$ \newline \hspace*{0.15cm} Beregnet   \newline \hspace*{0.25cm} $B_1$       \newline (gauss)}  &
\multicolumn{1}{p{2cm}}{\centering $a=R/2$\newline \hspace*{0.15cm} Beregnet   \newline \hspace*{0.25cm} $B_1$       \newline (gauss)}  &    
\multicolumn{1}{p{2cm}}{\centering $a=2R$ \newline \hspace*{0.05cm} Målt       \newline \hspace*{0.1cm}  $B_2$       \newline (gauss)}  &
\multicolumn{1}{p{2cm}}{\centering $a=2R$ \newline \hspace*{0.05cm} Differanse \newline \hspace*{0.1cm}  $B_2 - B_1$ \newline (\%}      &
\multicolumn{1}{p{2cm}}{\centering $a= R$ \newline \hspace*{0.05cm} Målt \newline\hspace*{0.1cm}  $B_2$ \newline (prosent)}             &
\multicolumn{1}{p{2cm}}{\centering $a= R$ \newline \hspace*{0.05cm} Differanse \newline\hspace*{0.1cm}  $B_2 - B_1$ \newline (\%)}      & 
\multicolumn{1}{p{2cm}}{\centering $a=R/2$\newline \hspace*{0.05cm} Målt \newline\hspace*{0.1cm}  $B_2$ \newline (prosent)}             &
\multicolumn{1}{p{2cm}}{\centering $a=R/2$\newline \hspace*{0.05cm} Differanse \newline\hspace*{0.1cm}  $B_2 - B_1$ \newline (\%)}       \\
              \midrule                                          
-0,18   &  5,265    &  3,365    &  2,981    &  5,31     &  0,864    &  3,34     & -0,743    &  3,01     &  0,959 \\
-0,16   &  7,725    &  4,667    &  4,057    &  7,77     &  0,584    &  4,60     & -1,439    &  4,08     &  0,559 \\
-0,14   & 11,595    &  6,666    &  5,675    & 11,61     &  0,131    &  6,58     & -1,293    &  5,68     &  0,084 \\
-0,12   & 17,398    &  9,797    &  8,175    & 17,48     &  0,470    &  9,62     & -1,811    &  8,17     & -0,061 \\
-0,10   & 24,796    & 14,694    & 12,114    & 24,71     & -0,345    & 14,35     & -2,338    & 12,04     & -0,607 \\
-0,08   & 30,955    & 21,937    & 18,306    & 30,82     & -0,436    & 21,46     & -2,175    & 18,10     & -1,125 \\
-0,06   & 31,701    & 30,981    & 27,529    & 31,61     & -0,288    & 30,59     & -1,262    & 27,27     & -0,940 \\
-0,04   & 27,387    & 38,714    & 39,223    & 27,42     &  0,121    & 38,68     & -0,088    & 38,99     & -0,595 \\
-0,02   & 22,688    & 41,914    & 49,688    & 22,67     & -0,080    & 42,49     &  1,375    & 49,85     &  0,326 \\
 0,00   & 20,852    & 42,202    & 53,852    & 20,74     & -0,538    & 43,00     &  1,892    & 54,31     &  0,850 \\
 0,02   & 22,688    & 41,914    & 49,688    & 22,49     & -0,873    & 42,46     &  1,303    & 50,04     &  0,708 \\
 0,04   & 27,387    & 38,714    & 39,223    & 27,00     & -1,412    & 39,01     &  0,764    & 39,36     &  0,349 \\
 0,06   & 31,701    & 30,981    & 27,529    & 31,42     & -0,887    & 31,16     &  0,578    & 27,63     &  0,368 \\
 0,08   & 30,955    & 21,937    & 18,306    & 31,00     &  0,145    & 22,06     &  0,560    & 18,41     &  0,568 \\
 0,10   & 24,796    & 14,694    & 12,114    & 25,13     &  1,348    & 14,88     &  1,269    & 12,15     &  0,301 \\
 0,12   & 17,398    &  9,797    &  8,175    & 17,83     &  2,482    &  9,89     &  0,945    &  8,26     &  1,040 \\
 0,14   & 11,595    &  6,666    &  5,675    & 11,91     &  2,718    &  6,77     &  1,558    &  5,74     &  1,141 \\
 0,16   &  7,725    &  4,667    &  4,057    &  8,00     &  3,561    &  4,74     &  1,561    &  4,13     &  1,792 \\
 0,18   &  5,265    &  3,365    &  2,981    &  5,39     &  2,384    &  3,45     &  2,526    &  3,04     &  1,965 \\
            \bottomrule
        \end{tabular}}
\end{table}

\end{document

Yeah, it is not a pretty sight. I think I have tried every single mean I have to make it look good. Like using the siunitx package and so on.

So yeah, any help in making this table look good is greatly appreciated.

My main problem and question is how to make the title be centered above the numbers, and having the numbers align at comma / alligned to the right

share|improve this question
add comment

3 Answers

up vote 11 down vote accepted

I note that you're trying to make the nine data columns all have the same width. That's not straightforward to do if one one also wants to use the S column type that's provided by the siunitx package.

To provide a workable solution, one can start by observing that the word "Differanse" is the longest word in any of the columns; by providing code in the "non-Differanse" columns that forces them to occupy the width of the "Differanse", one can achieve the equal-column-width objective. The MWE below builds on these observations to achieve your objective.

Note that when you're dealing with an S column type, material that's enclosed in curly braces will be centered by default. Hence, if you want to center-typeset some non-numeric material, say "xyz", in the respective S column, it's not necessary to provide a \multicolumn{1}{c}{xyz} wrapper -- all that's needed is {xyz}.

\documentclass[11pt,a4paper]{article}  
\usepackage{booktabs,color,graphicx}
\usepackage[utf8]{inputenc}
\usepackage[norsk]{babel}
\usepackage[labelfont=bf]{caption}

% set margins to whatever specifications are correct...
\usepackage[margin=1in]{geometry} 

\usepackage{siunitx}
\sisetup{ table-number-alignment = right,
          output-decimal-marker = {,} ,
          table-format=2.3}

\setlength\tabcolsep{3pt} % default is 5pt

% set up some auxiliary variables and macros
\newsavebox\diffbox
   \sbox\diffbox{Differanse}
\newlength\diffwidth
   \setlength\diffwidth{\wd\diffbox}
\newsavebox\Berbox
   \savebox{\Berbox}[\diffwidth][c]{\begingroup Beregnet\endgroup}
\newsavebox\Maltbox
   \savebox{\Maltbox}[\diffwidth][c]{\begingroup Målt\endgroup}

% Now the commands to typeset the words "Beregnet" and "Malt" in 
% "boxes" that are as wide as the word "Differanse"
\newcommand{\Beregnet}{\usebox{\Berbox}}
\newcommand{\Malt}{\usebox{\Maltbox}}
\begin{document}

\begin{table}[h!tb]
\caption{Beregnede og målte verdier av magnetfeltet $B$ på aksen til 
  en kort spole s,f.a. avstanden $x$ fra spolens midtplan.}
\label{Tabell2}

\vspace{0.5cm}\noindent  % Litt ekstra plass for å få det til å se penere ut.
\resizebox{\textwidth}{!}{%
\begin{tabular}{S[table-format=2.2] *{9}{S}}
\toprule
& {$a=2R$} & {$a=R$} & {$a=R/2$} 
& {$a=2R$} & {$a=2R$} & {$a=R$} 
& {$a=R$} & {$a=R/2$} & {$a=R/2$}\\
& \Beregnet  & \Beregnet & \Beregnet 
& \Malt & {Differanse} & \Malt 
& {Differanse} & \Malt & {Differanse} \\
{$x$}
& {$B_1$} & {$B_1$} & {$B_1$} 
& {$B_2$} & {$B_2-B_1$} & {$B_2$} 
& {$B_2-B_1$} & {$B_2$} & {$B_2-B_1$} \\
{(m)} % don't set in italics
& {(gauss)} & {(gauss)} & {(prosent)} 
& {(gauss)} & {(\%)} & {(gauss)} 
& {(\%)} & {(prosent)} & {(\%)} \\
\midrule
-0,18 & 5,265 & 3,365 & 2,981 & 5,31 & 0,864 & 3,34 & -0,743 & 3,01 & 0,959 \\
-0,16 & 7,725 & 4,667 & 4,057 & 7,77 & 0,584 & 4,60 & -1,439 & 4,08 & 0,559 \\
-0,14 & 11,595 & 6,666 & 5,675  & 11,61 & 0,131 & 6,58 & -1,293 & 5,68 & 0,084 \\
-0,12 & 17,398 & 9,797 & 8,175 & 17,48 & 0,470 & 9,62 & -1,811 & 8,17 & -0,061 \\
-0,10 & 24,796 & 14,694 & 12,114 & 24,71 & -0,345 & 14,35 & -2,338 & 12,04 & -0,607 \\
-0,08 & 30,955 & 21,937 & 18,306 & 30,82 & -0,436 & 21,46 & -2,175 & 18,10 & -1,125 \\
-0,06 & 31,701 & 30,981 & 27,529 & 31,61 & -0,288 & 30,59 & -1,262 & 27,27 & -0,940 \\
-0,04 & 27,387 & 38,714 & 39,223 & 27,42 & 0,121 & 38,68 & -0,088 & 38,99 & -0,595 \\
-0,02 & 22,688 & 41,914 & 49,688 & 22,67 & -0,080 & 42,49 & 1,375 & 49,85 & 0,326 \\
 0,00 & 20,852 & 42,202 & 53,852 & 20,74 & -0,538 & 43,00 & 1,892 & 54,31 & 0,850 \\
 0,02 & 22,688 & 41,914 & 49,688 & 22,49 & -0,873 & 42,46 & 1,303 & 50,04 & 0,708 \\
 0,04 & 27,387 & 38,714 & 39,223 & 27,00 & -1,412 & 39,01 & 0,764 & 39,36 & 0,349 \\
 0,06 & 31,701 & 30,981 & 27,529 & 31,42 & -0,887 & 31,16 & 0,578 & 27,63 & 0,368 \\
 0,08 & 30,955 & 21,937 & 18,306 & 31,00 & 0,145 & 22,06 & 0,560 & 18,41 & 0,568 \\
 0,10 & 24,796 & 14,694 & 12,114 & 25,13 & 1,348 & 14,88 & 1,269 & 12,15 & 0,301 \\
 0,12 & 17,398 & 9,797 & 8,175 & 17,83 & 2,482 & 9,89 & 0,945 & 8,26 & 1,040 \\
 0,14 & 11,595 & 6,666 & 5,675 & 11,91 & 2,718 & 6,77 & 1,558 & 5,74 & 1,141 \\
 0,16 & 7,725 & 4,667 & 4,057 & 8,00 & 3,561 & 4,74 & 1,561 & 4,13 & 1,792 \\
 0,18 & 5,265 & 3,365 & 2,981 & 5,39 & 2,384 & 3,45 & 2,526 & 3,04 & 1,965 \\
\bottomrule
\end{tabular}} % end of scope of \resizebox command
\end{table}
\end{document} 

enter image description here

share|improve this answer
    
I really like this suggestion, but a short question before accepting this. Is there any work around to avoid using '\setlength\tabcolsep{3pt}' ? This tabe is part of a longer physics repport that contains several tables. Forcing the tablewidth globaly makes the table seem awkward =) –  N3buchadnezzar Apr 13 '12 at 20:49
    
Sorry for the late edit: But I had problems with another very similar table. It is far from being worthy of its own question. Where is my mistake? I thought I did this exactly as yours. Although it is only the last coloumn that is wrong pastebin.com/3jTu4ctj –  N3buchadnezzar Apr 13 '12 at 22:38
    
Regarding your first question: To keep the instruction \setlength\tabcolsep{3pt} local to a specific tabular environment, just remove it from the preamble and reinsert it between the \begin{table} and \begin{tabular} statements; that way, its scope ends when LaTeX encounters the next \end{table} statement. By the way, in your original code you have 10 [!] r@{} column type specifiers; the @{} part removes half of the intercolumn whitespace, hence you'd be down to 2.5pt of whitespace anyway. Not that different from the 3pt whitespace I suggest in my example. –  Mico Apr 14 '12 at 0:12
    
Regarding your second question: For the second tabular environment, you should replace \begin{tabular}{S[table-format=2.2] *{7}{S}} with \begin{tabular}{S[table-format=2.2] *{5}{S} S[table-format=2.9]}. (You have only 7 columns in total.) By the way, I can't help but wonder what the use may be of showing 9 digits after the decimal in the final column given that it's a difference of two quantities whose precision would appear to be no better than 3 digits after the decimal? –  Mico Apr 14 '12 at 0:15
add comment

I had the exact same issue today.

I used \hspace{1ex} where there was a number "missing". Doing so I could use center alignment without any troubles. It works before a number (i.e. 17.23 in a column with numbers like 133.23) and behind the number (i.e. 0.23 and 0.235).

You might want to define a new command like \newcommand{\x}{\hspace{1ex}} for this, because using \hspace{1ex} all the time makes your code difficult to read.

EDIT: A better solution is to use phantom characters like \phantom{0}. Doing so LaTeX adds an invisible character with the same height and width as the character you specify (in this case a zero). \vphantom and \hpantomalso exist. They work the same as \phantom but only in the Vertical and Horizontal direction.

share|improve this answer
1  
Welcome to TeX.sx! –  Kurt Nov 19 '12 at 18:51
add comment

A first approximation is

\documentclass[11pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}  
\usepackage[norsk]{babel}
\usepackage{color}
\usepackage{graphicx} 
\usepackage{siunitx}
\sisetup{output-decimal-marker={,}}

\begin{document}

\begin{table}
\newcommand\my[1]{\multicolumn{1}{c}{#1}} % just to simplify input

\centering

\caption{beregnede og målte verdier av magnetfeltet $B$ på aksen til
en kort spole s, f.a. avstanden $x$ fra spolens midtplan.}\label{Tabell2}

\resizebox{\textwidth}{!}{%
\begin{tabular}{*{10}{S[table-format=3.3]}} 
\toprule
& \my{$a=2R$} & \my{$a=R$} & \my{$a=R/2$} & \my{$a=2R$} & \my{$a=2R$} &
  \my{$a=R$}  & \my{$a=R$} & \my{$a=R/2$} & \my{$a=R/2$} \\
& \my{Beregnet} & \my{Beregnet} & \my{Beregnet} &
\my{Mål} & \my{Differanse} & 
\my{Mål} & \my{Differanse} & 
\my{Mål} & \my{Differanse}\\
\my{$x$} & \my{$B_1$} & \my{$B_1$} & \my{$B_1$} & \my{$B_2$} &
\my{$B_2 - B_1$} & \my{$B_2$} & \my{$B_2 - B_1$} & \my{$B_2$} & \my{$B_2 - B_1$} \\
\my{(\si{m})} &
\my{(\si{gauss})} &   
\my{(\si{gauss})} &
\my{(\si{gauss})} &    
\my{(\si{gauss})} &
\my{(\%)} &
\my{(prosent)} &
\my{(\%)} & 
\my{(prosent)} &
\my{(\%)} \\
\midrule                                          
-0,18   &  5,265    &  3,365    &  2,981    &  5,31     &  0,864    &  3,34     & -0,743    &  3,01     &  0,959 \\
-0,16   &  7,725    &  4,667    &  4,057    &  7,77     &  0,584    &  4,60     & -1,439    &  4,08     &  0,559 \\
-0,14   & 11,595    &  6,666    &  5,675    & 11,61     &  0,131    &  6,58     & -1,293    &  5,68     &  0,084 \\
-0,12   & 17,398    &  9,797    &  8,175    & 17,48     &  0,470    &  9,62     & -1,811    &  8,17     & -0,061 \\
-0,10   & 24,796    & 14,694    & 12,114    & 24,71     & -0,345    & 14,35     & -2,338    & 12,04     & -0,607 \\
-0,08   & 30,955    & 21,937    & 18,306    & 30,82     & -0,436    & 21,46     & -2,175    & 18,10     & -1,125 \\
-0,06   & 31,701    & 30,981    & 27,529    & 31,61     & -0,288    & 30,59     & -1,262    & 27,27     & -0,940 \\
-0,04   & 27,387    & 38,714    & 39,223    & 27,42     &  0,121    & 38,68     & -0,088    & 38,99     & -0,595 \\
-0,02   & 22,688    & 41,914    & 49,688    & 22,67     & -0,080    & 42,49     &  1,375    & 49,85     &  0,326 \\
 0,00   & 20,852    & 42,202    & 53,852    & 20,74     & -0,538    & 43,00     &  1,892    & 54,31     &  0,850 \\
 0,02   & 22,688    & 41,914    & 49,688    & 22,49     & -0,873    & 42,46     &  1,303    & 50,04     &  0,708 \\
 0,04   & 27,387    & 38,714    & 39,223    & 27,00     & -1,412    & 39,01     &  0,764    & 39,36     &  0,349 \\
 0,06   & 31,701    & 30,981    & 27,529    & 31,42     & -0,887    & 31,16     &  0,578    & 27,63     &  0,368 \\
 0,08   & 30,955    & 21,937    & 18,306    & 31,00     &  0,145    & 22,06     &  0,560    & 18,41     &  0,568 \\
 0,10   & 24,796    & 14,694    & 12,114    & 25,13     &  1,348    & 14,88     &  1,269    & 12,15     &  0,301 \\
 0,12   & 17,398    &  9,797    &  8,175    & 17,83     &  2,482    &  9,89     &  0,945    &  8,26     &  1,040 \\
 0,14   & 11,595    &  6,666    &  5,675    & 11,91     &  2,718    &  6,77     &  1,558    &  5,74     &  1,141 \\
 0,16   &  7,725    &  4,667    &  4,057    &  8,00     &  3,561    &  4,74     &  1,561    &  4,13     &  1,792 \\
 0,18   &  5,265    &  3,365    &  2,981    &  5,39     &  2,384    &  3,45     &  2,526    &  3,04     &  1,965 \\
\bottomrule
\end{tabular}%
}
\end{table}

\end{document}

Check the documentation of siunitx for fine tuning the column specifications.

share|improve this answer
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.