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 have a table which I need to automatically format all numbers to two decimal places. Is this possible? Or do I need to format the entire document?

EDIT:

I have applied the code suggested in an answer but unfortunately it doesn't seem to work for my specific table! Anyone have any ideas on how to adapt it accordingly?

\documentclass{article} \usepackage{siunitx} \begin{document}

\sisetup{
table-number-alignment = center,
table-figures-integer = 1,
table-figures-decimal = 2
}

\begin{table}[thb]

\caption{\label{labelname}Table Caption}

\scriptsize
{\centering \begin{tabular
{lr@{\hspace{0cm}}c@{\hspace{0cm}}rr@{\hspace{0cm}}c@{\hspace{0cm}}r@{\hspace{0.1cm}}cr@{\
space{0cm}}c@{\hspace{0cm}}r@{\hspace{0.1cm}}cr@{\hspace{0cm}}c@{\hspace{0cm}}r@{\hspace{0
1cm}}cr@{\hspace{0cm}}c@{\hspace{0cm}}r@{\hspace{0.1cm}}cr@{\hspace{0cm}}c@{\hspace{0cm}}r
{\hspace{0.1cm}}cr@{\hspace{0cm}}c@{\hspace{0cm}}r@{\hspace{0.1cm}}cr@{\hspace{0cm}}c@{\hs
ace{0cm}}r@{\hspace{0.1cm}}c}

\\
\hline
Dataset & \multicolumn{3}{c}{ConjunctiveRule }& \multicolumn{4}{c}{DecisionTable } &
\multicolumn{4}{c}{DTNB } & \multicolumn{4}{c}{JRip} & \multicolumn{4}{c}{NNge} &
\multicolumn{4}{c}{OneR} & \multicolumn{4}{c}{PART} & \multicolumn{4}{c}{Ridor} \\
\hline
Number correct & 9.5900 & $\pm$ & 1.16 & 9.5600 & $\pm$ & 1.22 &         & 9.5600 & $\pm$ &
1.22 &         & 9.5000 & $\pm$ & 1.24 &         & 8.9500 & $\pm$ & 1.34 &         & 9.6000
& $\pm$ & 1.16 &         & 9.3000 & $\pm$ & 1.34 &         & 9.1700 & $\pm$ & 1.42 &        \\


\end{tabular} \scriptsize \par}
\end{table}
\end{document}
share|improve this question
1  
Please add a minimal working example (MWE) that illustrates your problem that starts from \documentclass and ends with \end{document} –  percusse Sep 2 '12 at 20:10
    
@percusse done! thanks –  redrubia Sep 2 '12 at 20:17
    
I get 143, 9, 1, 6 errors when I compile and fix along the way. Please compile your own example (by copying from the question) to test if it is indeed working. –  percusse Sep 2 '12 at 20:25
    
def works now, just tried it :) –  redrubia Sep 2 '12 at 20:38
add comment

2 Answers

up vote 3 down vote accepted

You have to define each column with decimal figures as an S-columns.

You have 31 columns in your tabular, some with decimal, some with a mathematical sign and some empty. In the empty one, I have put a emdash (to keep track).

Here is a MWE (minimal working example). I used scrartcl with a high DIV-factor and landscape mode to squeeze the tabular in between the margin of a page. You can achieve the same by using the geometry-package with correct parametre:

\documentclass[a4paper,landscape,DIV=20]{scrartcl}
\usepackage{siunitx}
\begin{document}
\sisetup{
  table-number-alignment = center,
  table-figures-integer = 1,
  table-figures-decimal = 2
}
\begin{table}[thb]
\caption{\label{labelname}Table Caption}
\centering\tiny
\begin{tabular}{
    l
    S[table-auto-round]
    c
    S[table-auto-round]
    S[table-auto-round]
    c
    S[table-auto-round]
    l
    S[table-auto-round]
    c
    S[table-auto-round]
    l
    S[table-auto-round]
    c
    S[table-auto-round]
    l
    S[table-auto-round]
    c
    S[table-auto-round]
    l
    S[table-auto-round]
    c
    S[table-auto-round]
    c
    S[table-auto-round]
    c
    S[table-auto-round]
    c
    S[table-auto-round]
    c
    S[table-auto-round]
    l}

Dataset & \multicolumn{3}{c}{ConjunctiveRule }& \multicolumn{4}{c}{DecisionTable } &
\multicolumn{4}{c}{DTNB } & \multicolumn{4}{c}{JRip} & \multicolumn{4}{c}{NNge} &
\multicolumn{4}{c}{OneR} & \multicolumn{4}{c}{PART} & \multicolumn{4}{c}{Ridor} \\

Number correct & 
9.5900 & 
$\pm$ & 
1.16 & 
9.5600 & 
$\pm$ &
1.22 &
--- &
9.5600 &
$\pm$ &
1.22 &
--- & 
9.5000 & 
$\pm$ & 
1.24 &
--- &
8.9500 & 
$\pm$ &
 1.34 &
--- & 
9.6000 & 
$\pm$ & 
1.16 & 
--- & 
9.3000 & 
$\pm$ 
& 1.34 & 
--- & 
9.1700 & 
$\pm$ &
 1.42 &
--- \\ 
\end{tabular}
\end{table}
\end{document}

enter image description here

share|improve this answer
    
Thank you soo much- really appreciate the help! –  redrubia Sep 3 '12 at 10:17
add comment

See page 57 of the siunitx documentation.

A small example:

\documentclass{article}
\usepackage{siunitx}
\begin{document}
\sisetup{
  table-number-alignment = center,
  table-figures-integer = 1,
  table-figures-decimal = 2
}
\begin{tabular}{
    S[table-auto-round]
  }
  2.\\
  2.3\\
  2.34\\
  2.345
\end{tabular}
\end{document}

share|improve this answer
    
Thanks! Will do –  redrubia Sep 2 '12 at 15:36
    
Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. –  canaaerus Sep 2 '12 at 17:15
    
Thanks. I have tried the smaple code on its own and it works well! But unfortunately when I apply it to my own table, it doesn't work. I will now post some code in my question –  redrubia Sep 2 '12 at 19:57
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.