1

My question is related to this question and also this one. I want to align the values perfectly with respect to the decimal point. I also want to fit the contents with optimal column width so that space is not wasted.

The solution to the current problem will be used to prepare bigger tables (with >10 columns and >20 rows).

Expected output formatting: The current problem is to create a table which is formatted like shown in the image below (made in MS Word), with perfect alignment with respect to the decimal point:

enter image description here

The value in the parentheses represents the t-stat for the parameter estimate.

EDIT: I am able to address the alignment issues with respect to the decimal point. However, I am now facing some other alignment issues shown in figure below: enter image description here

I want to:

  1. reduce the gaps shown by green arrows
  2. eliminate the gaps shown by the red arrows

The MWE is given below:

\documentclass{article}
\usepackage{amsmath,booktabs,multirow,multicol,tabularx,siunitx}
\usepackage[table, dvipsnames]{xcolor}
\sisetup{
    output-exponent-marker = \text{\hspace{1pt}e},
    exponent-product={},
    retain-explicit-plus,
    input-open-uncertainty  = ,
    input-close-uncertainty = ,
    table-align-text-pre    = false,
    table-align-text-post = false,
    round-mode=places,
    round-precision=3,
    table-space-text-pre    = (,
    table-space-text-post   = )
}
\newcommand{\cellvalItal}[1]{\begin{tabular}[l]{S[round-precision=2,math-rm=\mathit]}#1\end{tabular}}
\newcommand{\cellval}[2]{\begin{tabular}[l]{S}#1\\\cellvalItal{#2}\end{tabular}}

\newcommand{\Shifted}[1]{\begin{tabular}[l]{@{\phantom{~~~}}l@{}}#1\end{tabular}}
\newcommand{\ShiftedWithBaseCase}[2]{\begin{tabular}[l]{@{\phantom{~~~}}l}#1 \\ {[}\emph{#2}{]}\end{tabular}}

\begin{document}
\begin{table}
\begin{tabular}{l@{}S@{}S}
\toprule
\multirow{2.3}{*}{\textbf{Parameter}}  
    &   \multicolumn{2}{@{}c}{\textbf{Estimate}} \\
    \cmidrule(lr){2-3}
    & {\centering\textbf{Model~M1}} & {\centering\textbf{Model~M2}}\\
    \cmidrule(lr){1-1}
    \cmidrule(lr){2-2}
    \cmidrule(lr){3-3}
{Constant} & 
    \cellval{-1.316e-2}{(-23.153)} & 
    \cellval{-58.510}{(-4.26)}\\ % User defined functions to shorten the code length
\addlinespace
\textbf{\emph{Category}} & & \\
\ShiftedWithBaseCase{Male members}{Base case: Female members} & \cellval{0.112}{(2.04)}&
{---}\\
\bottomrule
\end{tabular}
\end{table}
\end{document}

Can anybody help?

2
  • Why the recursive nesting of tabulars?
    – Alenanno
    Nov 15, 2020 at 19:41
  • @Alenanno Because the numbers in parentheses are italicized and have a different level of precision, i.e. 2 decimal places. The numbers outside the parentheses have precision up to 3 decimal places. That is what I could think of. Please feel free to suggest if there is a better way to do it.
    – TRK
    Nov 15, 2020 at 19:54

1 Answer 1

1

This is probably not the most elegant solution and maybe needs some more adjustments, but nevertheless, hopefully, closer to the desired output. Relevant changes in comparison to the original example are highlighted using % <--- which is followed by a short explanation of the corresponding change.

enter image description here

\documentclass{article}
\usepackage{amsmath,booktabs,multirow,multicol,tabularx,siunitx}
\usepackage[table, dvipsnames]{xcolor}
\sisetup{
    output-exponent-marker = \text{\hspace{1pt}e},
    exponent-product={},
    retain-explicit-plus,
    input-open-uncertainty  = ,
    input-close-uncertainty = ,
    table-align-text-pre    = false,
    table-align-text-post = false,
    round-mode=places,
    round-precision=3,
    table-space-text-pre    = (,
    table-space-text-post   = )
}
\newcommand{\cellvalItal}[1]{\begin{tabular}[l]{S[round-precision=2,math-rm=\mathit]}#1\end{tabular}}
\newcommand{\cellvalleft}[2]{\begin{tabular}[l]{@{}S[table-format=-2.3e-1]@{}}#1\\\cellvalItal{#2}\end{tabular}} % <--- added @{} to either side of the S column specifier to remove extro horizontal space in the columns
\newcommand{\cellvalright}[2]{\begin{tabular}[l]{@{}S[table-format=-2.3]@{}}#1\\\cellvalItal{#2}\end{tabular}} % <--- added @{} to either side of the S column specifier to remove extro horizontal space in the columns, added appropriate table-format option, needed to be different than the table-format of the second cell, hence the second command.

%\newcommand{\Shifted}[1]{\begin{tabular}[l]{@{\phantom{~~~}}l@{}}#1\end{tabular}}
\newcommand{\ShiftedWithBaseCase}[2]{\begin{tabular}[l]{@{\phantom{~~~}}l}#1 \\ {[}\emph{#2}{]}\end{tabular}}

\renewcommand{\cmidrulekern}{0.25em} % <--- added do decrease space etween adjacent \cmidrule commands

\begin{document}
\begin{table}
\begin{tabular}{@{}l@{}S@{}S@{}} % <---- added @{} here to left align text with the left edge of the table
\toprule
\multirow{2.3}{*}{\textbf{Parameter}}  
    &   \multicolumn{2}{c}{\textbf{Estimate}} \\
    \cmidrule(l){2-3} % <--- removed r here in order to make sure the cmirdlue reaches the right "border" of the table
    & {\textbf{Model~M1}} & {\textbf{Model~M2}}\\ % <--- removed \centering here. Text in {} is by default centered in S type columns.
    \cmidrule(r){1-1} % <--- removed l here to make line left aligned with left endge of table
    \cmidrule(lr){2-2}
    \cmidrule(l){3-3} % <--- removed r here in order to make sure the cmirdlue reaches the right "border" of the table
{Constant} & 
    \cellvalleft{-1.316e-2}{(-23.153)} & 
    \cellvalright{-58.510}{(-4.26)}\\ % User defined functions to shorten the code length
\addlinespace
\textbf{\emph{Category}} & & \\
\ShiftedWithBaseCase{Male members}{Base case: Female members} & \cellvalleft{0.112}{(2.04)}&
{---}\\
\bottomrule
\end{tabular}
\end{table}
\end{document}
1
  • Thanks a lot @leandriis :). It works very well. Your explanations have also provided additional insight. They will be very useful when I make bigger tables with alignment and space constraints.
    – TRK
    Nov 16, 2020 at 18:18

You must log in to answer this question.

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