[Tex/LaTex] Combining ThreePartTable, longtable and tabularx preserving table entries alignment

longtablesiunitxtabularxthreeparttable

This question is a follow up of Alignment of table entries (part 2): negative numbers and table panels with different units of measurement, from which I use the code of one of the answers to create the following working example.

My aim is to use ThreePartTable to allow for the possiblity that the table spans across pages and use the flexible TableNotes environemnt.
The example below additionally uses tabularx to allow for flexible panels, each with different table-format settings from the siunitx package.
So if possible I would like to keep using this solution.

Normally, I would "embed" longtable into a ThreePartTable environment, define appropriate \endfirsthead, \endhead, \endfoot and \endlastfoot and include notes with \insertTableNotes. In this case I tried to create these breakpoints in the table, but with no success due to the two tabularx environments.
Thanks for any suggestions.

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}

Best Answer

You can use two consecutive tabularx environments, which can break across pages thanks to the ltablex package, which brings the functionalities of longtable to tabularx. These environments are nested in a ThreePartTable environment:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{booktabs, ltablex, siunitx, threeparttablex, caption}
\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}
\keepXColumns
\begin{document}


\vspace*{15cm}
\begin{ThreePartTable}
\begin{TableNotes}
\item[a] \label{tn:a}Lalala…
\end{TableNotes}
\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]}}
\caption{Example table} \label{tab:table_balance_AvsH}\\
    \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
\endfirsthead
    \midrule
    & \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
\endhead
\midrule\\
\endfoot
\bottomrule
\insertTableNotes
\endlastfoot
    \rowgroup{\textit{Panel A: Outcome 1 (USD)\phantom{Th.~}}}
    & \mc{} & \mc{} & \mc{} & \mc{} & \mc{} & \mc{} \\
    $t - 5$\tnote{a} & 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 \\
    & \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 \\
    & \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 \\
    & \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
\\
\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{} \\
\endhead
\midrule\\
\endfoot
\bottomrule
\insertTableNotes
\endlastfoot
    $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 \\
\end{tabularx}
\end{ThreePartTable}

\end{document} 

enter image description here enter image description here

Related Question