1 % \iffalse meta-comment
5 % The LaTeX3 Project and any individual authors listed elsewhere
8 % This file is part of the Standard LaTeX `Tools Bundle'.
9 % -------------------------------------------------------
11 % It may be distributed and/or modified under the
12 % conditions of the LaTeX Project Public License, either version 1.3c
13 % of this license or (at your option) any later version.
14 % The latest version of this license is in
15 % http://www.latex-project.org/lppl.txt
16 % and version 1.3c or later is part of all distributions of LaTeX
17 % version 2005/12/01 or later.
19 % The list of all files belonging to the LaTeX `Tools Bundle' is
20 % given in the file `manifest.txt'.
24 %% File: dcolumn.dtx Copyright (C) 1992-1996 1999-2001 David Carlisle
27 \ProvidesFile{dcolumn.dtx}
29 %<package>\NeedsTeXFormat{LaTeX2e}
30 %<package>\ProvidesPackage{dcolumn}
31 %<driver>\ProvidesFile{dcolumn.drv}
33 % \ProvidesFile{dcolumn.dtx}
34 [2014/10/28 v1.06 decimal alignment package (DPC)]
38 \documentclass{ltxdoc}
42 \DocInput{dcolumn.dtx}
47 % \GetFileInfo{dcolumn.dtx}
49 % \title{The \textsf{dcolumn} package\thanks{This file
50 % has version number \fileversion, last
51 % revised \filedate.}}
52 % \author{David Carlisle}
54 % \author{David Carlisle}
55 % \MaintainedByLaTeXTeam{tools}
59 % \changes{v1.00}{1992/02/17}{Initial version}
60 % \changes{v1.01}{1992/06/12}{Re-issue for the new doc and docstrip.}
61 % \changes{v1.02}{1994/03/14}{Re-issue for LaTeX2e}
62 % \changes{v1.03}{1996/02/28}{New feature, after tools/2093}
63 % \changes{v1.05}{1999/07/05}
64 % {Minor doc changes latex/3058}
65 % \changes{v1.06}{2001/05/28}
66 % {More doc changes (suggested by BNB, done by CAR) latex/3315}
71 % This package defines a system for defining columns of entries in an
72 % \texttt{array} or \texttt{tabular} which are to be aligned on a
78 % This package defines {\tt D} to be a column specifier with three
80 % "D{"\meta{sep.tex}"}{"\meta{sep.dvi}"}{"\meta{decimal
83 % \meta{sep.tex} should be a single character, this is used as the
84 % separator in the {\tt .tex} file. Thus it will usually be `{\tt.}' or
87 % \mbox{\meta{sep.dvi}} is used as the separator in the output, this may
88 % be the same as the first argument, but may be any math-mode
89 % expression, such as "\cdot". It should be noted that \texttt{dcolumn}
90 % always uses math mode for the digits as well as the separator.
92 % \meta{decimal places} should be the maximum number of decimal places
93 % in the column. If this is negative, any number of decimal places can
94 % be used in the column, and all entries will be centred on
95 % (the leading edge of) the
96 % separator. Note that this can cause a column to be too wide, compare
97 % the first two columns in the example below. If this argument is
98 % positive, the column uses macros equivalent to "\rightdots"
99 % "\endrightdots" of {\tt array.sty}, otherwise the macros are
100 % essentially equivalent to "\centerdots" "\endcenterdots".
102 % You may not want to use all three entries in the {\tt array} or {\tt
103 % tabular} preamble, so you may define your own preamble specifiers
104 % using "\newcolumntype".
106 % For example we may say:
108 % \noindent"\newcolumntype{d}[1]{D{.}{\cdot}{#1}}"
110 % {\tt d} takes a single argument specifying the number of decimal
111 % places, and the {\tt .tex} file should use {\tt.}, with $\cdot$ being
112 % used in the output.
114 % \noindent"\newcolumntype{.}{D{.}{.}{-1}}"
116 % {\tt .} specifies a column of entries to be centred on the~$.$.
118 % \noindent"\newcolumntype{,}{D{,}{,}{2}}"
120 % {\tt ,} specifies takes a column of entries with at most two decimal
121 % places after a~$,$.
123 % \newcolumntype{d}[1]{D{.}{\cdot}{#1}}
124 % \newcolumntype{.}{D{.}{.}{-1}}
125 % \newcolumntype{,}{D{,}{,}{2}}
127 % The following table begins "\begin{tabular}{|d{-1}|d{2}|.|,|}"
130 % \begin{tabular}{|d{-1}|d{2}|.|,|}
131 % 1.2 & 1.2 &1.2 &1,2 \\
132 % 1.23 & 1.23 &12.5 &300,2 \\
133 % 1121.2& 1121.2&861.20 &674,29 \\
134 % 184 & 184 &10 &69 \\
140 % Note that the first column, which had a negative \meta{decimal places}
141 % argument is wider than the second column, so that the decimal point
142 % appears in the middle of the column.
143 % Also note that this package deals correctly with entries with no
144 % decimal part, no integer part, and blank entries.
146 % If you have table headings (inserted with "\multicolumn{1}{c}{..}"
147 % to over-ride the "D" column type) then it may be that neither of the
148 % above `centred' or `right aligned' forms is quite what you want.
149 % \begin{center}\small
150 % \begin{tabular}[t]{|D..{-1}|D..{1}|D..{5.1}|}
151 %\multicolumn{1}{|c|}{head}&
152 %\multicolumn{1}{c|}{head}&
153 %\multicolumn{1}{c|}{head}\\[3pt]
155 % 11212.2& 11212.2&11212.2 \\
159 % \begin{tabular}[t]{|D..{-1}|D..{1}|D..{1.1}|}
160 %\multicolumn{1}{|c|}{wide heading}&
161 %\multicolumn{1}{c|}{wide heading}&
162 %\multicolumn{1}{c|}{wide heading}\\[3pt]
168 % In both of these tables the first column is set with "D{.}{.}{-1}"
169 % to produce a column centered on the ".", and the second column is
170 % set with "D{.}{.}{1}" to produce a right aligned column.
172 % The centered column produces columns that are wider than necessary
173 % to fit in the numbers under a heading as it has to ensure that the
174 % decimal point is centred. The right aligned column two does not have
175 % this drawback, but under a wide heading a column of small right
176 % aligned figures looks a bit odd.
178 % In version v1.03 a third possibility is introduced. The third
179 % \meta{decimal places} argument may specify \emph{both} the number of
180 % digits to the left and to the right of the decimal place. The third
181 % column in the first table above is set with "D{.}{.}{5.1}" and in the
182 % second table, "D{.}{.}{1.1}", to specify
183 % `five places to the left and one to the right' and `one place to the
184 % left and% one to the right' respectively. (You may use `,' or other
185 % tokens, not necessarily `.' in this argument.) The column of figures
186 % is then positioned such that a number with the specified numbers of
187 % digits is centred in the column.
189 % This notation also enables columns that are centred on the mid-point
190 % of the separator, rather than its leading edge; for example
191 % "D{+}{\,\pm\,}{3,3}" will give nice, symmetric layout of up to three
192 % digits on either side of a $\pm$ sign.
197 % \section{The Macros}
203 % First we load {\tt array.sty} if it not already loaded.
205 \RequirePackage{array}
208 % The basic ideas behind these macros are explained in the documentation
209 % for {\tt array.sty}. However they use three
210 % tricks which may be useful in other contexts.
212 % \item The separator is surrounded in extra "{ }", so that it is set
213 % with "\mathord" spacing, otherwise, for instance a `,' would have
214 % extra space after it.
215 % \item The separator is not given its special definition by making it
216 % active, as this would not work for an entry such as "& .5 &", as the
217 % first token of an alignment entry is read {\em before\/} the preamble
218 % part, in case it is an "\omit", in which case the preamble is to be
219 % omitted. Instead we switch the mathcode to (hex) 8000, which makes the
220 % token act as if it were active.
221 % \item Although \verb|\mathcode`.="8000| makes {\tt.} act as if it
222 % were active, it is still not allowed in constructions such as
223 % "\def.{}", even in math-mode, so we have to construct an active
224 % version of the separator, this is done by making it the uppercase of
225 % "~", and then using the construct\\
226 % "\uppercase{\def~}{"\meta{definition}"}".\\
227 % Note that the \meta{definition} is not uppercased, so the definition
228 % can refer to the standard, non-active use of the separator.
231 % \begin{macro}{\DC@}
232 % \changes{v1.03}{1996/02/28}{New feature, after tools/2093}
233 % Set up uppercase tables as required, and then grab the first part of
234 % the numerical argument into "\count@".
239 \afterassignment\DC@x\count@#3\relax{#1}{#2}}
243 % \begin{macro}{\DC@x}
244 % \changes{v1.03}{1996/02/28}{Macro added}
245 % If "\count@" is negative, centre on the decimal point. If it is
246 % positive either "#1" will be empty in which case bad out decimal
247 % part to the number of digits specified by "\count@" or (new feature
248 % in v1.03) it is none empty in which case "\count@" contains the
249 % number of digits to the left of the point, and "#1" contains a junk
250 % token (probably ".") followed by the number of digits to the right
251 % of the point. In either of these latter cases, "\DC@right" is used.
253 \def\DC@x#1\relax#2#3{%
255 \expandafter\DC@centre
257 \expandafter\DC@right
263 % \begin{macro}{\DC@centre}
264 % If centering on the decimal point, just need to box up the two halves.
266 \def\DC@centre#1#2#3{%
267 \let\DC@end\DC@endcentre
268 \uppercase{\def~}{$\egroup\setbox\tw@=\hbox\bgroup${#2}}%
269 \setbox\tw@=\hbox{${\phantom{{#2}}}$}%
270 \setbox\z@=\hbox\bgroup$\mathcode`#1="8000 }
274 % \begin{macro}{\DC@endcentre}
275 % and then pad out the smaller of the two boxes so there is the same
276 % amount of stuff either side of the point.
278 \def\DC@endcentre{$\egroup
279 \ifdim \wd\z@>\wd\tw@
280 \setbox\tw@=\hbox to\wd\z@{\unhbox\tw@\hfill}%
282 \setbox\z@=\hbox to\wd\tw@{\hfill\unhbox\z@}\fi
287 % \begin{macro}{\DC@right}
288 % \changes{v1.03}{1996/02/28}{Re-implemented, after tools/2093}
289 % This deals with both the cases where a specified number of decimal
292 \def\DC@right#1#2#3{%
295 % If "#3" is empty, add "\hfill" to right align the column, and
296 % Just set "\DC@rl" to begin a group, so nothing fancy is done with
297 % the whole number part.
303 % Otherwise set "\DC@rl" so that the whole number part is put in a
304 % box "\count@" times as wide as a digit.
305 % In order to share code with the other branch, then move "#3" (the
306 % number of decimal places) into "\count@" throwing away the `.' from
308 % \changes{v1.04}{1996/09/23}{Add \cs{hfill} so integer part
309 % is still flush right if no decimal point used.}
311 \edef\DC@rl{to\the\count@\dimen@ii\bgroup\hss\hfill}%
312 \count@\@gobble#3\relax
317 \let\DC@end\DC@endright
319 % Box 2 contains the decimal part, set to "\dimen@" which is
320 % calculated below to be "\count@" times the width of a digit, plus
321 % the with of the `decimal point'.
323 \uppercase{\def~}{$\egroup\setbox\tw@\hbox to\dimen@\bgroup${#2}}%
324 \setbox\z@\hbox{$1$}\dimen@ii\wd\z@
325 \dimen@\count@\dimen@ii
326 \setbox\z@\hbox{${#2}$}\advance\dimen@\wd\z@
327 \setbox\tw@\hbox to\dimen@{}%
329 % Box 0 contains the whole number part, either just at its natural
330 % size for right aligned columns, or set to (the old value of)
331 % "\count@" times the width of a digit. "\DC@rl" defined above
332 % determines the two cases.
334 \setbox\z@\hbox\DC@rl$\mathcode`#1="8000 }
338 % \begin{macro}{\DC@endright}
339 % \changes{v1.03}{1996/02/28}{Re-implemented, after tools/2093}
340 % Just finish off the second box, and then put out both boxes.
342 \def\DC@endright{$\hfil\egroup\box\z@\box\tw@}
347 % The user interface, define the {\tt D} column to take three arguments.
348 % For special purposes, you may need to directly access "\DC@" rather
349 % than the "D" column, eg to get a bold version you could use
351 % \newcolumntype{E}[3]{>{\boldmath\DC@{#1}{#2}{#3}}c<{\DC@end}}
354 \newcolumntype{D}[3]{>{\DC@{#1}{#2}{#3}}c<{\DC@end}}