27

I want to align two numbers in two different rows the decimal point.

I do see a link for that here: Aligning numbers by decimal points in table columns

However, I don't understand how to incorporate that into my code. What follows is a reduced version of my code.

\documentclass[12pt,english]{article}
\usepackage{longtable}
\usepackage{fullpage}
\usepackage{times}
\usepackage[flushleft]{threeparttable}
\usepackage[font=large,labelfont=bf,tableposition=top,textfont=bf]{caption}
\usepackage{tabularx}
\usepackage{booktabs}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}

\clearpage \newpage
\begin{table}[!ht]
\caption{Table Title}
\def\arraystretch{1.05}
\vspace{-0.2cm}
\begin{threeparttable}
\small
\begin{tabularx}{\textwidth}{l*{10}{C}}
\hline \hline \addlinespace
& (1)  \\  
Variable Name & 0.1234566  \\
& (0.1234566) \\
\hline \hline \addlinespace
 \end{tabularx}
\begin{tablenotes}
\vspace{0.1cm}
\footnotesize{

\item \noindent \hspace{-1.8mm} Notes: 

 \noindent Sources: 
 }
\end{tablenotes}
\end{threeparttable}
\end{table}

\end{document}
J G
  • 2,593

4 Answers4

27

In addition to trying out the dcolumn package mentioned by @DavidCarlisle, you may also want to check out some of the capabilities of the siunitx package, specifically, its S column type. I've simplified your (not exactly minimal) working example to the example code below, in order to focus on the operation of this column type:

\documentclass[12pt]{article}
\usepackage{booktabs,mathptmx,siunitx}
\sisetup{input-symbols = {()},  % do not treat "(" and ")" in any special way
         group-digits  = false} % no grouping of digits
\begin{document}
\begin{table}
  \begin{tabular}{@{}l S[table-format=2.7] S[table-format=4.2] @{}}
    \toprule
    & \multicolumn{1}{c}{(1)} & \multicolumn{1}{c@{}}{(2)}  \\  
    \midrule
    Variable Name & 98.1234567 & 1234.56  \\
    & (0.6789) & (54.3)\\
    \bottomrule
  \end{tabular}
\end{table}
\end{document}

enter image description here

David Carlisle
  • 757,742
Mico
  • 506,678
24

It looks like you don't want tabularx at all, just use a normal tabular with a D column for your numeric columns, perhaps defined by

\newcolumntype{.}{D{.}{.}{-1}}

using the dcolumn package.

for completeness, complete example:

\documentclass[12pt]{article}
\usepackage{booktabs,dcolumn}

\begin{document}
\begin{table}
  \begin{tabular}{@{}l*{2}{D{.}{.}{7}}@{}}
    \toprule
    & (1) & (2)  \\  
    \midrule
    Variable Name & 98.1234567 & 1234.56  \\
    & (0.6789) & (54.3)\\
    \bottomrule
  \end{tabular}
\end{table}
\end{document}

enter image description here

David Carlisle
  • 757,742
  • Thank you very much for getting back to me. Why don't I want tabularx? The rest of my table is designed using that, I think. I may be wrong. I tried changing the syntax from tabularx to tabular and it did not work. – J G Feb 15 '12 at 23:50
  • saying "did not work" isn't very descriptive, Obviously if you switch to tabular (and it looks as if you should) you have to not use tabularx syntax, specifically the {\textwidth} argument and X columns. – David Carlisle Feb 16 '12 at 00:30
  • @DavidCarisle. I don't understand what you are saying. Could you please take my code, and modify it to show what I should insert and where? I wouldn't normally ask for this, but I do not understand a lick of what you are saying. I am very grateful. This might help other people coming to this thread in the future. – J G Feb 16 '12 at 00:50
  • I've already changed your table to use tabular in your other question – David Carlisle Feb 16 '12 at 01:19
  • @DavidCarisle. Does that solution include having the decimals align? Does it include making the numbers centered within the columns? It is also not crystal clear to me what to do. I should substitute those 4 lines with the 4 lines in your minimum working example? I am sorry that I keep on harping about how to implement your suggestions. I just hardly know a thing about lateX so it is difficult for me to incorporate what you are suggesting. – J G Feb 16 '12 at 01:47
  • 1
    Answer updated with example – David Carlisle Feb 16 '12 at 17:54
  • One problem with this solution is that all the entries (e.g. column headings) are pushed to the left of the decimal point also. For example if "(1)" is replaced with "column 1" it will be poorly aligned. Is there a way to fix/improve this? – geordie Feb 05 '14 at 07:06
  • 2
    @geordie if headings want (say) center alignment then as usual you can specify that instead of the default column alignment with \multicolumn{1}{c}{my heading} – David Carlisle Feb 05 '14 at 10:36
15

I thought I'd add my answer because it doesn't make use of any other packages (although I think using dcolumn is the easiest solution). What you can do is break each number column into two columns, one for the abscissa (left of the decimal) and one for the ordinate (right). Then, right align the first column, left align the second, and squeeze the space between them with @{}.

Specifically, this is option 1 in the question you linked.

I think the appropriate minimum solution is

\documentclass{article}
\begin{document}
\begin{table}
\begin{tabular}{lr@{}lr@{}l}
\hline
& \multicolumn{2}{c}{(1)} & \multicolumn{2}{c}{(2)}  \\  
\hline
Variable Name & 98 & .1234567 & 1234 & .56  \\
              & (0 & .6789)   &  (54 & .3)  \\
\hline
\end{tabular}
\end{table}
\end{document}
David Carlisle
  • 757,742
Warrick
  • 681
0

A simple workaround for when you don't have too many entries (or if you don't mind auto-substituting a piece of text into every line) is to pad with \phantom{} (which has the effect of taking up as much space as its argument would have taken, but will not show anything). For example:

\documentclass{article}
\usepackage{booktabs}
\begin{document}
\begin{tabular}{rrr}
& (1) & (2) \\  
\midrule
Variable Name & 98.1234567           & 1234.56 \\
              & (0.6789)\phantom{67} &  (54.3) \\
\bottomrule
\end{tabular}
\end{document}