10

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}
  • Can you provide a minimum working example (MWE) of the type of table you are constructing, which might help guide the most efficient way to attack the problem? – Steven B. Segletes Oct 31 '13 at 17:41
  • @StevenB.Segletes Am working on it, might take a few minutes to upload it. – djas Oct 31 '13 at 17:56
13

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}
  • I knew that if anyone could do it, you would be the one! :-) – Mico Oct 31 '13 at 19:38
  • @David So the trick here is to ensure you have at least (and perhaps at most) one \multicolumn{}{X}{}, right? – djas Oct 31 '13 at 19:47
  • @djas seems so:-) – David Carlisle Oct 31 '13 at 19:55
6

I don't see how one might combine the decimal-aligning capabilities of the dcolumn package with the width-setting capabilities of the tabularx package. (However, David Carlisle might just decide to show how this might be done after all. Update: Sure enough, he did!)

You could, though, use the tabular* environment (part of core-LaTeX, no extra package required) along with the dcolumn package. Unlike the tabular environment, which takes one argument (the set of column specifications), tabular* takes two arguments: the overall width, and the set of column specifications. The big difference between tabular* and tabularx is that whereas the latter works by expanding by column widths suitably, the former works by adjusting the amount of intercolumn whitespace.

The "trick", such as it is, is to use the construct @{\extracolsep{\fill}} to expand the intercolumn white space so that the tabular* environment takes up the full width specified in the environment's first argument. In the example below, which is largely adapted from your first example, columns 2, 3, and 4 all have the same widths because of the dcolumn specifier d{6.3}; this can be verified by comparing the lengths of the three \cmidrules. (Aside: because the table features plenty of intercolumn whitespace, it's not necessary to trim the \cmidrules to assure some separation between them.) The columns are separated by equal amounts of intercolumn whitespace, courtesy of the @{\extracolsep{\fill}} directive.

I hope this is, more or less, what you're looking for.

enter image description here

\documentclass{article}
\usepackage{dcolumn} 
\newcolumntype{d}[1]{D{.}{.}{#1}}    
\usepackage{booktabs}
\newcommand{\mc}[1]{\multicolumn{1}{c}{#1}} % handy shortcut macro
\begin{document}
\noindent
The width of the text block is indicated by this horizontal rule:
\hrule

\begin{table}[h] % I'm the "h" location specifier just to assure that the table is typeset after the \hrule...
\caption{Table using dcolumn and tabular*}
\begin{tabular*}{\textwidth}{l@{\extracolsep{\fill}}*{3}{d{6.3}}}
\toprule
&  \mc{col A} & \mc{col B} & \mc{col C} \\ 
\cmidrule{2-2} \cmidrule{3-3} \cmidrule{4-4}
\midrule  
North &      2,228   &   0.300 &  10.6 \\    
South &        689.2 &   0.8   &   2.6 \\
\bottomrule
\end{tabular*}     
\end{table}
\end{document}
  • Your output looks pretty much what I had in mind, yes. However, tabularx and tabular*do give different results in general. Do you think in this particular case, where column type is d this won't happen? I remember not going with tabular*because of some examples I saw. – djas Oct 31 '13 at 20:00
  • 1
    @djas - I'm pretty sure that it's helpful, in the tabular* example, to specify the same column parameters (d{6.3}) for all three numerical columns. Comparing the outputs of the examples David and I came up with, what strikes me is that there's a lot of empty whitespace to the right of the righthand-most column when using the tabularx method, whereas the issue isn't as pronounced when using the tabular* method. The fact that the three intercolumn whitespaces are the same under the tabular* method, whereas that's not the case under the tabularx method, may also be worth contemplating. – Mico Oct 31 '13 at 20:05

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.