1

Writing

\documentclass{article}
\begin{document}
    \begin{table}
        \begin{tabular}{lr@{}lr@{}l}
            \hline
            & \multicolumn{2}{c}{First column} & \multicolumn{2}{c}{Second column}  \\  
            \hline
            Variable Name & 98 & .1234567 & 1234 & .56  \\
                          & (0 & .6789)   &  (54 & .3)  \\
            \hline
        \end{tabular}
    \end{table}
\end{document}

results in

enter image description here

This is a variant of the code from this answer.

The column captions “First column” and “Second column” are perfectly centred due to \multicolumn{2}{c}{First column} and \multicolumn{2}{c}{Second column}. The numbers, however, are left-justified because of r@{}l but I would like to centre them below the column captions. I tried editing r@{}l but this did not help because it is required to stay this way in order to align all numbers along their decimal points.

I chose this solution because it does not need any packages and the siunitx package does not work on my computer (I don’t know why that is).

So, is there a way to solve this problem without packages (or at least without siunitx)?

  • 2
    It would be simpler to do with siunitx, and the S column type. – Bernard Jul 07 '19 at 13:36
  • 2
    I'd investigate why siunitx doesn't work. Did you do a thorough update to your TeX distribution? What is it? – egreg Jul 07 '19 at 13:42
  • 1
    The dcolumn package might be an alternative. – leandriis Jul 07 '19 at 14:02
  • @Incognito .. You already have 3 questions with no one of them having an accepted from you question (possibly neither upvoted) ... This is possible to make (at least some) users to avoid answering to your next questions, not because they will not get points but because these answers/questions will be possibly useless to future users since they won't really know if the answers there solves the initial problem and they could "google" again for a better luck on their problem – koleygr Jul 08 '19 at 09:42

3 Answers3

2

It works with siunitx

\documentclass{article}
\usepackage{siunitx,booktabs}

\begin{document}

\begin{table} \centering

\begin{tabular}{ @{} l S[table-format=2.7,input-digits=0123456789()] S[table-format=4.2,input-digits=0123456789()] @{} } \toprule & \multicolumn{1}{@{}c}{First column} & \multicolumn{1}{c@{}}{Second column} \
\midrule Variable Name & 98.1234567 & 1234.56 \ & (0.6789) & (54.3) \ \bottomrule \end{tabular}

\end{table}

\end{document}

enter image description here

I'd investigate why siunitx doesn't work.

You might also use dcolumn.

\documentclass{article}
\usepackage{dcolumn,booktabs}

\begin{document}

\begin{table} \centering

\begin{tabular}{l D{.}{.}{2.7} D{.}{.}{4.2} } \toprule & \multicolumn{1}{c}{First column} & \multicolumn{1}{c}{Second column} \
\midrule Variable Name & 98.1234567 & 1234.56 \ & (0.6789) & (54.3) \ \bottomrule \end{tabular}

\end{table}

\end{document}

enter image description here

egreg
  • 1,121,712
0

Take a look at siunitxpackage but this could be out of topic... So, a manual fix could be to ad an \hspace{} command with a distance selected after tests to every element of the first raw you wish to center:

\documentclass{article}
\begin{document}
    \begin{table}
        \begin{tabular}{lr@{}lr@{}l}
            \hline
            & \multicolumn{2}{c}{First column} & \multicolumn{2}{c}{Second column}  \\  
            \hline
            Variable Name & \hspace{0.05cm} 98 & .1234567 & \hspace{0.6cm}1234 & .56  \\
                          & (0 & .6789)   &  (54 & .3)  \\
            \hline
        \end{tabular}
    \end{table}
\end{document}

enter image description here

koleygr
  • 20,105
-1

As other mentioned in answer and comments, why you not use siunitx or dcolumn package, which syntax enable simple writing decimal numbers aligned t decimal points. For example with siunitx where multi column cells in column headers are not needed:

\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}

\begin{document} \begin{table} \sisetup{input-symbols = ()} \centering \begin{tabular}{l S[table-format=2.7] S[table-format=4.2]} \toprule & {First column} & {Second column} \ \midrule Variable Name & 98.123 456 7 & 1234.56 \ & (0.6789) & (54.3) \ \bottomrule \end{tabular} \end{table} \end{document}

enter image description here

Zarko
  • 296,517