2

I have a question about Latex tables and dcolumn package and I was hoping you could help me fix my problem. I am using dcolumn package to align my regression results from the decimal point. Everything looks fine but I also have a row that includes string values (Yes, No, etc.). For these strings, I use \multicolumn command to center align them with the rest of the column but, as you see in the minimal code and its output, the code aligns them with the numbers INCLUDING the significance stars and that is why the string values are pushed too much to the right. How can I revise this code so I still have the same format for the numeric values but the Yes, No values are center aligned with only numbers EXCLUDING the significance stars? Thanks for your help!

This is the minimal code:

\documentclass[11pt]{article}

\usepackage{amssymb,amsmath,color,booktabs,dcolumn,caption}
\usepackage[left=1in, right=1in, top=1in, bottom=1in]{geometry}


\newcolumntype{d}[1]{D{.}{.}{#1}}
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}


\begin{document}

\begin{table}[h]\centering\small

\caption{Falsification Tests: Non-Fed-Regulated Firms}

\begin{tabular*}{\textwidth}{@{\hskip\tabcolsep\extracolsep\fill}l*{6}{d{2.6}}}
    \toprule
                    &\multicolumn{1}{c}{(1)}         &\multicolumn{1}{c}{(2)}         &\multicolumn{1}{c}{(3)}         &\multicolumn{1}{c}{(4)}         &\multicolumn{1}{c}{(5)}         &\multicolumn{1}{c}{(6)}         \\
\midrule
Post $\times$ Small Non-Fin.&      -0.026         &      -0.042         &      -0.046         &       0.066         &       0.050         &       0.041         \\
                    &      (0.051)         &      (0.055)         &      (0.047)         &      (0.082)         &      (0.083)         &      (0.071)         \\ \addlinespace

log Assets          &                     &      -0.185\sym{***}&      -0.197\sym{***}&                     &      -0.288\sym{***}&      -0.304\sym{***}\\
                    &                     &      (0.059)         &      (0.041)         &                     &      (0.083)         &      (0.084)         \\

Other Controls      &   \multicolumn{1}{c}{{No}}         &\multicolumn{1}{c}{No}         &\multicolumn{1}{c}{Yes}         &\multicolumn{1}{c}{No}         &\multicolumn{1}{c}{No}         &\multicolumn{1}{c}{Yes}         \\

    \bottomrule
    \end{tabular*}

\end{table}

\end{document}

And this is the output:

Minimal table

  • any news? you receive two answers, which solve your problem, but you not accept anyone ... – Zarko Jan 24 '18 at 22:34
4

I could obtain what you want, with the S column type and the relevant parameters, defined by siunitx, which has more possibilities for fine-tuning. A simplification is that a non-numerical contents in an S column is centred by just enclosing it in a pair of braces. I also simplified your definition of \sym and some parts of your preamble, and used tabularx in the place of tabular*.

\documentclass[11pt]{article}

\usepackage{amssymb, mathtools, xcolor, booktabs, dcolumn, caption, siunitx, tabularx}
\usepackage[margin=1in, showframe]{geometry}

\newcolumntype{d}[1]{D{.}{.}{#1}}
\def\sym#1{\ensuremath{^{\mathrlap{#1}}}}

\begin{document}

\begin{table}[h]\centering\small
\sisetup{table-format = -1.3, table-number-alignment = center, table-space-text-pre = (, table-align-text-pre = false, table-space-text-post = {***}}
\setlength{\tabcolsep}{5pt}
    \begin{tabularx}{\textwidth}{X*{6}{S}}
    \toprule
                        & {(1)} & {(2)} & {(3)} & {(4)} & {(5)} & {(6)} \\
    \midrule
    Post $\times$ Small Non-Fin.& -0.026 & -0.042 & -0.046 & 0.066 & 0.050 & 0.041 \\
                        &{(}0.051{)} & {(}0.055{)} & {(}0.047{) }& {(}0.082{)} & {(}0.083{)} & {(}0.071{)} \\ \addlinespace

    log Assets & & -0.185\sym{***}& -0.197\sym{***}& & -0.288\sym{***}& -0.304\sym{***}\\
                        & & {(}0.059{)} & {(}0.041{)} & &{(}0.083{)} & {(}0.084{)} \\
    Other Controls & {No} & {No} & {Yes} & {No} & {No} & {Yes} \\
    \bottomrule
    \end{tabularx}
\end{table}

\end{document} 

enter image description here

  • +1. Since you're using the S column type for the six data columns, the twelve [12!] \multicolumn{1}{c} "wrappers" aren't needed. Just write {(1)}, {(2)}, ..., {Yes}. – Mico Jan 21 '18 at 2:34
  • Oh! yes, of course. I completely forgot this point. – Bernard Jan 21 '18 at 2:38
  • Don't forget the bottom-most row... :-) – Mico Jan 21 '18 at 3:12
  • Bernard, Thank you for your suggestion. I am aware of S columns and I wish I was more thorough when I posted my question. For some reason, I do not wish to use S column and would like to achieve the desirable output using dcloumn. The reason is that I usually add background colors to some cells when I want to present the tables in Beamer and with S columns it is hard to do it and I get into all kinds of errors. With dcolumn, however, I can play with colors and stuff easily. Any help would be greatly appreciated. – Shahram Jan 22 '18 at 23:51
  • @Bernard, thank you for taking time and replying my post. I wish I was more thorough when I posted my question. I am aware of S column and what it does but for some reason (presentation in Beamer, etc.) I wish to achieve the output using dcolumn instead of S column. Any help would be greatly appreciated. – Shahram Jan 23 '18 at 0:06
3

is this result close to what you like to have?

enter image description here

edit: i done the following changes:

  • increase number of integer digits so, that compensate width of significance stars
  • correct \tabcolsep that table body now fit table width, this also enable to set table fonts to normal size
  • consider Mico comment below and define new command \mc{...} for shorter writing \multicolumn{1}{c}{...}

\documentclass[11pt]{article}
\usepackage{amssymb,amsmath,color,booktabs,dcolumn,caption}
\usepackage[left=1in, right=1in, top=1in, bottom=1in]{geometry}
\newcolumntype{d}[1]{D{.}{.}{#1}}
\newcommand\mc[1]{\multicolumn{1}{c}{#1}}% <---
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}

\begin{document}
    \begin{table}[h]
%\small
\setlength\tabcolsep{0pt}% <---
\caption{Falsification Tests: Non-Fed-Regulated Firms}
\label{tab:falsification-test}
\begin{tabular*}{\textwidth}{@{\hskip4pt\extracolsep\fill}l*{6}{d{4.6}}@{\hskip4pt}}% <---
    \toprule
    & \mc{(1)}  & \mc{(2)}  & \mc{(3)}  &  \mc{(4)} &  \mc{(5)} &  \mc{(6)} \\
\midrule
Post $\times$ Small Non-Fin.
    & -0.026    & -0.042    & -0.046    &  0.066    &  0.050    &  0.041    \\
    & (0.051)   & (0.055)   & (0.047)   & (0.082)   & (0.083)   & (0.071)   \\ 
    \addlinespace
log Assets          
    &   & -0.185\sym{***}   & -0.197\sym{***}   &   &  -0.288\sym{***}  & -0.304\sym{***} \\
    &   & (0.059)           & (0.041)   &           & (0.083)   & (0.084)   \\
Other Controls      
    & \mc{{No}} & \mc{No}   & \mc{Yes}  & \mc{No}   & \mc{No}   & \mc{Yes}  \\
    \bottomrule
\end{tabular*}
    \end{table}
\end{document}
  • 1
    +1. A suggestion: Since there are 12 [!] instances of \multicolumn{1}{c}{...}, it would make sense to define a shortcut macro, such as \newcommand\mc[1]{\multicolumn{1}{c}{#1}}. Separately, \centering is redundant since the tabular* environment occupies the full width of the textblock. (The \small directive isn't needed either, but maybe it's a stylistic preference of the OP...) – Mico Jan 21 '18 at 2:28
  • @Zarko, thank you so much for your reply. This is exactly what I needed. You're a life saver. Do you mind telling me what \setlength\tabcolsep{0pt} does in this code? – Shahram Jan 23 '18 at 0:04
  • And how about \hskip4pt? Is it a new command? I mean, is it always 4pt or could be other numbers too? If so, how should I decide about this value? I guess my question is how did you choose 4pt? – Shahram Jan 23 '18 at 0:28
  • Sorry for multiple questions! You have increased the number of integer digits from 2 to 4. How did you know it should be 4? Thanks! – Shahram Jan 23 '18 at 0:42
  • \hskip4pt is not essential, you can omit them, if you like. they only determine some space on both side of table. number of digits: it is needed four for decimal part of number (3 for numbers, one for )), the same should for integer part, that the text in other cell is approximately centered as you like to have. widths of *** is slightly bigger as width of one digit, but i didn't consider and left them that protrude out of reserved space for number. more accurate needed space you can determine with S columns type from siunitx package. – Zarko Jan 23 '18 at 1:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.