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}