3

This question is related to Alignment of table entries: the case of negative numbers, where the problem is how to align table column entries in the presence of negative numbers and/or statistical significance stars.

In my case I have the same issue, with the additional problem that I have different panels in my table (two in the example below), where each panel contains variables measured with a different scale.

Below there's a simplified working example. In panel A I have 2 decimals preceded by 1 digit, whereas in panel B I have no decimals and 2 or 3 digits.

I know one option is to use \usepackage{dcolumn}, but here I am not sure how to define the custom column type, since the digits vary according to the panel. Thanks for any suggestion.

enter image description here

\documentclass{article}
\usepackage{booktabs, longtable}
\usepackage[left=1.7cm,right=1.7cm,top=1.3cm,bottom=1.3cm]{geometry}
\newcommand{\rowgroup}[1]{\hspace{-2em}#1}  % for indentation

\usepackage{dcolumn}


\begin{document}

\begin{longtable}[c]{ @{\hspace*{\leftmargin}} lcccccc}

    \caption {Example table} \label{tab:table_balance_AvsH} \\ \toprule


    & \multicolumn{3}{c}{Period 1} & \multicolumn{3}{c}{Period 2} \\ \cmidrule(r){2-4} \cmidrule(r){5-7}
    & Group 1 & Group 2 & Difference & Group 1 & Group 2 & Difference \\
    & (1) & (2) & (3) & (4) & (5) & (6) \\  \midrule


    \rowgroup{\textit{Panel A: outcome 1 (USD)}} \\ \addlinespace
     t - 5  & 8.76 & 9.28 & -0.52 & 10.71 & 6.93 & 3.78 \\ 
     t - 4  & 9.07 & 9.17 & -0.10 & 9.07 & 6.93  & 2.14 \\ 
     t - 3  & 9.34 & 8.36 & 0.97  & 9.36 & 7.27  & 2.09 \\ 
     t - 2  & 9.33 & 8.46 & 0.87  & 9.43 & 8.07  & 1.36 \\ 
     t - 1  & 9.40 & 9.07 & 0.33  & 8.87 & 8.33  & 0.53 \\ 

    \addlinespace
    \rowgroup{\textit{Panel B: outcome 2 (Th. USD)}} \\ \addlinespace
     t - 5  & 456  & 650 & -194\mbox{*} & 1,007 & 656   & 352 \\ 
     t - 4  & 490  & 650 & -160\mbox{*} & 1,055 & 619   & 436\mbox{*} \\ 
     t - 3  & -383 & 625 & -1008        & 1,060 & 663   & 398 \\ 
     t - 2  & -467 & 653 & -1120        & 1,120 & 877   & 242 \\ 
     t - 1  & 666  & 746 & -80          & 1,117 & 1,020 & 97  \\ \bottomrule

\end{longtable}

\end{document}
5
  • 2
    You should take a look at the siunitx package.
    – Bernard
    Feb 21, 2017 at 12:47
  • the negative numbers here are shown with hyphens, not minus signs, which require math. (i'm sure this is taken care of by siunitx, as suggested by @Bernard.) Feb 21, 2017 at 13:42
  • Does the environment have to be a longtable, i.e., is there a chance that the real table will span more than 1 page?
    – Mico
    Feb 21, 2017 at 13:49
  • @Mico yes, I left it there for that reason. Feb 21, 2017 at 13:51
  • @Bernard I did, however I get something similar to what posted by Zarko, where the alignment is fine within each panel, but it looks weird across panels. Feb 21, 2017 at 13:59

2 Answers 2

5

enter image description here

\documentclass{article}
\usepackage{siunitx}
\usepackage{booktabs, longtable}
\usepackage[left=1.7cm,right=1.7cm,top=1.3cm,bottom=1.3cm]{geometry}
\newcommand{\rowgroup}[1]{\hspace{-2em}#1}  % for indentation

\sisetup{table-format=-4.0,
        table-space-text-post=*,
        table-align-text-post,
        group-minimum-digits = 4,
        group-separator = {,},
        }

\begin{document}
\begin{longtable}[c]{ @{\hspace*{\leftmargin}} l *{6}{S}}
    \caption {Example table} \label{tab:table_balance_AvsH} \\ \toprule
    & \multicolumn{3}{c}{Period 1} & \multicolumn{3}{c}{Period 2} \\ \cmidrule(r){2-4} \cmidrule(r){5-7}
    & {Group 1} & {Group 2} & {Difference} & {Group 1} & {Group 2} & {Difference} \\
    & {(1)} & {(2)} & {(3)} & {(4)} & {(5)} & {(6)} \\  \midrule
    \rowgroup{\textit{Panel A: outcome 1 (USD)}} \\ \addlinespace
     t - 5  & 8.76 & 9.28 & -0.52 & 10.71 & 6.93 & 3.78 \\
     t - 4  & 9.07 & 9.17 & -0.10 & 9.07 & 6.93  & 2.14 \\
     t - 3  & 9.34 & 8.36 & 0.97  & 9.36 & 7.27  & 2.09 \\
     t - 2  & 9.33 & 8.46 & 0.87  & 9.43 & 8.07  & 1.36 \\
     t - 1  & 9.40 & 9.07 & 0.33  & 8.87 & 8.33  & 0.53 \\
    \addlinespace
    \rowgroup{\textit{Panel B: outcome 2 (Th. USD)}} \\ \addlinespace
     t - 5  &  456 & 650 & -194*        & 1 007 & 656   & 352 \\
     t - 4  &  490 & 650 & -160*        & 1 055 & 619   & 436* \\
     t - 3  & -383 & 625 & -1008        & 1 060 & 663   & 398 \\
     t - 2  & -467 & 653 & -1120        & 1 120 & 877   & 242 \\
     t - 1  &  666 & 746 & -80          & 1 117 & 1020  & 97  \\ \bottomrule
\end{longtable}
\end{document}

Addendum: if you add two zeros to numbers in the second part of table (how looks if you split table, see Mico answer), the table probably looks nicer:

enter image description here

\documentclass{article}
\usepackage{siunitx}
\usepackage{booktabs, longtable}
\usepackage[left=1.7cm,right=1.7cm,top=1.3cm,bottom=1.3cm]{geometry}

\sisetup{table-space-text-post=*,
         table-align-text-post,
         group-minimum-digits = 4,
         group-separator = {,},
        }

\begin{document}
\begin{longtable}[c]{c 
                     S[table-format=-3.2]
                     S[table-format= 3.2]
                     S[table-format= 4.2]
                *{2}{S[table-format=-4.2]}
                     S[table-format=-3.2]
                    }
    \caption {Example table} \label{tab:table_balance_AvsH} \\ \toprule
    & \multicolumn{3}{c}{Period 1} & \multicolumn{3}{c}{Period 2} \\ \cmidrule(r){2-4} \cmidrule(r){5-7}
    & {Group 1} & {Group 2} & {Difference} & {Group 1} & {Group 2} & {Difference} \\
    & {(1)} & {(2)} & {(3)} & {(4)} & {(5)} & {(6)} \\  \midrule
  {\textit{Panel A: outcome 1 (USD)}} \\ \addlinespace
     t - 5  & 8.76 & 9.28 & -0.52 & 10.71 & 6.93 & 3.78 \\
     t - 4  & 9.07 & 9.17 & -0.10 & 9.07 & 6.93  & 2.14 \\
     t - 3  & 9.34 & 8.36 & 0.97  & 9.36 & 7.27  & 2.09 \\
     t - 2  & 9.33 & 8.46 & 0.87  & 9.43 & 8.07  & 1.36 \\
     t - 1  & 9.40 & 9.07 & 0.33  & 8.87 & 8.33  & 0.53 \\
    \addlinespace
{\textit{Panel B: outcome 2 (Th. USD)}} \\ \addlinespace
     t - 5  &  456.00 & 650.00 &  -194.00*  & 1 007.00  & 656.00   & 352.00 \\
     t - 4  &  490.00 & 650.00 &  -160.00*  & 1 055.00  & 619.00   & 436.00* \\
     t - 3  & -383.00 & 625.00 & -1008.00   & 1 060.00  & 663.00   & 398.00 \\
     t - 2  & -467.00 & 653.00 & -1120.00   & 1 120.00  & 877.00   & 242.00 \\
     t - 1  &  666.00 & 746.00 &   -80.00   & 1 117.00  & 1020.00  & 97.00  \\ \bottomrule
\end{longtable}
\end{document}
6
  • thanks, this gets very close to what I had in mind. However, I would like to have digits aligned across panels regardless of decimal points. In the example, the 0 of "9.40" in the first column should be aligned with the 6 of "456" below, and so on. Feb 21, 2017 at 13:55
  • 2
    see, if additions of two zeros to numbers in second part of table make table more beautiful. or split table into two parts with different columns settings.
    – Zarko
    Feb 21, 2017 at 14:05
  • this is acceptable in my case, many thanks for the help. Feb 21, 2017 at 14:16
  • Thanks for the addendum, can I ask the use of "-" in table-format ? Feb 21, 2017 at 21:07
  • 1
    @Stezzo, it tell siunitx that numbers in column can be negative and consequently it should reserved space for minus sign.
    – Zarko
    Feb 21, 2017 at 21:10
2

Here's a solution that embeds two tabularx environments (with equal widths assigned to the six data columns) inside the longtable environment.

A personal comment: The contents of the two "panels" may be sufficiently different to warrant creating two entirely separate table-like environments.

enter image description here

\documentclass{article}
\usepackage{booktabs,tabularx,siunitx,longtable}
\usepackage[hmargin=1.7cm,vmargin=1.3cm]{geometry}
\newcommand{\rowgroup}[1]{\hspace{-2em}#1}  % for indentation
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcommand\mc[1]{\multicolumn{1}{C}{#1}}
\sisetup{group-separator={,} , group-four-digits=true}

\begin{document}
\begin{longtable}{@{}c@{}}
\caption {Example table} \label{tab:table_balance_AvsH}\\
\begin{tabularx}{0.9\textwidth}{ @{\hspace*{\leftmargin}} % first 'tabularx'
    l *{2}{S[table-format=1.2]}
    S[table-format=-1.2] S[table-format=2.2]
    *{2}{S[table-format=1.2]}}
    \toprule
    & \multicolumn{3}{c}{Period 1} & \multicolumn{3}{c}{Period 2} \\ 
    \cmidrule(lr){2-4} \cmidrule(l){5-7}
    & {Group 1} & {Group 2} & {Diff.} & {Group 1} & {Group 2} & {Diff.} \\
    & {(1)} & {(2)} & {(3)} & {(4)} & {(5)} & {(6)} \\  
    \midrule
    \rowgroup{\textit{Panel A: Outcome 1 (USD)\phantom{Th.~}}} 
    & \mc{} & \mc{} & \mc{} & \mc{} & \mc{} & \mc{} \\
    $t - 5$  & 8.76 & 9.28 & -0.52 & 10.71 & 6.93 & 3.78 \\
    $t - 4$  & 9.07 & 9.17 & -0.10 & 9.07 & 6.93  & 2.14 \\
    $t - 3$  & 9.34 & 8.36 & 0.97  & 9.36 & 7.27  & 2.09 \\
    $t - 2$  & 9.33 & 8.46 & 0.87  & 9.43 & 8.07  & 1.36 \\
    $t - 1$  & 9.40 & 9.07 & 0.33  & 8.87 & 8.33  & 0.53 \\
    \addlinespace
\end{tabularx}\\

\begin{tabularx}{0.9\textwidth}{ @{\hspace*{\leftmargin}} % second 'tabularx'
    l S[table-format=-3.0] S[table-format=3.0]
      S[table-format=-4.0,table-space-text-post=\textsuperscript{*}]
      *{2}{S[table-format=4.0]}
      S[table-format=-3.0,table-space-text-post=\textsuperscript{*}]}      
    \rowgroup{\textit{Panel B: Outcome 2 (Th.\ USD)}} 
    & \mc{} & \mc{} & \mc{} & \mc{} & \mc{} & \mc{} \\
    $t - 5$  &  456 & 650 &  -194\textsuperscript{*} & 1007 &  656  & 352 \\
    $t - 4$  &  490 & 650 &  -160\textsuperscript{*} & 1055 &  619  & 436\textsuperscript{*} \\
    $t - 3$  & -383 & 625 & -1008     & 1060 &  663  & 398 \\
    $t - 2$  & -467 & 653 & -1120     & 1120 &  877  & 242 \\
    $t - 1$  &  666 & 746 &   -80     & 1117 & 1020  &  97  \\ 
    \bottomrule
\end{tabularx}
\end{longtable}

\end{document}
4
  • many thanks, and I also agree on the "table splitting" comment. Feb 21, 2017 at 15:07
  • how to include notes in this example? I usually the longtable in a ThreePartTable environment. In my specific example I don't expect the overall table to span in multiple pages, but I still find it useful to use ThreePartTable to have aligned notes. Feb 23, 2017 at 20:48
  • @Stezzo - Please post a new query to ask about how to combine longtable and threeparttable. That way, more people will have a chance to see and (hopefully) provide helpful answers.
    – Mico
    Feb 23, 2017 at 20:51
  • 1
    Here's the separate question based on your answer: Combining ThrePartTable, longtable and tabularx preserving table entries alignment Feb 23, 2017 at 21:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .