[Tex/LaTex] Combine column types defined in dcolumn with tabularx

dcolumntablestabularx

I'm looking for a way to define a column type which "mixes" the 'D' column defined in the dcolumn package and the 'X' column defined by the tabularx package. In other words, I want a column type that aligns cell contents by the decimal point and can be stretched to fit the table width I specify. Of course, it does not necessarily have to mix dcolumn and tabularx. Any thoughts?

Here's some code to make clear what I'm trying to achieve. The first table uses column type D from the dcolumn package, inside a tabular environment. How do I get the table to stretch to the width I specify?table 1

The second table uses column X from tabularx package. The table spreads the way I want, but how do I get cells aligned by the decimal point?table 2

The third table tries to combine the two. The result is obviously not what I want (although it's exactly what I'd expect).table 3

% preamble
\usepackage{array}       
\usepackage{tabularx}
\usepackage{dcolumn} 
  \newcolumntype{d}[1]{D{.}{.}{#1}}    
\usepackage{booktabs}


\begin{document}

% table using D from dcolumn package
\begin{table}

  \centering
  \caption{table using dcolumn}
  \begin{tabular}{l*{3}{d{-2}}}

  \toprule
            &  \multicolumn{1}{c}{col A} & \multicolumn{1}{c}{col B} & \multicolumn{1}{c}{col C} \\
  \cmidrule(lr){2-2} \cmidrule(lr){3-3} \cmidrule(lr){4-4}
  \midrule  
       North &      2,228   &   0.300 &  10.6 \\    
       South &        689.2 &   0.8   &   2.6 \\
  \bottomrule

  \end{tabular}     
\end{table}


% table using X from tabularx
\begin{table}

  \centering
  \caption{table using tabularx}

  \begin{tabularx}{\textwidth}{l*{3}{X}}

  \toprule

            &  \multicolumn{1}{c}{col A} & \multicolumn{1}{c}{col B} & \multicolumn{1}{c}{col C} \\
  \cmidrule(lr){2-2} \cmidrule(lr){3-3} \cmidrule(lr){4-4}

  \midrule  
       North &      2,228   &   0.300 &  10.6 \\    
       South &        689.2 &   0.8   &   2.6 \\     
  \bottomrule

  \end{tabularx}    
\end{table}

% table using D inside tabularx
\begin{table}

  \centering
  \caption{table using dcolumn inside tabularx}
  \begin{tabularx}{\textwidth}{l*{3}{d{-2}}}

  \toprule
            &  \multicolumn{1}{c}{col A} & \multicolumn{1}{c}{col B} & \multicolumn{1}{c}{col C} \\
  \cmidrule(lr){2-2} \cmidrule(lr){3-3} \cmidrule(lr){4-4}
  \midrule  
       North &      2,228   &   0.300 &  10.6 \\    
       South &        689.2 &   0.8   &   2.6 \\
  \bottomrule

  \end{tabularx}    
\end{table}



\end{document}

Best Answer

enter image description here

Never tried it before but....

\documentclass{article}

\usepackage{array}       
\usepackage{tabularx}
\usepackage{dcolumn} 
  \newcolumntype{d}[1]{D{.}{.}{#1}}    
\usepackage{booktabs}


\begin{document}

% table using D from dcolumn package
\begin{table}

  \centering
  \caption{table using dcolumn}
  \begin{tabularx}{\textwidth}{l*{3}{d{-2}}}

  \toprule
            &  \multicolumn{1}{X}{\centering col A} &
 \multicolumn{1}{X}{\centering col B} &
 \multicolumn{1}{X}{\centering col C} \\
 \cmidrule(lr){2-2} \cmidrule(lr){3-3} \cmidrule(lr){4-4}
  \midrule  
       North &      2,228   &   0.300 &  10.6 \\    
       South &        689.2 &   0.8   &   2.6 \\
  \bottomrule

  \end{tabularx}     
\end{table}


\end{document}
Related Question