# Table alignment with dcolumn

Here is a MWE of a table I used with dcolumn, where I align by the decimal:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{dcolumn}
\usepackage{tabularx, multirow, booktabs}
\usepackage{amsmath}

\begin{document}

\begin{table}[t]
\centering \footnotesize
\caption{bla bla bla bla bla bla bla bla bla bla bla bla bla bla }
\begin{tabularx}{\linewidth}{l *{8}{D{.}{.}{-2}}}
\toprule
&\multicolumn{2}{c}{Column 1 is here}
&\multicolumn{2}{c}{Column 2 is here}
&\multicolumn{2}{c}{Column 3 is here}
&\multicolumn{2}{c}{Column 4 is here}\\
\cmidrule{2-3} \cmidrule{4-5} \cmidrule{6-7} \cmidrule{8-9}
&a &b
&a &b
&a &b
&a &b \\\midrule \\ [-2ex]
asd    &0.01^{**} &0.01^{***} &0.100^{**} &0.100^{**} &0.100^{**} &0.100^{**} &0.100^{**} &0.10^{**}\\
\bottomrule
\end{tabularx}
\caption*{bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla }
\end{table}

\end{document}


This works in principle, but there is something wrong with the horizontal length of the table:

Is there a way to make the table a bit more compact horizontally? Currently, the spacing between various columns is quite large.

EDIT (3/12) Adding title: Is it possible to add a "title" inside the table, something along the lines of:

    \documentclass{article}
\usepackage{dcolumn}
\usepackage{tabularx, multirow, booktabs}

\usepackage[skip=1ex]{caption}  % new
\usepackage{xparse}             % not needed at recent LaTeX version
\NewExpandableDocumentCommand\mcc{O{1}m}
{\multicolumn{#1}{c}{#2}}

\begin{document}

\begin{table}[ht]
\centering
\footnotesize
\setlength\tabcolsep{0pt}
\caption{bla bla bla bla bla bla bla bla bla bla bla bla bla bla}
\begin{tabular*}{\linewidth}{l @{\extracolsep{\fill}}
*{3}{D{.}{.}{6}}}
\toprule
& \mcc[2]{Column 4 is here}   \\
\cmidrule{2-3}
& \mcc{a}    & \mcc{b}    \\
\midrule
\multicolumn{3}{l}{Panel X: This is my title}\\
asd & 0.01^{**}  & 0.01^{***} \\
\\
\multicolumn{3}{l}{Panel Y: This is my looooooong title}\\
asd & 0.01^{**}  & 0.01^{***} \\
\bottomrule
\end{tabular*}
\caption*{bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla }
\end{table}

\end{document}


The only problem with this I see is that it ruins the alignment.

• tabularx can not work here: there are no X columns (and no linebreaking within the cells) just use a normal tabular Nov 27, 2020 at 12:03
• note you caption is just the text * and the bla text is a paragraph after the caption, is that intended? (there is not a \caption* command by default so it is working like \caption{*} Nov 27, 2020 at 12:06

Like this?

Instead of tabularx I suggest to use tabular* and calculation \tabcolsep leave to macro @{\extracolsep{\fill}}. Also you need to correct D column format.

It seems, that you use the second caption as notes to table. If this is the case, than you need for desired formatting load caption package too:

\documentclass{article}
\usepackage{dcolumn}
\usepackage{tabularx, multirow, booktabs}
\usepackage[skip=1ex]{caption}

\begin{document}
\begin{table}[ht]
\centering
\footnotesize
\setlength\tabcolsep{0pt}
\caption{bla bla bla bla bla bla bla bla bla bla bla bla bla bla}
\begin{tabular*}{\linewidth}{l @{\extracolsep{\fill}}
*{8}{D{.}{.}{6}}}
\toprule
&\multicolumn{2}{c}{Column 1 is here}
&\multicolumn{2}{c}{Column 2 is here}
&\multicolumn{2}{c}{Column 3 is here}
&\multicolumn{2}{c}{Column 4 is here}\\
\cmidrule{2-3} \cmidrule{4-5} \cmidrule{6-7} \cmidrule{8-9}
&a &b
&a &b
&a &b
&a &b \\ \midrule
asd    &0.01^{**} &0.01^{***} &0.100^{**} &0.100^{**} &0.100^{**} &0.100^{**} &0.100^{**} &0.10^{**}\\
\bottomrule
\end{tabular*}
\caption*{bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla }
\end{table}
\end{document}


Edit: or you may prefer to have horizontal centered column headers:

\documentclass{article}
\usepackage{dcolumn}
\usepackage{tabularx, multirow, booktabs}

\usepackage[skip=1ex]{caption}  % new
%\usepackage{xparse}             % not needed at recent LaTeX version
\NewExpandableDocumentCommand\mcc{O{1}m}
{\multicolumn{#1}{c}{#2}}

\begin{document}
\begin{table}[ht]
\centering
\footnotesize
\setlength\tabcolsep{0pt}
\caption{bla bla bla bla bla bla bla bla bla bla bla bla bla bla}
\begin{tabular*}{\linewidth}{l @{\extracolsep{\fill}}
*{8}{D{.}{.}{6}}}
\toprule
& \mcc[2]{Column 1 is here}
& \mcc[2]{Column 2 is here}
& \mcc[2]{Column 3 is here}
& \mcc[2]{Column 4 is here}   \\
\cmidrule{2-3} \cmidrule{4-5} \cmidrule{6-7} \cmidrule{8-9}
& \mcc{a}    & \mcc{b}    & \mcc{a}    & \mcc{b}
& \mcc{a}    & \mcc{b}    & \mcc{a}    & \mcc{b}    \\
\midrule
asd & 0.01^{**}  & 0.01^{***} & 0.100^{**} & 0.100^{**}
& 0.100^{**} & 0.100^{**} & 0.100^{**} & 0.10^{**}  \\
\bottomrule
\end{tabular*}
\caption*{bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla }
\end{table}
\end{document}

• Thanks, this is really neat. Is it possible to add a "title" inside the table? I updated the OP with a working, but properly not "correct" Latex, example. Dec 3, 2020 at 14:26
• @TylerD, you just copy ma answer to your question. It works properly :-). Sorry, but why you like to have caption inside table? That is not a way for tables which are not inside floats environments as is table. At tables long a number of pages, which are not encapsulated in float environment, captions are inside long table, which are designed to has caption above each part of table.I never do this. Dec 3, 2020 at 14:47
• @TylerD, Anyway, if you persist to have caption inside table, you can encapsulate it in \multicolumn cell, for example as: \multicolumn{9}{p{\linewidth}}{\caption{bla bla bla bla bla bla bla bla bla bla bla bla bla bla}}\\ . Result of this is not pleasant. Dec 3, 2020 at 14:47
• Thanks a lot :) I updated the second example in my OP with a screenshot as well. There you can see what I mean by "title". The only issue with the multicolumn is that it screws up the alignment - do you know of a way to fix it? Dec 4, 2020 at 7:44
• @TylerD, insert your "title# in multicolumn: \multicolumn{9}{p{\linewidth}}{your title}. If this is not what you looking for, please ask new question. Dec 4, 2020 at 8:19