1 % \iffalse (meta-comment)
2 % -----------------------------------------------------------------
5 % Maintainer: Danie Els (dnjels@sun.ac.za)
7 % This file is part of the booktabs package for publication
8 % quality tables for LaTeX
10 % Copyright (C) 1995--2005 Simon Fear
12 % This program is free software; you can redistribute it and/or
13 % modify it under the terms of the GNU General Public License as
14 % published by the Free Software Foundation; either version 2 of
15 % the License, or (at your option) any later version.
17 % This program is distributed in the hope that it will be useful,
18 % but WITHOUT ANY WARRANTY; without even the implied warranty of
19 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 % GNU General Public License for more details.
22 % You should have received a copy of the GNU General Public
23 % License along with this program; if not, write to the Free
24 % Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 % -----------------------------------------------------------------
28 % This file contains the documentations and source code for the
29 % booktabs package for use with LaTeX2e. See the file 'README'
30 % for a list of all the files as well as directions for the
31 % installation of this package.
32 % -----------------------------------------------------------------
37 \ProvidesFile{booktabs.dtx}
39 %<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
40 %<package>\ProvidesPackage{booktabs}
42 [2005/04/14 v1.61803 publication quality tables]
46 \documentclass{ltxdoc}
47 \usepackage{booktabs}[2005/04/14]
51 \setlength\hfuzz{15pt}
56 \DocInput{booktabs.dtx}
65 % {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
66 % Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
67 % Digits \0\1\2\3\4\5\6\7\8\9
68 % Exclamation \! Double quote \" Hash (number) \#
69 % Dollar \$ Percent \% Ampersand \&
70 % Acute accent \' Left paren \( Right paren \)
71 % Asterisk \* Plus \+ Comma \,
72 % Minus \- Point \. Solidus \/
73 % Colon \: Semicolon \; Less than \<
74 % Equals \= Greater than \> Question mark \?
75 % Commercial at \@ Left bracket \[ Backslash \\
76 % Right bracket \] Circumflex \^ Underscore \_
77 % Grave accent \` Left brace \{ Vertical bar \|
78 % Right brace \} Tilde \~}
82 % \DeleteShortVerb{\|}
83 % \GetFileInfo{booktabs.dtx}
85 % \title{Publication quality tables in \LaTeX%
86 % \thanks{This file has version number \fileversion,
87 % (converging to phi, the golden ratio),
88 % last revised \filedate.}}
89 % \author{Simon Fear\\300A route de Meyrin\\Meyrin\\Switzerland}
90 % \date{Printed \today}
96 % This article describes some additional commands to enhance
97 % the quality of tables in \LaTeX. Guidelines are given as
98 % to what constitutes a good table in this context. The 2000
99 % release (Version 1.61) of the =booktabs= package, described
100 % herein, adds some enhancements to the 1995 release (Version 1.00),
101 % most notably =longtable= compatibility.
102 % Releases (Versions 1.618, 1.6180 and 1.61803) are
103 % only bug patches and support for the =colortbl= package.\footnote{By Danie Els
104 % (\texttt{dnjels@sun.ac.za}) in the absence of the author.}
109 % \section{Introduction}
111 % The routines described below are to enable the easy production
112 % of tables such as should appear in published scientific books and
113 % journals. What distinguishes these from plain
114 % \LaTeX{} tables is the default use of additional space above and below rules,
115 % and rules of varying `thickness'. What further distinguishes them from
116 % the tables many people {\em do} produce using \LaTeX{} is the
117 % absence of vertical rules and double rules.
119 % I must draw a clear
120 % distinction between what I call here a {\em formal table},
121 % which is a set of values in labelled columns, as distinct from
122 % what I will call a {\em tableau}. The latter is the kind of thing
123 % illustrated in the \LaTeX{} manual, and increasingly
124 % common as the output of
125 % many database management systems; it will probably have
126 % icons in abundance, and no doubt use colour too.
127 % The layout of such a {\em tableau} is determined
128 % (hopefully) as a one-off, given a jumble of material the
129 % designer is trying to combine into a meaningful configuration.
130 % But the layout of a {\em table} has been established over
131 % centuries of experience and should only be altered in
132 % extraordinary circumstances.
134 % By way of illustration, consider this tableau from the \LaTeX{}
135 % manual (p. 64 old edition):
138 % \begin{tabular}{||l|lr||} \hline
139 % gnats & gram & \$13.65 \\ \cline{2-3}
140 % & each & .01 \\ \hline
141 % gnu & stuffed & 92.50 \\ \cline{1-1} \cline{3-3}
142 % emu & & 33.33 \\ \hline
143 % armadillo & frozen & 8.99 \\ \hline
147 % This is a hotch-potch of information that is probably
148 % reasonably clearly presented as is (but is the emu stuffed or
149 % not?). However, as a published table, this should
150 % much rather appear along the lines suggested
151 % further down the page in the manual:
154 % \begin{tabular}{@{}llr@{}} \toprule
155 % \multicolumn{2}{c}{Item} \\ \cmidrule(r){1-2}
156 % Animal & Description & Price (\$)\\ \midrule
157 % Gnat & per gram & 13.65 \\
159 % Gnu & stuffed & 92.50 \\
160 % Emu & stuffed & 33.33 \\
161 % Armadillo & frozen & 8.99 \\ \bottomrule
165 % It takes much less work to lay
166 % this out, as a formal table; we don't have to work out a new
167 % layout for everything we do. Moreover, we can be almost certain
168 % that the data cannot be misread, because the reader does not
169 % have to learn how to read some novel presentation.
171 % The above table cannot be produced in pure \LaTeX,
172 % unfortunately. It can be laid out as it should be, but despite
173 % your best efforts, using plain =\hline= commands produces
176 % \begin{tabular}{@{}llr@{}} \hline
177 % \multicolumn{2}{c}{Item} \\ \cline{1-2}
178 % Animal & Description & Price (\$)\\ \hline
179 % Gnat & per gram & 13.65 \\
181 % Gnu & stuffed & 92.50 \\
182 % Emu & stuffed & 33.33 \\
183 % Armadillo & frozen & 8.99 \\ \hline
187 % Note (if it is not already obvious) that there is not enough
188 % space between the top line and the capital I of `Item', and so
189 % on for all the lines: contrast with the previous version. Also, in
191 % the top and bottom rules (ie lines) are
192 % heavier than the middle rule, which is turn heavier than the
193 % subrule underneath `Item'. Of course you \emph{could} redefine
194 % =\doublerulesep= and then use =\hline\hline= to get something
195 % near the same effect, and you could use struts to improve
196 % the spacing. But you should not have to
197 % think of such things. The =booktabs= style defines its commands so
198 % that such things are taken care of automatically.
200 % In general, I would say that
201 % this package is of no interest to those looking for
202 % an alternative to Pic\TeX{} to conjure up fancy tableaux.
203 % Rather, it is a style guide for authors of scientific papers
204 % and books as regards table layout. It is not going too far to
205 % say that if you cannot
206 % create a table using the commands in this package, you should
209 % \subsection{A note on terminology\label{notation}}
211 % In British typesetting, a `line' is always called a `rule'. Perhaps
212 % confusingly (for historic reasons in fact), the `thickness' of
213 % rule is often referred to as is its `width' (whereas just about
214 % everyone else would call this `depth' or `height', if they were thinking
215 % of a horizontal rule). A `thick black line' is called a `heavy
216 % rule'. I have used this terminology in most of the new commands
217 % below. If nothing else it avoids confusion with =\hline=.
219 % \section{The layout of formal tables}
221 % You will not go far wrong if you remember two simple
222 % guidelines at all times:
224 % \item Never, ever use vertical rules.
225 % \item Never use double rules.
227 % These guidelines may seem extreme but I have
228 % never found a good argument in favour of breaking them. For
229 % example, if you feel that the information in the left half of
230 % a table is so different from that on the right that it needs
231 % to be separated by a vertical line, then you should use two
232 % tables instead. Not everyone follows the second guideline:
233 % I have worked for a publisher who insisted on a
234 % double light rule above a row of totals. But this would not
235 % have been my choice.
237 % There are three further guidelines worth mentioning here as they
238 % are generally not known outside the circle of professional
239 % typesetters and subeditors:
240 % \begin{enumerate}\setcounter{enumi}{2}
241 % \item Put the units in the column heading (not in the body of
243 % \item Always precede a decimal point by a digit; thus 0.1
245 % \item Do not use `ditto' signs or any other such convention to
246 % repeat a previous value. In many circumstances a blank
247 % will serve just as well. If it won't, then repeat the value.
250 % Whether or not you wish to follow the minor niceties,
251 % if you use only the following commands in your formal tables
252 % your reader will be grateful. I stress that
253 % the guidelines are not just to
254 % keep the pedantic happy. The principal is that enforced structure of
256 % enforces structured thought in the first instance.
258 % \section{Use of the new commands}
260 % \DescribeMacro\toprule
261 % \DescribeMacro\midrule
262 % \DescribeMacro\bottomrule
263 % In the simplest of cases a table begins with a =\toprule=, has
264 % a single row of column headings, then a dividing rule called
265 % here a =\midrule=; after the columns of data we finish off with
266 % a =\bottomrule=. Most book publishers set the =\toprule= and
267 % =\bottomrule= heavier (ie thicker, or darker; see section \ref{notation})
268 % than the intermediate =\midrule=. However, when tables appear
269 % in very small typesizes it is sometimes impossible to make this
270 % distinction, and moreover quite a few journals routinely use all rules of
271 % the same heaviness.
273 % The rule commands here all take a default
274 % which may be reset within the document (preferably, but not
275 % necessarily, in the preamble). For the top and bottom rules this
277 % is =\heavyrulewidth= and for midrules it is =\lightrulewidth=
278 % (fully described below). In very rare cases where you need to do
279 % something special, you may use the optional arguments to the rule
280 % commands which have formal syntax as follows:
282 % "\toprule["\meta{wd}"]" \\
283 % "\midrule["\meta{wd}"]" \\
284 % "\bottomrule["\meta{wd}"]"
286 % where \meta{wd} is a \TeX dimension (for example 1pt, .5em,
289 % All the rule commands described here go after the closing
290 % =\\= of the preceding row (except =\toprule=, which
291 % comes right after the =\tabular{}= command); in other words,
292 % exactly where plain \LaTeX{} allows =\hline= or =\cline=.
294 % \DescribeMacro\cmidrule
295 % Frequently we need a sub-rule to extend over only some of the columns,
296 % for which we need a =\cmidrule= (the analogue of \LaTeX's =\cline=
297 % command). Generally, this rule should not come to the full width of
298 % the columns, and this is especially the case when we need to begin
299 % a =\cmidrule= straight after the end of another one (\LaTeX's
300 % =\cline=s crash into each other here if you are not extra careful with
301 % =\extracolsep=). Thus, you will generally want to use the optional
302 % `trimming' commands.
304 % The trimming commands, if used at all, go in
305 % parentheses (like this), with no spaces separating them. The possible
306 % specifications are =r=, =r{=\meta{wd}=}=, =l= and
307 % =l{=\meta{wd}=}=, or any combination of these, where \meta{wd} is a
308 % dimension, and =r= and =l= indicate whether the right and/or left ends
309 % of the rule should be trimmed. The form without explicit argument is
310 % equivalent to =r{\cmidrulekern}=, where =\cmidrulekern= defaults to
311 % $0.5$ em, but can be set by the user in the preamble.\footnote{User
312 % feedback suggested the Version 1.00 default, 0.25 em, was too
313 % small. Sorry for any loss of backward compatibility. Remember that
315 % can easily set \texttt{\textbackslash cmidrulekern} in the preamble, or just use
316 % \texttt{(r\{.25em\})} to recover the original behaviour.}
318 % Here's an illustrative example: =(lr{.75em})= gives you a default
319 % left trim and exactly $0.75$ em right trim. Equally valid here is
320 % =(r{.75em}l)=.\footnote{As a matter of fact, \texttt{(lrrlr\{.75em\})}
321 % does the same thing: only the
322 % last encountered left and the last encountered right specification
325 % The full syntax of the command is
327 % "\cmidrule["\meta{wd}"]("\meta{trim}"){"$a$--$b$"}"
329 % where \meta{wd} is an optional rule width command, in square
330 % brackets [like this] (the
331 % default here is =\cmidrulewidth=), and the last argument, \emph{which
332 % is not optional}, gives the column numbers to be spanned.
334 % An example of the commands in use is given by the code used to
335 % produce the example table above:
337 % \begin{tabular}{@{}llr@{}} \toprule
338 % \multicolumn{2}{c}{Item} \\ \cmidrule(r){1-2}
339 % Animal & Description & Price (\$)\\ \midrule
340 % Gnat & per gram & 13.65 \\
342 % Gnu & stuffed & 92.50 \\
343 % Emu & stuffed & 33.33 \\
344 % Armadillo & frozen & 8.99 \\ \bottomrule
348 % \DescribeMacro\addlinespace
349 % Occasionally we want to put an extra space between certain rows
350 % of a table; for example, before the last row, if this is a
351 % total. This is simply a matter of inserting
353 % "\addlinespace["\meta{wd}"]"
355 % after the =\\= alignment marker. Between ordinary rows of text, the
356 % effect is identical to the ordinary \LaTeX\ usage
357 % =\\[\defaultaddspace]=, which I find rather clumsy, and it is better
358 % than =\\ \\=, which inserts too much space. Also,
359 % =\addlinespace= can be used before, after, or between rules if you
360 % want to control the exact amount of space to be inserted. The default
361 % space before or after an adjacent rule is replaced by exactly
362 % =\defaultaddspace= or the amount of
363 % space specified in the optional argument.\footnote{This is a change
364 % from version 1.00, where the space was sometimes \emph{in addition
365 % to} default rule space.}
367 % \section{Abuse of the new commands}
369 % Let's face it, nobody can leave well alone, so here are some
370 % guidelines and extra commands.
372 % The new rule commands are not guaranteed to work with =\hline=
373 % or =\cline=, although these remain available and unchanged. I
374 % cannot foresee any reason to want to mix them.
376 % More importantly the rules generated by the new commands are in
377 % no way guaranteed to connect with verticals generated by ={|}=
378 % characters in the preamble. This is a feature (see above). You
379 % should not use vertical rules in tables, end of story.
381 % \DescribeMacro\morecmidrules
382 % If you just cannot stop yourself from using a double rule,
383 % even a construction as bizarre as =\toprule\bottomrule\midrule=
384 % will work without generating an error message (just as you can
385 % double =\hline=). These rules will be separated by the ordinary
386 % \LaTeX\ separator =\doublerulesep=. However if your perversion
387 % is to want double =\cmidrule=s you will need the extra command
388 % =\morecmidrules= to do so properly, because normally two
389 % =\cmidrule=s in a row is a sane construction
390 % calling for two rules on the same `rule row'. Thus in
392 % "\cmidrule{1-2}\cmidrule{1-2}"
394 % the second command writes a rule that just overwrites the first
395 % one; I suppose you wanted
397 % "\cmidrule{1-2}\morecmidrules\cmidrule{1-2}"
399 % which gives you a double rule between columns one and two,
400 % separated by =\cmidrulesep= (note: since a =\cmidrule= is
401 % generally very light, the ordinary =\doublerulesep= is probably
402 % too much space). Finish off a whole row of rules before giving the
403 % =\morecmidrules= command. Note that =\morecmidrules= has no
404 % effect whatsoever if it does not immediately follow a
405 % =\cmidrule= (ie it is not a general space-generating command).
407 % \DescribeMacro\specialrule
408 % If you find some extraordinary need to specify exactly $0.5$ em, say, between two
409 % rules, you could use a construction such as =\midrule =
410 % =\addlinespace[.5em] \midrule=. In a rare fit of tolerance, though,
412 % provided the command
414 % "\specialrule{"\meta{wd}"}{"\meta{abovespace}"}{"\meta{belowspace}"}"
416 % where all three arguments are mandatory (I couldn't be bothered to
417 % program in defaults). If you use this frequently, you have
418 % misunderstood the purpose and content of the guidelines given above. A
419 % preceeding rule does not add its default space below, and a following
420 % rule adds no space above itself, so you get \emph{exactly} the space
421 % specified in the arguments.\footnote{This is a change from Version 1.00,
422 % which rather liked to add an extra \texttt{\textbackslash doublerulesep} space whenever it
426 % \section{Booktabs and longtables}
428 % If you have both =booktabs= and =longtable= packages loaded, the
429 % =booktabs= rule commands can now all be used exactly as described
430 % above, within a longtable.
432 % There is an addition worth noting: within a =longtable=, you can use
433 % the optional left and right trimming commands, which normally only
434 % work for =\cmidrule=s, with =\toprule=, =\midrule= and =\bottomrule=
435 % (and if you must, also with =\specialrule=). Users who hacked the
436 % previous release for longtable compatability\footnote{Jim Service was
437 % the first} seemed to like all the rules to be right trimmed 0.5 em. I
438 % think you can do the same by making =@{}= be the last column
439 % specifier. Still, after working out the rest of the code, it was easy
440 % to add parsing for the optional arguments, so I did. (I didn't go the
441 % whole way and allow the optional trimming \emph{outside} a
442 % =longtable=; this would be a huge amount of work. If you must have
443 % trimmed rules, make all your tables be =longtable=s!)
445 % A somewhat technical note: within a =longtable=, =\hline= and
446 % =\hline\hline= both produce a \emph{double} rule (to allow for page
447 % breaks occurring at that point). But the =booktabs= rules do
448 % \emph{not}. Longtable's automatic doubling of =\hline= is
449 % questionable, even according to the documentation within that
450 % package. But doubled =booktabs= rules make almost no sense at all. In
451 % the unfortunate event that a =booktabs= rule should occur at a page
452 % break, then you will have to make the necessary adjustments by
453 % hand. (In general, this will mean deleting the offending rule.)
456 % \section{Booktabs and and the colortbl package}
458 % =Booktabs= is now compatible with the =colortbl= package.\footnote{Since v1.6180}
459 % The \cmd{\arrayrulecolor} command will result in coloured
460 % rules if the =colortbl= package is loaded.
463 % \section{Technical summary of commands}
465 % The new rule commands are valid inside the standard =tabular= (and
466 % =array=) environment, in the modified =tabular= and =array= of
467 % =\usepackage{array}=, and within both standard tables and longtables
468 % after =\usepackage{longtable}=.
470 % The commands follow the standard placement syntax of =\hline=.
471 % There can be space (including carriage-return,
472 % but not two carriage-returns) between successive
473 % rule commands.\footnote{A welcome change from Version 1.00, where space between
474 % rule commands generated a very baffling error message.}
476 % In what amounts to quite a big change from former releases, within
477 % the macro code I now define three classes of rules. (But we don't need these
478 % definitions within ordinary use, so I haven't even mentioned them above.)
479 % A class 1 rule (otherwise called a `normal' rule) is any of
480 % =\toprule=, =\midrule=, =\bottomrule=, or =\cmidrule=. The class 2
481 % rules are =\specialrule= and =\addlinespace=. Finally, a class 0 rule
482 % is none of the preceeding --- or in other words, not a rule at
483 % all.\footnote{Except that \texttt{\textbackslash hline} and
484 % \texttt{\textbackslash cline} are class 0. Still, there
485 % is no reason to lose sleep over this, since one would not want to mix
486 % the two rule-drawing systems.} Note that =\addlinespace= counts as a
487 % class 2 rule, not as class 0 text.
489 % In the following, we first describe each command in `normal use', meaning
490 % that the rule is being used between two lines of text (or more
491 % technically, is preceded and followed by a class 0 rule). After that,
492 % we will look at the exceptions.
494 % "\toprule["\meta{wd}"]"
496 % A rule of width \meta{wd} (default =\heavyrulewidth=) with
497 % =\abovetopsep= space above and
498 % =\belowrulesep= extra vertical space inserted below it. By default,
499 % =\abovetopsep= is zero, which seems sensible for a rule designed to
500 % go at the top. However, if your
501 % tables have captions, it can make sense to use =\abovetopsep= to
502 % insert a reasonable amount of space between caption and table,
503 % rather than remember to use a =\vspace{}= command in the float.
505 % "\midrule["\meta{wd}"]"
507 % A \meta{wd} (default =\lightrulewidth=) rule with
508 % =\aboverulesep= space above it and
509 % with =\belowrulesep= space below it.
511 % "\bottomrule["\meta{wd}"]"
513 % A \meta{wd} (default =\heavyrulewidth=) rule with =\aboverulesep=
514 % space above it and with =\belowbottomsep= space below it. By default
515 % =\belowbottomsep= is zero\footnote{This is a change from Version 1.00,
516 % where there was always a \texttt{\textbackslash belowrulesep}}.
517 % There is a frequent and legitimate reason
518 % you might want space below a bottom rule: namely, when there's a table
519 % footnote.\footnote{But don't use footnotes, Donald.}
520 % If you don't override the default you could use
521 % =\bottomrule \addlinespace[\belowrulesep]=
522 % or you could put a suitably sized strut
523 % into the footnote text.\footnote{I don't like either of these. Sort it out in
524 % Version 1.618?} But the default has to be zero, so that it behaves
525 % sensibly in a =longtable= footer.
527 % "\cmidrule["\meta{wd}"]("\meta{trim}"){"$a$--$b$"}"
529 % A \meta{wd} (default =\cmidrulewidth=) rule with
530 % =\aboverulesep= space above it (unless following another
531 % =\cmidrule=, in which case it is on the same vertical alignment; or
532 % if following =\morecmidrules=, separated from a previous =\cmidrule=
533 % by =\cmidrulesep=). A =\cmidrule= has
534 % =\belowrulesep= below it (unless followed by another
535 % =\cmidrule=, in which case the following rule is on the same
536 % vertical alignment; or if followed by =\morecmdirules=, when there
538 % =\cmidrulesep= below it).
540 % The =\cmidrule= spans columns $a$ to $b$ as specified in the mandatory
541 % argument. The optional argument \meta{trim}, which goes in parentheses
542 % if at all, can contain any sequence of the tokens =r=, =l= and
543 % ={=\meta{wd}=}=, with the latter setting the kerning to be applied to
544 % right or left sides as specified by the immediately preceding token.
545 % (There's currently no error checking done here, so be careful to get
550 % Instructs \LaTeX\ to begin a new row of =\cmidrules=,
551 % separated from the last by =\cmidrulesep=. Has no meaning
552 % in any other context.
554 % "\specialrule{"\meta{wd}"}{"\meta{abovespace}"}{"\meta{belowspace}"}"
556 % A \meta{wd} rule (note: here this is a mandatory argument) with
557 % \meta{abovespace} above it and \meta{belowspace} below it.
559 % "\addlinespace["\meta{wd}"]"
561 % Technically this has the same effect as
562 % =\specialrule{0pt}{0pt}{=\meta{wd}=}=, i.e. a zero-width rule with no
563 % space above and with \meta{wd} (default =\defaultaddspace=) space
564 % below. This command was primarily designed to add space between rows
565 % in the body of the table, but it may also be used to specify an exact
566 % amount of space above or below a class 1 rule.
569 % Now we come to the exceptions to the above. We have already seen in
570 % the definitions that the type 2 rules are preceded and followed by
571 % exactly the amount of space specified by the arguments. That is, a
572 % type 2 rule suppresses the space that would normally be generated by a
573 % previous type 1 rule (e.g. =\belowrulesep= after a =\toprule=) and
574 % replaces it by the argument of the type 2 rule. Similarly, in the
575 % combination \{type 2 rule\}\{type 1 rule\}, the ordinary space above
576 % the type 1 rule (e.g. =\aboverulesep=) is suppressed. But in the
577 % combination \{type 2 rule\}\{type 2 rule\}, no space is suppressed:
578 % the rules will be separated by both the first rule's
579 % ={=\meta{belowspace}=}= and the second rule's ={=\meta{abovespace}=}=
580 % arguments. Last but not least, the combination \{type 1 rule\}\{type 1
581 % rule\} will always give rules separated by =\doublerulesep=,
582 % suppressing all normal space generated between the rules (but
583 % retaining normal space above the first and below the second).
585 % As an exception to this last exception, `type 1 rule' excludes
586 % =\cmidrule=. Such rules combine with other =\cmidrule=s and
587 % =\morecmidrules= in normal use as described above. I don't know and
589 % care what the combination =\toprule\cmidrule{1-2}\midrule= would
590 % produce. I can see no excuse for such usage.
593 % The default dimensions are defined at the beginning of the macro
594 % description section (Section \ref{code}). The user can change
595 % these defaults in the preamble, or outside a
596 % tabular environment, by simply
597 % inserting a command in exactly the same format as in Section \ref{code}; the
598 % redefinition will stay in effect for the rest of the document
599 % or until redefined again. \emph{Inside a table} you would have to make the
600 % assignment globally in a =noalign= group:
601 % e.g. \verb|\noalign\{\global\abovetopsep=1em\toprule}|. I hope
602 % you never have to do that.
604 % \section{Acknowledgments}
605 % Hugely indebted of course to DEK and Lamport; the optional
606 % argument and =\cmidrule=
607 % stuff especially was stolen from =latex.sty=.
608 % The documentation driver stuff is stolen from the
609 % tools package description =dcolumn.dtx= by David Carlisle.
611 % For beta testing and encouragement ...
615 % \section{The code\label{code}}
617 % The current version is defined at the top of the file looking
618 % something like this
621 %\NeedsTeXFormat{LaTeX2e}
622 %\ProvidesPackage{booktabs}
623 % [\filedate\space version\fileversion]
626 % First we set up the new dimensions described above:
628 \newdimen\heavyrulewidth
629 \newdimen\lightrulewidth
630 \newdimen\cmidrulewidth
631 \newdimen\belowrulesep
632 \newdimen\belowbottomsep
633 \newdimen\aboverulesep
634 \newdimen\abovetopsep
635 \newdimen\cmidrulesep
636 \newdimen\cmidrulekern
637 \newdimen\defaultaddspace
638 \heavyrulewidth=.08em
639 \lightrulewidth=.05em
645 \cmidrulesep=\doublerulesep
647 \defaultaddspace=.5em
649 % And some internal counters of no interest to the end user:
650 % \changes{v1.618}{2003/03/28}{Remove \cs{@cmidrulewidth} }
654 \newdimen\@aboverulesep
655 \newdimen\@belowrulesep
656 \newcount\@thisruleclass
657 \newcount\@lastruleclass
659 \newdimen\@thisrulewidth
661 % which will be described as needed below.
663 % \begin{macro}{\futurenonspacelet}
664 % Next we define a very useful macro (more-or-less straight from the
665 % \TeX book's Dirty Tricks chapter; documented there). Use
666 % =\futurenonspacelet= instead of =\futurelet= when looking for the next
667 % (non-space) token after a macro that has an argument. (After a macro
668 % without an argument, space is ignored anyway, so =\futurenonspacelet=
669 % wouldn't be needed.) This hack allows users to type white
670 % space between successive rule commands (which did not work in Version
673 \def\futurenonspacelet#1{\def\@BTcs{#1}%
674 \afterassignment\@BTfnslone\let\nexttoken= }
675 \def\@BTfnslone{\expandafter\futurelet\@BTcs\@BTfnsltwo}
676 \def\@BTfnsltwo{\expandafter\ifx\@BTcs\@sptoken\let\next=\@BTfnslthree
677 \else\let\next=\nexttoken\fi \next}
678 \def\@BTfnslthree{\afterassignment\@BTfnslone\let\next= }
682 % \subsection{Full width rules}
684 % When we are not in a =longtable= environment, we can simply
685 % implement the full width rules as a =\hrule= in a =\noalign{}=
686 % group. But within a =longtable=, the rule has to be drawn like a
687 % =\cmidrule{1-\LT@cols}= (the rationale for this is explained in the
688 % =longtable= documentation).
690 % In order to allow for both, all the rule macros have to
691 % open a =\noalign= group immediately, while they work out whether
692 % they have been called within a =longtable=; if you don't do this,
693 % \TeX's underlying =\halign= process gets hiccups.
695 % dirty trick (\verb?\ifnum=0`}?) to fool the parser that the bracket
696 % count is OK. The bracket really gets closed after all the skipping
697 % at the end of the =\@BTendrule= macro.
699 % The class 1 rules, and =\specialrule=, really only differ in the
700 % defaults for space above and
701 % below, and the width, passed to a common routine, =\@BTrule=,
702 % described below. The spaces, =\@aboverulesep= and =\@belowrulesep=,
703 % are set within the =\noalign= group, so are inherited by
704 % =\@BTrule=. Similarly, =\@BTrule= knows as much as it needs to
705 % about the routine that called it by examining the inherited
706 % =\@thisruleclass=. The optional width argument is parsed by
707 % =\@BTrule= after being set to default if absent.
710 % \begin{macro}{\toprule}
711 % \changes{v1.61803}{2005/04/14}{Change \cmd{\@belowrulesep} to \cmd{\belowrulesep}}
712 % \begin{macro}{\midrule}
713 % \begin{macro}{\bottomrule}
714 % \begin{macro}{\specialrule}
716 \def\toprule{\noalign{\ifnum0=`}\fi
717 \@aboverulesep=\abovetopsep
718 \global\@belowrulesep=\belowrulesep %global cos for use in the next noalign
719 \global\@thisruleclass=\@ne
720 \@ifnextchar[{\@BTrule}{\@BTrule[\heavyrulewidth]}}
721 \def\midrule{\noalign{\ifnum0=`}\fi
722 \@aboverulesep=\aboverulesep
723 \global\@belowrulesep=\belowrulesep
724 \global\@thisruleclass=\@ne
725 \@ifnextchar[{\@BTrule}{\@BTrule[\lightrulewidth]}}
726 \def\bottomrule{\noalign{\ifnum0=`}\fi
727 \@aboverulesep=\aboverulesep
728 \global\@belowrulesep=\belowbottomsep
729 \global\@thisruleclass=\@ne
730 \@ifnextchar[{\@BTrule}{\@BTrule[\heavyrulewidth]}}
731 \def\specialrule#1#2#3{\noalign{\ifnum0=`}\fi
732 \@aboverulesep=#2\global\@belowrulesep=#3\global\@thisruleclass=\tw@
739 % \begin{macro}{\addlinespace}
740 % An =\addlinespace= is essentially a zero-width rule with
741 % zero space above and argument (or default) space below. But because
742 % the rule is not actually drawn, but is just a =\vskip=, there is no
743 % need to check if we're in a longtable, so we don't need to call
744 % =\@BTrule= as for `real' rules. But we do share the =\@BTendrule=
745 % lookahead and flagsetting code (described below), and the =\vskip= is done there.
747 \def\addlinespace{\noalign{\ifnum0=`}\fi
748 \@ifnextchar[{\@addspace}{\@addspace[\defaultaddspace]}}
749 \def\@addspace[#1]{\global\@belowrulesep=#1\global\@thisruleclass=\tw@
750 \futurelet\@tempa\@BTendrule}
755 % \begin{macro}{\@BTrule}
756 % All the rules (except =\addlinespace=) share this code.
759 \global\@thisrulewidth=#1\relax
761 % Save the width argument (if the user didn't give one, then the
762 % calling routine will have called =\@BTrule= with the default) in a global
763 % variable for later use when drawing the rule.
765 \ifnum\@thisruleclass=\tw@\vskip\@aboverulesep\else
767 % Specialrules always insert specified space above. (Note: addlinespaces
770 \ifnum\@lastruleclass=\z@\vskip\@aboverulesep\else
771 \ifnum\@lastruleclass=\@ne\vskip\doublerulesep\fi\fi\fi
773 % After text (last rule class 0), precede the rule by =\aboverulesep=;
774 % but if immediately after a
775 % previous rule, insert a =\doublerulesep=.
777 % Now we work out, by a very nasty hack, if we're within a
778 % =longtable=. It's easy if =\longtable= isn't even defined: then we
779 % can't be. But it is not enough just to check if =longtable= is
780 % loaded --- we might be within an ordinary table rather than a
781 % longtable. So we look to see if =\hline= has been re-defined
782 % from its \LaTeX\ definition to be the same as
783 % =\LT@hline=. (Longtable currently does this redefinition when it opens a
784 % =longtable= environment, but not globally, so it is
785 % cleared it when the environment closes.) Another
786 % package could potentially do this! And =longtable= might change the
787 % way it implements this! So, it is not entirely safe, but I have
788 % found no better way so far.
790 % We set up =\@BTswitch= to call =\@BTnormal= or =\@BLTrule=, as
791 % appropriate, then call it.
793 \ifx\longtable\undefined
794 \let\@BTswitch\@BTnormal
795 \else\ifx\hline\LT@hline
796 \let\@BTswitch\@BLTrule
798 \let\@BTswitch\@BTnormal
805 % \begin{macro}{\CT@arc@}
806 % \changes{v1.6180}{2005/03/16}{add colortbl command for color support}
807 % This is support for the colortbl package for colored
808 % rules. \cmd{\CT@arc@} hold the \cmd{\arrayrulecolor} setting.
811 \providecommand*\CT@arc@{}}%% colortbl support
816 % \begin{macro}{\@BTnormal}
817 % \changes{v1.6180}{2005/03/16}{add colortbl \cmd{\CT@arc@} command for color support}
818 % This is when we're \emph{not} within a longtable. We are already in a
819 % =\noalign= group, all we need do is draw an =\hrule= and gobble any
820 % trailing spaces, then call the closing routine with =\@tempa= set
821 % equal to the next token in the document.
825 {\CT@arc@\hrule\@height\@thisrulewidth}%
826 \futurenonspacelet\@tempa\@BTendrule}
830 % \begin{macro}{\@BLTrule}
831 % This is for full width rule within a =longtable=. First we check if
832 % a kerning argument has been used; if so let =\@@BLTrule= read it,
833 % else call =\@@BLTrule= with an empty string:
835 \def\@BLTrule{\@ifnextchar({\@@BLTrule}{\@@BLTrule()}}
838 % \begin{macro}{\@@BLTrule}
840 \def\@@BLTrule(#1){\@setrulekerning{#1}%
841 \global\@cmidlb\LT@cols
843 % The =\@setrulekerning= routine parses the kerning argument tokens
844 % and sets global kerning widths accordingly (or to defaults, if user
845 % hasn't set them explicitly). The global assignment to =\@cmidlb=
846 % sets up the column count for the =\@cmidruleb= macro, which is
847 % shared with =cmidrules=.
851 % Close the currently open =\noalign= group. Within a =longtable=,
852 % rules are all to be drawn as leaders within a text box that is
853 % =\LT@cols= columns wide.
857 % Draw the rule. We share the =\@cmidruleb= code with ordinary =\cmidrule=s.
859 \noalign{\ifnum0=`}\fi
861 % We have to open a new noalign immediately else \TeX will start a new
862 % text box where we don't want one. Then, after gobbling any
863 % unwanted white space, we call the closing routine.
865 \futurenonspacelet\@tempa\@BTendrule}
868 % \begin{macro}{\@BTendrule}
869 % We look one step ahead (token is in =\@tempa=) to see if another
870 % rule follows (shame on user!). If so, we set =\@lastruleclass= equal
871 % to =\@thisruleclass= (thus setting it up for the following rule). If
872 % there isn't a following rule, we clear =\@lastruleclass= (ie set it
873 % to zero), which isn't technically true since we have just drawn a
874 % rule, but sets it up correctly for the next rule encountered, which
875 % must be following some intervening text.
877 \def\@BTendrule{\ifx\@tempa\toprule\global\@lastruleclass=\@thisruleclass
878 \else\ifx\@tempa\midrule\global\@lastruleclass=\@thisruleclass
879 \else\ifx\@tempa\bottomrule\global\@lastruleclass=\@thisruleclass
880 \else\ifx\@tempa\cmidrule\global\@lastruleclass=\@thisruleclass
881 \else\ifx\@tempa\specialrule\global\@lastruleclass=\@thisruleclass
882 \else\ifx\@tempa\addlinespace\global\@lastruleclass=\@thisruleclass
883 \else\global\@lastruleclass=\z@\fi\fi\fi\fi\fi\fi
884 \ifnum\@lastruleclass=\@ne\relax\else\vskip\@belowrulesep\fi
890 % \subsection{Special subrules}
892 % \begin{macro}{\@setrulekerning}
893 % The following code parses the trimming arguments (if there are any)
894 % for =\cmidrule= or a =\BLTrule=. The rule
895 % will be trimmed left and right by =\cmrkern@l= and =\cmrkern@l=,
896 % which are zero by default, set to =\cmidrulekern= by the plain
897 % =(lr)= arguments, or user set as in =(r{.5em})=. We parse token by
898 % token through the arguments. The
899 % tokens =r= and =l= cause =\cmrkern@r= or =\cmrkern@l= to be set to
900 % =\cmidrulekern=. There is no lookahead to see if a width is the
901 % next token; this strategy is efficient for the plain commands,
902 % while inefficient for the qualified commands, but more importantly
903 % it is much easier to program. Tokens =r= and =l= also set
904 % =\cmrswitch= so that if the next token turns out to be
905 % ={=\meta{wd}=}= then the kerning will be done on the side
906 % currently specified. I have been too lazy to program an error
907 % message should one encounter tokens other than =r=, =l= or
909 % \changes{v1.6180}{2005/03/16}{Refine option testing in \cmd{\@setrulekerning}}
911 \def\@setrulekerning#1{%
912 \global\let\cmrkern@l\z@
913 \global\let\cmrkern@r\z@
914 \@tfor\@tempa :=#1\do
917 \global\let\cmrkern@r\cmidrulekern
918 \def\cmrsideswitch{\cmrkern@r}%
922 \global\let\cmrkern@l\cmidrulekern
923 \def\cmrsideswitch{\cmrkern@l}%
925 \global\expandafter\let\cmrsideswitch\@tempa
930 % \begin{macro}{\cmidrule}
931 % \begin{macro}{\@cmidrule}
932 % \begin{macro}{\@@cmidrule}
933 % \begin{macro}{\@@@cmidrule}
934 % The =\cmidrule= re-uses =\@lastruleclass= in an entirely different
935 % way from the full width rules. (Maybe I should have used a
936 % different flag; it seemed efficient at the time \ldots). This
937 % is (left) set to one if you are in the middle of a row of
938 % =\cmidrules=, or starting a new one (with =\morecmidrules=).
939 % Otherwise, when =\@lastruleclass= is zero, we precede the
940 % rule with =\aboverulesep=.
942 \def\cmidrule{\noalign{\ifnum0=`}\fi
943 \@ifnextchar[{\@cmidrule}{\@cmidrule[\cmidrulewidth]}}
944 \def\@cmidrule[#1]{\@ifnextchar({\@@cmidrule[#1]}{\@@cmidrule[#1]()}}
945 \def\@@cmidrule[#1](#2)#3{\@@@cmidrule[#3]{#1}{#2}}
947 % The above is fiddling around to set defaults for missing optional
948 % arguments. We also pass to =\@@@cmidrule= in a different order,
949 % namely =[a-b]=={width required}= ={kerning commands}= (this being
950 % the order in which the arguments are actually processed):
952 \def\@@@cmidrule[#1-#2]#3#4{\global\@cmidla#1\relax
953 \global\advance\@cmidla\m@ne
954 \ifnum\@cmidla>0\global\let\@gtempa\@cmidrulea\else
955 \global\let\@gtempa\@cmidruleb\fi
956 \global\@cmidlb#2\relax
957 \global\advance\@cmidlb-\@cmidla
959 % This has set up a switch (=\@gtempa=) to call the relevant routine,
960 % =\@cmidrulea= or =\@cmidruleb=, depending on whether we start
961 % from column one or not.
963 \global\@thisrulewidth=#3
965 % That is, set per default or given argument. Then parse any trimming
966 % arguments to set, globally, =\cmrkern@r= and =\cmrkern@l= accordingly:
970 % Now insert space above if needed, close the =\noalign=,
971 % then switch to appropriate rule drawing routine as determined
972 % above (=\let= to =\@gtempa=):
974 \ifnum\@lastruleclass=\z@\vskip \aboverulesep\fi
975 \ifnum0=`{\fi}\@gtempa
977 % Having now drawn the rule,
978 % open another =\noalign=, and call the closing routine:
980 \noalign{\ifnum0=`}\fi\futurenonspacelet\@tempa\@xcmidrule}
987 % \begin{macro}{\@xcmidrule}
988 % In this closing routine, see if another =\cmidrule= follows;
990 % vertical so it will line up with the one you just drew,
991 % and setting =\@lastruleclass= to 1 will suppress
992 % adding space above the next. If a =\morecimdrules= follows,
993 % we add (positive) =\cmidrulesep= (and again set =\@lastruleclass=
994 % to one). Otherwise this is the last rule of the current group
995 % and we can just add =\belowrulesep=. Finally, we close the =\noalign=.
996 % \changes{v1.618}{2003/03/28}{Change to \cmd{\@xcmidrule}:
997 % replace \cmd{\@cmidrulewidth} with \cmd{\@thisrulewidth }}
1000 \ifx\@tempa\cmidrule
1001 \vskip-\@thisrulewidth
1002 \global\@lastruleclass=\@ne
1003 \else \ifx\@tempa\morecmidrules
1005 \global\@lastruleclass=\@ne\else
1006 \vskip \belowrulesep
1007 \global\@lastruleclass=\z@
1012 % \begin{macro}{\@cmidrulea}
1013 % This code (called below) actually draws the rules. They are drawn
1014 % as boxes in text, rather than in a =\noalign= group, which permits
1015 % the left and right kerning.
1016 % \changes{v1.6180}{2005/03/16}{add colortbl \cmd{\CT@arc@} command for color support}
1019 \multispan\@cmidla&\multispan\@cmidlb
1020 \unskip\hskip\cmrkern@l%
1021 {\CT@arc@\leaders\hrule \@height\@thisrulewidth\hfill}%
1022 \hskip\cmrkern@r\cr}%
1025 % \begin{macro}{\@cmidruleb}
1026 % \changes{v1.6180}{2005/03/16}{add colortbl \cmd{\CT@arc@} command for color support}
1030 \unskip\hskip \cmrkern@l%
1031 {\CT@arc@\leaders\hrule \@height\@thisrulewidth\hfill}%
1032 \hskip\cmrkern@r\cr}%
1036 % \begin{macro}{\morecmidrules}
1037 % This is really a dummy command; all the work is done above
1038 % within the =\cmidrule= routine. We look one step ahead there
1039 % to see if a =\morecmidrules= follows the current =\cmidrule=,
1040 % and if so set the flag. Otherwise, =\morecmidrules= itself
1043 \def\morecmidrules{\noalign{\relax}}