5

After reading some answers on the site, I'm trying to use dcolumn pacakge in order to add horizontal alignment to my tables. Unfortunately without success so far. I'm writing as a part of a group so I shouldn't added packages that may affect others, that's why I'm trying to figure it w/o adding the siunitx package.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{ctable}
\usepackage{tabularx}
\usepackage{graphicx}
\usepackage{dcolumn} 
\newcolumntype{d}[1]{D{.}{.}{#1} }


\begin{document}

\begin{table}[]
    \centering
    \begin{tabular}{@{}lc*{4}{d{1.5}}@{}}
        \toprule
        {} & {} & \multicolumn{2}{c}{Group A} & \multicolumn{2}{c}{Group B} \\
        \cmidrule(lr){3-4}  \cmidrule(l){5-6}
        Types & Character & Results A & Results B     & Results A & Results B     \\
        \midrule
        \multirow{3}{*}{Main}
        {}    & The Good & .111      & .789_{b}^{b}  & .520      & .555_{b}^{a}  \\
        {}    & The Bad  & .111      & .636_{b}^{al} & .520      & .730_{b}^{al} \\
        {}    & The Ugly & .111      & .525_{b}      & .520      & .696_{b}      \\
        \bottomrule
    \end{tabular}
    \caption{Caption}
    \label{tab:my_label}
\end{table}

The result is:

result table

Please help me solve this puzzle, also I need to add bold to some values in the table, and since switching to dcolumn I've removed the $n$ I've had and therefore \mathbf seems not to work any more. What would be the solution in that case?

Oleg
  • 163
  • 2
    Welcome to TeX.SX! I know you stated your reasons for not using siunitx but, imho, you should take a careful look at it, for it is very well crafted and resourceful. Besides, you do load dcolumn don't you? – gusbrs Jan 21 '19 at 16:00
  • @gusbrs Yes, I have added the dcolumn loading. But from my (very narrow) understanding in LaTeX it seems to me as a more basic package, that should be in the basic libraries without any further installation. Personally I work in Overleaf, but some other group members use offline editors (which I know little about) so I'm afraid that adding a more advanced package may cause some issues with them – Oleg Jan 21 '19 at 16:05

2 Answers2

5

You are probably looking for \multicolumn:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{ctable}
\usepackage{tabularx}
\usepackage{graphicx}
\usepackage{dcolumn}
\newcolumntype{d}[1]{D{.}{.}{#1} }


\begin{document}

\begin{table}[]
  \centering
  \begin{tabular}{@{}lc*{4}{d{1.5}}@{}}
    \toprule
    {} & {} & \multicolumn{2}{c}{Group A} & \multicolumn{2}{c}{Group B} \\
    \cmidrule(lr){3-4}  \cmidrule(l){5-6}
    Types & Character & \multicolumn{1}{c}{Results A} & \multicolumn{1}{c}{Results B} & \multicolumn{1}{c}{Results A} & \multicolumn{1}{c}{Results B}  \\
    \midrule
    \multirow{3}{*}{Main}
    {}    & The Good & .111      & .789_{b}^{b}  & .520      & .555_{b}^{a}  \\
    {}    & The Bad  & .111      & .636_{b}^{al} & .520      & .730_{b}^{al} \\
    {}    & The Ugly & .111      & .525_{b}      & .520      & .696_{b}      \\
    \bottomrule
  \end{tabular}
  \caption{Caption}
  \label{tab:my_label}
\end{table}

\end{document}

enter image description here

However, as mentioned in the comments, I think you should reconsider the case against siunitx. More important than being a "basic package" is the fact that it is very well crafted and zealously maintained, besides being widely used. (This is not a judgement on dcolumn of which I'm not an user). You should, of course, decide that along with your work group.

The same table done with siunitx would be something like:

\documentclass{article}

\usepackage{booktabs}
\usepackage{multirow}
\usepackage{siunitx}

\begin{document}

\begin{table}
  \centering
  \sisetup{table-format = 0.3}
  \begin{tabular}{@{}
    lc
    S
    S[table-space-text-post = {$_{b}^{al}$}] % make it the largest post-number element
    S
    S[table-space-text-post = {$_{b}^{al}$}]
    @{}}
    \toprule
          &           & \multicolumn{2}{c}{Group A}     & \multicolumn{2}{c}{Group B}    \\
    \cmidrule(lr){3-4}  \cmidrule(l){5-6}
    Types & Character & {Results A} & {Results B}       & {Results A} & {Results B}      \\
    \midrule
    \multirow{3}{*}{Main}
          & The Good  & .111        & .789{$_{b}^{b}$}  & .520        & .555{$_{b}^{a}$}  \\
          & The Bad   & .111        & .636{$_{b}^{al}$} & .520        & .730{$_{b}^{al}$} \\
          & The Ugly  & .111        & .525{$_{b}$}      & .520        & .696{$_{b}$}      \\
    \bottomrule
  \end{tabular}
  \caption{Caption}
  \label{tab:my_label}
\end{table}

\end{document}

It does automatically take care of the leading zero, emphasized by @Denis in the comments.

enter image description here

gusbrs
  • 13,740
  • Thanks id did solved my problem, can you perhaps add a solution using siunitx ? – Oleg Jan 21 '19 at 16:22
  • btw, sorry that I can't upvote yet.. – Oleg Jan 21 '19 at 16:25
  • @Oleg Also note that the SI manual says that you should always write .11 as 0.11. – Denis Jan 21 '19 at 16:26
  • @Denis will it still appear in the table as .11 ? All my tables are generated by code so I don't have a problem changing the format - but I do need it to be in that form eventually – Oleg Jan 21 '19 at 16:29
  • @Oleg As I understand the SI brochure, we should always write 0.11, including in tables. – Denis Jan 21 '19 at 16:34
  • 1
    @Oleg To be precise, this is page 44 of the SI brochure (French Edition, which is the official edition). – Denis Jan 21 '19 at 16:37
  • 1
    @Oleg See edit for an siunitx equivalent. – gusbrs Jan 21 '19 at 16:45
4

You can (should, in my opinion) use siunitx; here I show also how to have real note markers upright that leave the exact space for them.

If you want to set some of the entries bold, there is a trick adapted from https://tex.stackexchange.com/a/352028/4427

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{siunitx}

\NewDocumentCommand{\bs}{}{\fontseries{b}\selectfont}

\makeatletter
\NewDocumentCommand{\tss}{mm}{%
  {\m@th\ensuremath{%
    ^{\mbox{\fontsize\sf@size\z@\selectfont #1}}%
    _{\mbox{\fontsize\sf@size\z@\selectfont #2}}%
  }}%
}
\makeatother

\begin{document}

\begin{table}[htp]
\centering

\sisetup{detect-weight,mode=text}
\DeclareDocumentCommand{\bfseries}{}{\bs}

\begin{tabular}{
  @{}
  l
  c
  S[table-format=1.3]
  S[table-format=1.3,table-space-text-post=\tss{al}{b}]
  S[table-format=1.3]
  S[table-format=1.3,table-space-text-post=\tss{al}{b}]
  @{}
}
\toprule
 & & \multicolumn{2}{c}{Group A} & \multicolumn{2}{c}{Group B} \\
\cmidrule(lr){3-4}  \cmidrule(l){5-6}
Types & Character & {Results A} & {Results B} & {Results A} & {Results B} \\
\midrule
Main & The Good & \bs .111 & .789\tss{b}{b}  & .520 & .555\tss{a}{b}  \\
     & The Bad  &     .111 & .636\tss{al}{b} & .520 & .730\tss{al}{b} \\
     & The Ugly &     .111 & .525\tss{}{b}   & .520 & .696\tss{}{b}   \\
\bottomrule
\end{tabular}

\caption{Caption}
\label{tab:my_label}

\end{table}

\end{document}

I wouldn't use \multirow: blank space below “Main” means repetition. Shifting it down makes quite unclear whether “Main” refers to all rows or just the middle one.

enter image description here

egreg
  • 1,121,712
  • Although it doesn't directly answer my question, but it's also a great answer. Thank you. Just one question, I've noticed that the values in the table don't appear to be in math mode, i.e not italic as it appears in the example above - can it be changed? – Oleg Jan 21 '19 at 17:31
  • @Oleg The sub/superscript seem better go in upright type. Of course, I don't know what they refer to, so your mileage may vary. – egreg Jan 21 '19 at 17:57
  • In the original file they stand for statistical significance, is this the line that controls it ? S[table-format=1.3,table-space-text-post=\tss{al}{b}] – Oleg Jan 21 '19 at 18:13
  • 1
    @Oleg No, it's the definition of \tss (short for \textsupersubscript). If you want them to be in italics, change both occurrences of \selectfont into \itshape. – egreg Jan 21 '19 at 18:18