0

I am trying to create a table using the code provided here for decimal alignment and the code provided here to have two lines for some headers. Since I'm not sure exactly what each line does, I had trouble applying it to my table. As shown below, the first and second column formats the year intervals differently and the second column is not centered.

Further, could someone explain what \begin{tabular}{l*{4}{d{3.3}} }does?

Any help with fixing these issues, and any other possible improvements you see, would be greatly appreciated.

\documentclass{article}


\usepackage{dcolumn,booktabs}
\newcolumntype{d}[1]{D{.}{.}{#1}}
\newcommand\mc[1]{\multicolumn{1}{c}{#1}} % handy shortcut macro
\newcommand*{\thead}[1]{%
\multicolumn{1}{c}{\bfseries\begin{tabular}{@{}c@{}}#1\end{tabular}}}

\begin{document}

\begin{table}
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{l*{4}{d{3.3}} }
\toprule
\thead{First Year\\Interval}& \thead{Second Year\\Interval} & \thead{Mean Difference} & \thead{95\% Family-Wise\\ Confidence Interval} \\ 
\midrule
2001-05&    1980-85 &0.08&  (-0.46,0.62)\\
2016-20 &1980-85    &-0.21& (-0.76,0.33)\\
2011-15&    2006-10&    0.04    &(-0.06,0.14)\\

\bottomrule
\end{tabular}
\end{table}

\end{document}

enter image description here

2 Answers 2

1

Here is a solution, with some improvements (in particular, loading the caption package yields a more sensible spacing between caption table). Note the d column type is really useful only in the third column.

\documentclass{article}

\usepackage{dcolumn,booktabs, caption}
\newcolumntype{d}[1]{D{.}{.}{#1}}
\newcommand\mc[1]{\multicolumn{1}{c}{#1}} % handy shortcut macro
\newcommand*{\thead}[1]{%
\multicolumn{1}{c}{\begin{tabular}{@{}>{\bfseries}c@{}}#1\end{tabular}}}

\begin{document}

\begin{table}
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{ccd{1.2}>{$}c<{$}}
\toprule
\thead{First Year\\Interval}& \thead{Second Year\\Interval} & \thead{Mean\\ Difference} & \thead{95\% Family-Wise\\ Confidence Interval} \\
\midrule
2001--05& 1980--85 &0.08& (-0.46,0.62)\\
2016--20 &1980--85 &-0.21& (-0.76,0.33)\\
2011--15& 2006--10& 0.04 &(-0.06,0.14)\\
\bottomrule
\end{tabular}
\end{table}

\end{document}

enter image description here

2
  • This looks great, thanks! Could you explain what \begin{tabular}{cc >{\arraybackslash}d{1.2}>{$}c<{$}*{4}{d{3.3}} } does?
    – Remy
    Jul 10, 2019 at 15:40
  • 1
    The first two columns are centred. You actually can remove >{\arraybackslash} – it is used in fixed width columns (such as p{some length}) to stipulate that \\ is used to go to the next row. I was not too sure of the status of the d column type. As to *{4}{d{3.3}}, which you can delete (I forgot to clean the preamble) it means repeating 4 times a column of type d{3.3}, the latter denoting a column centred on the decimal dot, with 3 digits in the integer part, and 3 digits in the decimal part.
    – Bernard
    Jul 10, 2019 at 15:55
1

You should change the preamble l*{4}{d{3.3}} and put ccd{3.3}d{3.3} and you will have the following result.

résultat du code

In ccd{3.3}d{3.3}, the two first c are for the two first columns centered and the two d{3.3} are for the other columns (alignment on the decimal point).

You must log in to answer this question.

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