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 display two sorts of values (say, A and B) in one table. The values are generated in R and I automatically create strings like "A (B)", so all values of type B are put in parentheses and are appended to the values of A. As you can see from the resulting table, the spacing is not quite right, it depends on the number of digits of B:

\documentclass{article}

\usepackage{tabularx}
\usepackage{booktabs}

\begin{document}
\begin{tabular}{rr}
  \toprule
   27.8 ( 2.8) &  38.4 ( 3.1)\\
  126.8 (10.3) & 171.9 (11.2)\\
  \bottomrule
\end{tabular}
\end{document}

I am wondering what good ways are to fix this?

One option would be to check the number of digits of B and add \phantom{0} accordingly, or \ \, also does it. But I would be interested in whether it is possible to align the numbers without these 'hacks'. Ideally, I would like to keep the numbers as they are (for example, to be able to easily use the same table in non-LaTeX documents).

In theory, a solution could be to align the numbers according to two dots (like an alignat environment in comparison to align) . But I'm not sure whether this can be done (with siunitx (?))

This is what I found on this topic, but adding ~ is like the above (thus required adjustments of the table entries) and monospaced fonts ... hmmm... doesn't seem very elegant.

UPDATE

Trial after David's post:

\documentclass{article}

\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{dcolumn}
\usepackage{siunitx}

\begin{document}

With \texttt{dcolumn}:
\par\medskip
\begin{tabular}{rD{.}{.}{-1}}
  \toprule
   27.8 &  38.4 (2.3)\\
  126.8 & 171.9 (29.1)\\
  \bottomrule
\end{tabular}
\par\bigskip
With \texttt{siunitx}:
\par\medskip
\begin{tabular}{S
                S}
  \toprule
   27.8 &  38.4(3)\\
  126.8 & 171.9(1)\\
  \bottomrule
\end{tabular}
\par\bigskip
How it should look like:
\par\medskip
\begin{tabular}{rr}
  \toprule
   27.8 &  38.4 (\ \,2.3)\\
  126.8 & 171.9 (29.1)\\
  \bottomrule
\end{tabular}

\end{document}
share|improve this question
    
Isn’t that horizontal alignment? –  Qrrbrbirlbel May 17 '13 at 0:15
add comment

2 Answers

up vote 5 down vote accepted

add \usepackage{dcolumn} then you can replace r by D{.}{.}{-1} for columns centred on a . (There are other options, see the package documentation). siunitx package S column has similar features.

enter image description here

\documentclass{article}

\usepackage{booktabs}
\usepackage{dcolumn}


\begin{document}


\begin{tabular}{
D{.}{.}{3.2} 
D{.}{.}{3.1} @{\,}
D{.}{.}{3.2} 
 }
  \toprule
   27.8 &  38.4 &(2.3)\\
  126.8 & 171.9 &(29.1)\\
  \bottomrule
\end{tabular}

\end{document}

Or this version which doesn't require the extra &

enter image description here

\documentclass{article}

\usepackage{booktabs}
\usepackage{dcolumn}

\makeatletter
\newcolumntype{E}{>{\DDC}c}
\def\DDC#1(#2){\DC@..{3.2}#1\DC@end\,(\DC@..{2.1}#2\DC@end)}
\makeatother
\begin{document}


\begin{tabular}{
D{.}{.}{3.2} 
E
 }
  \toprule
   27.8 &  38.4 (2.3)\\
  126.8 & 171.9 (29.1)\\
  \bottomrule
\end{tabular}

\end{document}

With Colour:

enter image description here

\documentclass{article}

\usepackage{booktabs}
\usepackage{dcolumn,color}

\makeatletter

\newcolumntype{E}{>{\myDDC}c}
\def\myDDC\ignorespaces{\@ifnextchar!{\color{red}\@myDDC\@gobble}{\@myDDC}}
\def\@myDDC#1(#2){\DC@..{3.2}#1\DC@end\,(\DC@..{2.1}#2\DC@end)}

\newcolumntype{F}{>{\myDC}c<{\DC@end}}
\def\myDC\ignorespaces{\@ifnextchar!{\color{red}\@myDC\@gobble}{\@myDC}}
\def\@myDC{\DC@..{3.2}}

\makeatother
\begin{document}


\begin{tabular}{FE}
  \toprule
   27.8 & !38.4 (2.3)\\
  !126.8 & 171.9 (29.1)\\
  \bottomrule
\end{tabular}

\end{document}
share|improve this answer
    
Hi David, many thanks. It sounds good, but I'm not sure if I use it correctly. Please see the update of my post. I read in the siunitx manual ("uncertainty"), but it seems strange: Once the content of () contains another dot, S seems to get confused: siunitx error: "invalid-token-in-uncertainty" Also, dcolumn seems to align the uncertainties to the left, not to the (second) dots. Do you know how to fix this? –  Marius Hofert May 16 '13 at 22:28
    
@MariusHofert Not sure I understand do you want to separately align on the . in both numbers (if so it would be easiest to add a &) –  David Carlisle May 16 '13 at 22:35
    
Hi David. Indeed, yes. The last version in the above update shows this (but I had to manually insert \ \,). I agree that it would normally be easiest to use double the amount of columns, but this is not really possible with the program that generates the table (all done automatically...). –  Marius Hofert May 16 '13 at 22:40
    
@MariusHofert ah I just posted a version with extra & :-) –  David Carlisle May 16 '13 at 22:51
    
@MariusHofert see update version not requiring the extra & posted –  David Carlisle May 16 '13 at 22:59
show 7 more comments

Have you considered a monospace font for your table?

\documentclass{article}

\usepackage{tabularx}
\usepackage{booktabs}

\begin{document}

\texttt{
\begin{tabular}{rr}
  \toprule
   27.8 ( 2.8) &  38.4 ( 3.1)\\
  126.8 (10.3) & 171.9 (11.2)\\
  \bottomrule
\end{tabular}
}

\end{document}
share|improve this answer
    
Could you please expand it to a complete answer? –  mafp May 16 '13 at 18:37
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.