The dcolumn package^{∗}

David Carlisle carlisle@cs.man.ac.uk

1994/03/14

Abstract

This package defines a system for defining columns of entries in an array or tabular which are to be aligned on a ‘decimal point’.

This package defines D to be a column specifier with three arguments. D{⟨sep.tex⟩}{⟨sep.dvi⟩}{⟨decimal places⟩}

⟨sep.tex⟩ should be a single character, this is used as the separator in the .tex file. Thus it will usually be ‘.’ or ‘,’.

⟨sep.dvi⟩ is used as the separator in the output, this may be the same as the first argument, but may be any math-mode expression, such as \cdot.

⟨decimal places⟩ should be the maximum number of decimal places in the column. If this is negative, any number of decimal places can be used in the column, and all entries will be centred on the separator. Note that this can cause a column to be too wide, compare the first two columns in the example below. If this argument is positive, the column uses macros equivalent to \rightdots \endrightdots of array.sty, otherwise the macros are essentially equivalent to

\centerdots \endcenterdots.

You may not want to use all three entries in the array or tabular preamble, so you may define your own preamble specifiers using \newcolumntype.

For example we may say:

\newcolumntype{d}[1]{D{.}{\cdot}{#1}}

d takes a single argument specifying the number of decimal places, and the .tex file should use ., with · being used in the output.

\newcolumntype{.}{D{.}{.}{-1}}

. specifies a column of entries to be centred on the ..

\newcolumntype{,}{D{,}{,}{2}}

, specifies takes a column of entries with at most two decimal places after a ,.

   The following table begins \begin{tabular}{|d{-1}|d{2}|.|,|}

1·2  1·2  1.2  1,2
1·23  1·23 12.5 300,2
1121·2  1121·2 861.20 674,29
184  184  10  69
·4  ·4  ,4

.4

∗This file has version number v1.02, last revised 1994/03/14.

Note that the first column, which had a negative ⟨decimal places⟩ argument is wider than the second column, so that the decimal point appears in the middle of the column.

Also note that this package deals correctly with entries with no decimal part, no integer part, and blank entries.

1 The Macros

1 ⟨∗package⟩

First we load array.sty if it not already loaded.

2 \RequirePackage{array}

The basic ideas behind these macros are explained in the documentation for array.sty. However they use three tricks which may be useful in other contexts.

• The separator is surrounded in extra { }, so that it is set with \mathord spacing, otherwise, for instance a ‘,’ would have extra space after it.

• The separator is not given its special definition by making it active, as this would not work for an entry such as & .5 &, as the first token of an alignment entry is read before the preamble part, incase it is an \omit, in which case the preamble is to be omitted. Instead we switch the mathcode to (hex) 8000, which makes the token act as if it were active.

• Although \mathcode`.="8000 makes . act as if it were active, it is still not allowed in constructions such as \def.{}, even in math-mode, so we have to construct an active version of the separator, this is done by making it the uppercase of ~, and then using the construct

\uppercase{\def~}{⟨definition⟩}.

Note that the ⟨definition⟩ is not uppercased, so the definition can refer to the standard, non-active use of the separator.

3 \def\DC@#1#2#3{%
4 \uccode`\~=`#1\relax
5 \m@th
6 \ifnum #3 < \z@ \expandafter\DC@centre
7 \else \expandafter\DC@right \fi
8 {#1}{#2}{#3}}
9 \def\DC@centre#1#2#3{%
10 \let\DC@end\DC@endcentre
11 \uppercase{\def~}{$\egroup\setbox\tw@=\hbox\bgroup${#2}}%
12 \setbox\tw@=\hbox{${\phantom{{#2}}}$}%
13 \setbox\z@=\hbox\bgroup$\mathcode`#1="8000 }
14 \def\DC@endcentre{$\egroup
15  \ifdim \wd\z@>\wd\tw@
16  \setbox\tw@=\hbox to\wd\z@{\unhbox\tw@\hfill}%
17  \else
18  \setbox\z@=\hbox to\wd\tw@{\hfill\unhbox\z@}\fi
19  \box\z@\box\tw@}
20 \def\DC@right#1#2#3{%
21 \let\DC@end\DC@endright

22 \uppercase{\def~}{$\egroup\setbox\tw@=\hbox to \dimen@\bgroup${#2}}%
23 \setbox\z@=\hbox{$1$}\dimen@=#3\wd\z@
24 \setbox\z@=\hbox{${#2}$}\advance\dimen@\wd\z@
25 \setbox\tw@=\hbox to \dimen@{}%
26 \setbox\z@=\hbox\bgroup$\mathcode`#1="8000 }
27 \def\DC@endright{$\hfil\egroup\hfill\box\z@\box\tw@}

The user interface, define the D column to take three arguments.

28 \newcolumntype{D}[3]{>{\DC@{#1}{#2}{#3}}c<{\DC@end}}

29 ⟨/package⟩