The caption2 package^{∗}

Axel Sommerfeldt

                 caption@sommerfeldt.net

2002/08/03

Abstract

The caption2 package provides many ways to customise the captions in

     environments such figure, table, longtable, and sidewaystable. The fol-

lowing L^{A}TEX 2ε packages are supported: float, longtable, and subfigure. But it works fine together with the following packages as well: floatfig, rotating, supertabular, and wrapfig.

1 Documentation

A (not yet finished) german documentation can be found within the file anleitung.tex. I’m terrible sorry, but at this time there is no english documentation available, I will start translating the german one right after finishing it. In the meantime take a look at the document epslatex, especially the section 19 “Customizing Captions with caption2”. This document can be found at

     ftp://tug.ctan.org/tex-archive/info/

as epslatex.ps and epslatex.pdf. There is also a french translation available:

     ftp://ftp.dante.de/pub/tex/info/fepslatex.ps

∗This package has version number v2.1, last revised 2002/08/03.

2 The Implementation

2.1 Preliminary declarations

\captionfont \captionlabelfont  \captionfont and \captionlabelfont will hold the font specifications for the

caption.

 1 \newcommand*\captionfont{}
 2 \newcommand*\captionlabelfont{}
\captionlabeldelim \captionlabelsep  \captionlabeldelim & \captionlabelsep will hold the iterim space between

caption label and text. (\captionlabeldelim will be typeset within \captionlabelfont,

\captionlabelsep not.)
 3 \newcommand*\captionlabeldelim{}
 4 \newcommand*\captionlabelsep{}

\captionsize The macro \captionsize is obsolete since v1.4 of the caption package, but we still support it to provide backward compatibility.

 5 \newcommand*\captionsize{}
\captionmargin \captionwidth \ifcaptionwidth Either \captionmargin (with specifies an extra margin) or \captionwidth (with

specifies an explicit width) can be set, therefore we need the flag \ifcaptionwidth to determine with parameter we should pay attention to.

 6 \newdimen\captionmargin
 7 \newdimen\captionwidth
 8 \newif\ifcaptionwidth

\captionindent \captionindent will be used in caption style indent and specifies the indention after the first line.

 9 \newdimen\captionindent
\ifcaptionlabel \ifonelinecaptions \ifignoreLTcapwidth More flags. If \ifcaptionlabel is not set the caption label should be suppressed;

we need this flag to support the \caption* command. If \ifonelinecaptions is set we support the L^{A}TEX base style ’one line captions’, that means the caption will be typeset centered if it fits to one line. If \ifignoreLTcapwidth is set we ignore the \LTcapwidth of longtable.

10 \newif\ifcaptionlabel\captionlabeltrue
11 \newif\ifonelinecaptions
12 \newif\ifignoreLTcapwidth

\setcaptionmargin \setcaptionwidth User-friendly commands to set the caption margin resp. width. Note that they additionally set the \ifcaptionwidth flag.

13 \newcommand*\setcaptionmargin{%
14 \captionwidthfalse
15 \setlength\captionmargin}
16 \newcommand*\setcaptionwidth{%
17 \captionwidthtrue
18 \setlength\captionwidth}

\normalcaptionparams \normalcaptionparams resets all caption related parameters to it’s normal default values. \captionfont will be set to \captionsize so setting the obsolete \captionsize will still work. Same story with \captiondelim and the obsolete

\captionlabeldelim.

19 \newcommand*\normalcaptionparams{%
20 \let\captionsize\@empty
21 \renewcommand*\captionfont{\captionsize}%
22 \let\captionlabelfont\@empty%
23 \renewcommand*\captionlabeldelim{:}%
24 \renewcommand*\captionlabelsep{\space}%
25 \setcaptionmargin\z@\setlength\captionindent\z@
26 \onelinecaptionstrue}

\caption@eh Some commands will produce an error message, use this as help text.

27 \newcommand*\caption@eh{%
28 If you do not understand this error, please take a closer look\MessageBreak
29 at the documentation of the `caption2' package.\MessageBreak
30 \@ehc}
\defcaptionstyle \newcaptionstyle \renewcaptionstyle These macros will define a new caption style. \newcaptionstyle and \renewcaptionstyle

will additionally check if the caption style already exists or not.

31 \newcommand*\defcaptionstyle[1]{%
32 \@namedef{caption@@#1}}
33 \newcommand*\newcaptionstyle[1]{%
34 \expandafter\ifx\csname caption@@#1\endcsname\relax
35  \expandafter\defcaptionstyle
36 \else
37  \PackageError{caption2}{Caption style `#1' already defined}{\caption@eh}%
38  \expandafter\@gobbletwo
39 \fi
40 {#1}}
41 \newcommand*\renewcaptionstyle[1]{%
42 \expandafter\ifx\csname caption@@#1\endcsname\relax
43  \PackageError{caption2}{Caption style `#1' undefined}{\caption@eh}%
44  \expandafter\@gobbletwo
45 \else
46  \expandafter\defcaptionstyle
47 \fi
48 {#1}}

\dummycaptionstyle This macro will also define a new caption style, but a one which is based on the actual set caption style. Therefore you can’t set a caption style made with this command with \captionstyle – we check this to avoid an endless recursion.

49 \newcommand*\dummycaptionstyle[2]{%
50 \defcaptionstyle{#1}{%
51  \expandafter\ifx\csname caption@@\caption@style\expandafter\endcsname%
52  \csname caption@@#1\endcsname
53  \PackageError{caption2}{You can't use the caption style `#1' directy}{%
54  The caption style `#1' is only a dummy and does not really exists.%
55  \MessageBreak You have to redefine it (with \protect\renewcaptionstyle)
56  before you can select\MessageBreak it with \protect\captionstyle.
57  \space\caption@eh}%
58  \else
59  #2\usecaptionstyle{\caption@style}%
60  \fi}}
style `normal' style `center' style `centerlast' style `flushleft' style `flushright' style `hang' style `indent' The predefined caption styles ‘normal’, ‘center’, ‘flushleft’, ‘flushright’, ‘centerlast’,

‘hang’, ‘hang+X’, and ‘indent’. Because they are quite similar they all are based on the macro \caption@makecaption which we’ll define later on.

61 \newcaptionstyle{normal}{\caption@makecaption{normal}}
62 \newcaptionstyle{center}{\caption@makecaption{center}}
63 \newcaptionstyle{centerlast}{\caption@makecaption{centerlast}}
64 \newcaptionstyle{flushleft}{\caption@makecaption{flushleft}}
65 \newcaptionstyle{flushright}{\caption@makecaption{flushright}}
66 \newcaptionstyle{hang}{\caption@makecaption{hang}}
67 \newcaptionstyle{hang+center}{\caption@makecaption{hang@center}}
68 \newcaptionstyle{hang+centerlast}{\caption@makecaption{hang@centerlast}}
69 \newcaptionstyle{hang+flushleft}{\caption@makecaption{hang@flushleft}}
70 \newcaptionstyle{indent}{\caption@makecaption{indent}}

\captionstyle \captionstyle sets the actual caption style. It includes a check if the given caption style is defined or not.

71 \newcommand*\captionstyle[1]{%
72 \expandafter\ifx\csname caption@@#1\endcsname\relax
73  \PackageError{caption2}{Undefined caption style `#1'}{\caption@eh}%
74 \else
75  \def\caption@style{#1}%
76 \fi}

2.2 Options

normal center centerlast,anne flushleft flushright hang,isu indent These options will set the caption style. (‘normal’ is the default one.) The options ‘anne’ and ‘isu’ are for backward compatibility only.

77 \DeclareOption{normal}{\captionstyle{normal}}
78 \DeclareOption{center}{\captionstyle{center}}
79 \DeclareOption{centerlast}{\captionstyle{centerlast}}
80 \DeclareOption{flushleft}{\captionstyle{flushleft}}
81 \DeclareOption{flushright}{\captionstyle{flushright}}
82 \DeclareOption{anne}{\ExecuteOptions{centerlast}}
83 \DeclareOption{hang}{\captionstyle{hang}}
84 \DeclareOption{hang+center}{\captionstyle{hang+center}}
85 \DeclareOption{hang+centerlast}{\captionstyle{hang+centerlast}}
86 \DeclareOption{hang+flushleft}{\captionstyle{hang+flushleft}}
87 \DeclareOption{isu}{\ExecuteOptions{hang}}
88 \DeclareOption{indent}{\captionstyle{indent}}
scriptsize footnotesize small normalsize large,Large These options will set the caption size. We use \g@addto@macro so more that one

option can be set.

89 \DeclareOption{scriptsize}{\g@addto@macro\captionsize\scriptsize}
90 \DeclareOption{footnotesize}{\g@addto@macro\captionsize\footnotesize}
91 \DeclareOption{small}{\g@addto@macro\captionsize\small}
92 \DeclareOption{normalsize}{\g@addto@macro\captionsize\normalsize}
93 \DeclareOption{large}{\g@addto@macro\captionsize\large}
94 \DeclareOption{Large}{\g@addto@macro\captionsize\Large}

up,it,sl,sc md,bf rm,sf,tt These options will set the caption label.

95 \DeclareOption{up}{\g@addto@macro\captionlabelfont\upshape}
96 \DeclareOption{it}{\g@addto@macro\captionlabelfont\itshape}
97 \DeclareOption{sl}{\g@addto@macro\captionlabelfont\slshape}
98 \DeclareOption{sc}{\g@addto@macro\captionlabelfont\scshape}
99 \DeclareOption{md}{\g@addto@macro\captionlabelfont\mdseries}

100 \DeclareOption{bf}{\g@addto@macro\captionlabelfont\bfseries}
101 \DeclareOption{rm}{\g@addto@macro\captionlabelfont\rmfamily}
102 \DeclareOption{sf}{\g@addto@macro\captionlabelfont\sffamily}
103 \DeclareOption{tt}{\g@addto@macro\captionlabelfont\ttfamily}

oneline nooneline These options will set the ‘oneline’ flag. (‘oneline’ is the default.)

104 \DeclareOption{oneline}{\onelinecaptionstrue}
105 \DeclareOption{nooneline}{\onelinecaptionsfalse}

\caption@package A helper macro, a value of 1 within parameter #2 will activate the support of the package given in parameter #1, a value of 0 will deactivate it.

106 \newcommand*\caption@package[1]{\@namedef{caption@pkt@#1}}

float longtable subfigure These options will enable or suppress the support of the packages float, longtable, and subfigure.

107 \DeclareOption{float}{\caption@twozerofalse\caption@package{float}{1}}
108 \DeclareOption{longtable}{\caption@twozerofalse\caption@package{longtable}{1}}
109 \DeclareOption{subfigure}{\caption@twozerofalse\caption@package{subfigure}{1}}

{none \atop all} These options will enable or suppress the support of all the above packages.

110 \DeclareOption{none}{\caption@twozerofalse
111 \caption@package{float}{0}\caption@package{longtable}{0}%
112 \caption@package{subfigure}{0}}
113 \DeclareOption{all}{\ExecuteOptions{float,longtable,subfigure}}

ruled The option ‘ruled’ introduced in caption v1.2 is obsolete now, but we will still support it. (We will check it later with \@ifpackagewith.)

114 \DeclareOption{ruled}{}

ignoreLTcapwidth This option will make the caption code ignore the setting of \LTcapwidth and use

 the setting of \setcaptionmargin or \setcaptionwidth instead.
115 \DeclareOption{ignoreLTcapwidth}{\ignoreLTcapwidthtrue}

debug This option will put additional debug information in the log file.

116 \DeclareOption{debug}{\caption@debugtrue}

That’s it! Now set the default values and start processing the options. (If \caption@twozero is set to true (default) we will emulate the package load algorithm of caption v2.0: If the package is already loaded patch it, otherwise do nothing.)

117 \newif\ifcaption@debug
118 \newif\ifcaption@twozero
119 \normalcaptionparams
120 \ExecuteOptions{none,normal}
121 \caption@twozerotrue
122 \ProcessOptions*
123 \ifcaption@twozero
124 \PackageInfo{caption2}{Running in caption2 v2.0 compatibility mode}
125 \fi

2.3 More declarations

\captionof \captionof*  \captionof resp. \captionof* will just set \@captype and do the normal

\caption resp. \caption*, so we can also typeset captions outside floating environments.

126 \def\captionof{\@ifstar{\caption@of{\caption*}}{\caption@of\caption}}
127 \newcommand*\caption@of[2]{\def\@captype{#2}#1}
\abovecaptionskip \belowcaptionskip Not all document classes define \abovecaptionskip and \belowcaptionskip

(like ucthesis), so we do it here if not already done.

128 \@ifundefined{abovecaptionskip}{%
129 \newlength\abovecaptionskip\setlength\abovecaptionskip{10\p@}}{}
130 \@ifundefined{belowcaptionskip}{%
131 \newlength\belowcaptionskip\setlength\belowcaptionskip{0\p@}}{}
\captionlinewidth \captionlabel \captiontext These values are only set and used within the caption code itself. \captionlinewidth

will be set to the given vertical space for the caption, normally this is \linewidth. \captionlabel and \captiontext will be set to the caption label resp. the caption text. (Because \captionlabel and \captiontext will be locally defined with \def we do not need to define them here.)

132 \newdimen\captionlinewidth

\@makecaption This is the heart of the caption2 package – the redefinition of the core caption code. It was taken from the L^{A}TEX 2ε standard classes and modified. It’s very

easy – apart from using \abovecaptionskip and \belowcaptionskip we just set
\captionlinewidth, \captionlabel and \captiontext to its appropriate values

and using the code of the actual caption style via \usecaptionstyle.

133 \renewcommand\@makecaption[2]{%
134 \vskip\abovecaptionskip
135 \captionlinewidth\hsize
136 \def\captionlabel{#1}%
137 \def\captiontext{#2}%
138 \usecaptionstyle{\caption@style}%
139 \vskip\belowcaptionskip}

\usecaptionstyle First we check if we are inside a caption – if \captiontext is undefined we are not. If we are we call the appropriate caption definition.

140 \newcommand*\usecaptionstyle[1]{%
141 \ifx\captiontext\relax
142  \PackageError{caption2}{You can't use \protect#1
143  in normal text}{The usage of \protect#1 is only
144  allowed inside code declared with\MessageBreak \protect\defcaptionstyle,
145  \protect\newcaptionstyle \space or \protect\renewcaptionstyle.
146  \space\caption@eh}
147 \else
148  \@ifundefined{caption@@#1}%
149  {\PackageError{caption2}{Caption style `#1' undefined}{\caption@eh}}%
150  {\@nameuse{caption@@#1}}
151 \fi}

\caption@makecaption Our predefined caption styles. \caption@makecaption takes the style name as parameter, it does the common stuff and calls a macro (build out of the style name) to do the uncommon stuff if neccessary.

152 \newcommand*\caption@makecaption[1]{%
153 \ifcaptionlabel
154  \def\caption@label{{\captionlabelfont\captionlabel\captionlabeldelim}\captionlabelsep}%
155 \else
156  \let\caption@label\@empty
157 \fi
158 \usecaptionmargin\captionfont
159 \onelinecaption{\caption@label\captiontext}%
160  {\@nameuse{caption@@@#1}}}

\caption@@@normal The ‘normal’ caption style. Just typeset caption (label & text) as paragraph.

161 \newcommand*\caption@@@normal{%
162 \caption@label\captiontext\par}

\caption@@@center The ‘center’ caption style. Typeset the caption centered within a parbox.

163 \newcommand*\caption@@@center{%
164 \centering\caption@label\captiontext\par}%

\caption@@@centerlast The ‘centerlast’ caption style. The idea how to do this was taken from Brüggemann-Klein[6], it is also mentioned in Kopka[7, p227].

165 \newcommand*\caption@centerlast{%
166 \advance\leftskip by 0pt plus 1fil%
167 \advance\rightskip by 0pt plus -1fil%
168 \parfillskip0pt plus 2fil\relax}
169 \newcommand*\caption@@@centerlast{%
170 \caption@centerlast\caption@label\captiontext\par}

\caption@@@flushleft The ‘flushleft’ caption style. Typeset the caption raggedright within a parbox.

171 \newcommand*\caption@@@flushleft{%
172 \raggedright\caption@label\captiontext\par}%

\caption@@@flushright The ‘flushright’ caption style. Typeset the caption raggedleft within a parbox.

173 \newcommand*\caption@@@flushright{%
174 \raggedleft\caption@label\captiontext\par}%

\caption@@@hang \caption@hangplus The ‘hang’ caption style. This code was taken from The L^{A}TEX Companion[5, p155] and modified.

175 \newcommand*\caption@@@hang{%
176 \sbox\@tempboxa{\caption@label}%
177 \hangindent\wd\@tempboxa\noindent
178 \usebox\@tempboxa\caption@hangplus\captiontext\par}
179 \newcommand*\caption@hangplus{}

\caption@@@hang@center The ‘hang+flushleft’ caption style.

180 \newcommand*\caption@@@hang@center{%
181 \let\caption@hangplus\centering\caption@@@hang}

\caption@@@hang@centerlast The ‘hang+flushleft’ caption style.

182 \newcommand*\caption@@@hang@centerlast{%
183 \let\caption@hangplus\caption@centerlast\caption@@@hang}

\caption@@@hang@flushleft The ‘hang+flushleft’ caption style.

184 \newcommand*\caption@@@hang@flushleft{%
185 \let\caption@hangplus\raggedright\caption@@@hang}

\caption@@@indent The ‘indent’ caption style. Is is quite like the ‘hang’ style but the indention is

 given as \captionindent.
186 \newcommand*\caption@@@indent{%
187 \hangindent\captionindent\noindent
188 \caption@label\captiontext\par}

\onelinecaption This macro definition helps setting captions the L^{A}TEX base classes way: If

 \ifonelinecaptions is set and the 1st argument fits within \captionlinewidth,

we typeset it centered – otherway we typeset the 2nd argument. (We use the savebox \@tempboxa as helper for this.)

189 \newcommand\onelinecaption[1]{%
190 \let\next\@firstofone
191 \ifonelinecaptions
192  \sbox\@tempboxa{#1}%
193  \ifdim\wd\@tempboxa >\captionlinewidth
194  \else
195  \def\next{{\centering\usebox{\@tempboxa}\par}\@gobble}%
196  \fi
197 \fi\next}

\usecaptionmargin Another helper macro for caption style authors: It calculates \leftskip and

 \rightskip out of \captionlinewidth and \captionmargin resp. \captionwidth.

Also \captionlinewidth will be corrected to the appropriate value.

198 \newcommand*\usecaptionmargin{%
199 \ifcaptionwidth
200  \leftskip\captionlinewidth
201  \advance\leftskip by -\captionwidth
202  \divide\leftskip by 2
203  \rightskip\leftskip
204  \captionlinewidth\captionwidth
205 \else
206  \leftskip\captionmargin
207  \rightskip\captionmargin
208  \advance\captionlinewidth by -2\captionmargin
209 \fi}

2.4 Support of other packages

\caption@package This macro will execute the code needed to support the package named within argument #1. The parameter #2 is the command which shows if the package is loaded – it is defined, it is already loaded, otherwise not. The parameter #3 contains code which will be executed if no support is required – this is for cleanup purposes. The final parameter #4 contains the code itself.

210 \renewcommand*\caption@package[3]{%
211 \if1\@nameuse{caption@pkt@#1}%
212  \@ifundefined{#2}%
213  {\let\next\AtBeginDocument}%
214  {\let\next\@firstofone}%
215 \else
216  \ifcaption@twozero
217  \@ifundefined{#2}{#3\let\next\@gobble}{%
218  \PackageWarning{caption2}{%

219  The `#1' package will be supported without explicit option %
220  (v2.0 compatibility issue)}%
221  \let\next\@firstofone}%
222  \else
223  #3\let\next\@gobble
224  \fi
225 \fi
226 \expandafter\let\csname caption@pkt@#1\endcsname\undefined
227 \ifcaption@debug
228  \ifx\next\@gobble\PackageInfo{caption2}{#1 => gobble}%
229  \else\ifx\next\@firstofone\PackageInfo{caption2}{#1 => firstofone}%
230  \else\ifx\next\AtBeginDocument\PackageInfo{caption2}{#1 => AtBeginDocument}%
231  \else\PackageInfo{caption2}{#1 => ???}\fi\fi\fi
232 \fi
233 \next}

2.4.1 Support of the float package

234 \caption@package{float}{floatc@plain}{}{%
235 \ifx\floatc@plain\relax
236  \PackageWarning{caption2}{%
237  Option `float' was set but there is no float package loaded}
238 \else
239  \PackageInfo{caption2}{float package v1.2 (or newer) detected}

\caption@floatc First we define a helper macro to typeset the caption via \usecaption, the 1st parameter is the caption style name, the 2nd and 3rd are the caption label and text. caption2 has the goal not to modify the output just by loading it (without options), therefore we have to be tricky here to support \@fs@cfont which is in fact the

 same as our \captionlabelfont. So we test if a \captionlabelfont has been

set by the user – if not \@fs@cfont will be used, otherwise \captionlabelfont.

240  \newcommand\caption@floatc[3]{%
241  \ifx\captionlabelfont\@empty
242  \let\captionlabelfont\@fs@cfont
243  \fi
244  \captionlinewidth\hsize
245  \def\captionlabel{#2}%
246  \def\captiontext{#3}%
247  \usecaptionstyle{#1}}

floatc@plain Now we can redefine the caption code of the float package. Here we redefine \floatc@plain to use our caption code, so plain and boxed float types will use the actual caption style set by the user.

248  \renewcommand*\floatc@plain{\caption@floatc{\caption@style}}

floatc@ruled The support of the ruled float type is a little more complex. First we define a caption style ‘ruled’ so the end-user can change this caption style afterwards. If the (obsolete) option ‘ruled’ is set, we define it in a caption v1.x compatible way, otherwise we define it in a float compatible way. Then we redefine \floatc@ruled so the caption style ‘ruled’ will be used.

249  \@ifpackagewith{caption2}{ruled}{%
250  \dummycaptionstyle{ruled}{\onelinecaptionsfalse\setcaptionmargin{\z@}}%

251  }{%
252  \newcaptionstyle{ruled}{%
253  \ifcaptionlabel
254  {\@fs@cfont\captionlabel}\space%
255  \fi\captiontext\par}%

256 }

257  \renewcommand*\floatc@ruled{\caption@floatc{ruled}}

caption@of Typesetting captions outside floats is not so easy with redefined floats, because

• The caption code of the float package needs not only \@captype defined, but \@fs@capt (the command which will typeset the caption itself) either.

• The caption is only saved within a \vbox, so the float package can typeset the caption later at it’s float style specific place (that means at top or at the bottom of the float).

Here is the new code: First we check if it’s a restyled float by checking if \fst@<floattype> is defined. If yes, we use this command (it will define \@fs@capt). Then we execute \@float@setevery, if it exists (that means we are dealing with the float package 1.3 or newer here). Now comes the basic trick: We redefine the caption typesetting command \@fs@capt, so it will close the \vbox, typeset the caption outside the vbox and finally start the group again so the original \@fs@capt is happy with closing the group.

258  \renewcommand*\caption@of[2]{\def\@captype{#2}%
259  \@ifundefined{fst@#2}{}{%
260  \@nameuse{fst@#2}%
261  \@ifundefined{@float@setevery}{}{\@float@setevery{#2}}%
262  \let\caption@fs@capt\@fs@capt
263  \let\@fs@capt\caption@of@float}
264  #1}
265  \newcommand\caption@of@float[2]{\egroup
266  \vskip\abovecaptionskip
267  \normalsize\caption@fs@capt{#1}{#2}%
268  \vskip\belowcaptionskip
269  \bgroup}%
270 \fi}

2.4.2 Support of the longtable package

271 \caption@package{longtable}{LT@makecaption}{}{%
272 \ifx\LT@makecaption\relax
273  \PackageWarning{caption2}{%
274  Option `longtable' was set but there is no longtable package loaded}
275 \else
276  \PackageInfo{caption2}{longtable package v3.15 (or newer) detected}

\LT@makecaption David Carlisle was so kind to introduce a macro called \LT@makecaption in version 3.15 of the longtable package which typeset the caption and can be easily redefined. This is the original definition:

     \def\LT@makecaption#1#2#3{%
       \LT@mcol\LT@cols c{\hbox to\z@{\hss\parbox[t]\LTcapwidth{%

⟨typeset #1{#2: }#3 as caption⟩

          \endgraf\vskip\baselineskip}%
        \hss}}}

So we do here: First we define a new (dummy) caption style ‘longtable’, than we redefine \LT@makecaption so this style will be used. (Remember: #1 is \@gobble in star form of \caption, and \@firstofone otherwise.)

277  \dummycaptionstyle{longtable}{}
278  \renewcommand\LT@makecaption[3]{%
279  \LT@mcol\LT@cols c{\hbox to\z@{\hss\parbox[t]\hsize{%
280  \ifignoreLTcapwidth
281  \else
282  \setcaptionwidth\LTcapwidth
283  \fi
284  \captionlinewidth\hsize
285  \captionlabelfalse#1\captionlabeltrue
286  \def\captionlabel{#2}%
287  \def\captiontext{#3}%
288  \usecaptionstyle{longtable}%
289  \endgraf\vskip\baselineskip}%
290  \hss}}}
291 \fi}

2.4.3 Support of the subfigure package

Some of the following code will not work within \if, because of the (yet) undefined \ifxxxs. So we simply define the critical code within the helper commands

 \setsubcapstyle and \caption@makesubcaption already here.

\setsubcapstyle This sets the subcaptionstyle to a appropriate value. If \ifsubcapraggedright is undefined (it was introduced into v2.1 of the subfigure package) we define it first.

292 \newcommand*\setsubcapstyle{%
293 \@ifundefined{subcapraggedrightfalse}{%
294  \newif\ifsubcapraggedright}{}%
295 \ifsubcaphang
296  \ifsubcapcenter
297  \subcapstyle{hang+center}%
298  \else\ifsubcapcenterlast
299  \subcapstyle{hang+centerlast}%
300  \else\ifsubcapraggedright
301  \subcapstyle{hang+flushleft}%
302  \else
303  \subcapstyle{hang}%
304  \fi\fi\fi
305 \else\ifsubcapcenter
306  \subcapstyle{center}%
307 \else\ifsubcapcenterlast
308  \subcapstyle{centerlast}%
309 \else\ifsubcapraggedright
310  \subcapstyle{flushleft}%
311 \else

312  \subcapstyle{normal}%
313 \fi\fi\fi\fi}

\caption@makesubcaption This will typeset the subcaption. We just set all our \captionxxx values to the values of \subcapxxx and typeset the caption like subfigure within a \hbox, but

with the help of \usecaptionstyle.

But this is not as easy as it seems. We typeset the caption like this:

     \captionfont
       {\capionlabelfont\captionlabel\captionlabeldelim}%
     \captionlabelsep\captiontext

Within subfigure 2.0 the caption will be set quite similar to:

     \subcapsize
       {\subcaplabelfont\captionlabel}%
     \space\captiontext

But within subfigure 2.1 this has changed to:

     \subcapsize
       {\subcaplabelfont\captionlabel}%
     \hskip\subfiglabelskip
       {\subcapfont\captiontext}}

So we have to be tricky here: We set \captionlabelfont to \normalfont plus

\subcapsize & \subcaplabelfont, so the font setting in \captionfont will not

affect the caption label in subfigure captions. Note that \hfil has changed to \hss from subfigure 2.0 to 2.1, so we use \caption@subfig@hss instead. (We will define this later on.)

314 \newcommand\caption@makesubcaption[2]{%
315 \renewcommand*\captionfont{\subcapsize\subcapfont}%
316 \renewcommand*\captionlabelfont{\normalfont\subcapsize\subcaplabelfont}%
317 \let\captionlabeldelim\subcaplabeldelim
318 \let\captionlabelsep\subcaplabelsep
319 \ifsubfigcapwidth\captionwidthtrue\else\captionwidthfalse\fi
320 \setlength\captionmargin\subfigcapmargin
321 \setlength\captionwidth\subfigcapwidth
322 \captionindent\subcapindent
323 \ifsubcapnooneline\onelinecaptionsfalse\else\onelinecaptionstrue\fi
324 \hbox to\@tempdima{%
325  \caption@subfig@hss\parbox[t]{\@tempdima}{%
326  \captionlinewidth\@tempdima
327  \captionlabeltrue
328  \def\captionlabel{#1}%
329  \def\captiontext{\ignorespaces #2}%
330  \usecaptionstyle{\caption@substyle}}%
331  \caption@subfig@hss}}

If the subfigure support is not needed, we throw the helper macros in the garbage can.

332 \caption@package{subfigure}{@makesubfigurecaption}{%
333 \let\setsubcapstyle\undefined
334 \let\caption@makesubcaption\undefined}{%

335 \ifx\@makesubfigurecaption\relax
336  \PackageWarning{caption2}{%
337  Option `subfigure' was set but there is no subfigure package loaded}
338  \let\setsubcapstyle\undefined
339  \let\caption@makesubcaption\undefined
340 \else

Some stuff has changed from version 2.0 to 2.1 of the subfigure package, so we make a branch here. If \subcapfont is undefined we assume v2.0, otherwise we assume v2.1 or newer.

341  \ifx\subcapfont\undefined
342  \PackageInfo{caption2}{subfigure package v2.0 detected}

\subcapfont We define \subcapfont here so we can use it later in common code for subfigure v2.0 and v2.1 (or newer).

343  \let\subcapfont\@empty
\subfigcapwidth \setsubcapmargin \setsubcapwidth Analogous to \captionwidth, \setcaptionmargin, and \setcaptionwidth we
define \subfigcapwidth, \setsubcapmargin, and \setsubcapwidth.

Note: \subfigcapmargin is a command in v2.0 of subfigure. So we make

\subfigcapwidth a command, too.
344  \newcommand*\subfigcapwidth{\z@}
345  \newcommand*\setsubcapmargin{%
346  \subfigcapwidthfalse
347  \renewcommand*\subfigcapmargin}
348  \newcommand*\setsubcapwidth{%
349  \subfigcapwidthtrue
350  \renewcommand*\subfigcapwidth}
\subcaplabelsep Analogous to \captionlabelsep we define \subcaplabelsep.
351  \newcommand*\subcaplabelsep{\space}

\caption@subfig@hss This will be uses within the caption code itself.

352  \let\caption@subfig@hss\hfil
353  \else
354  \PackageInfo{caption2}{subfigure package v2.1 (or newer) detected}
\subfigcapwidth \setsubcapmargin \setsubcapwidth Analogous to \captionwidth, \setcaptionmargin, and \setcaptionwidth we
define \subfigcapwidth, \setsubcapmargin, and \setsubcapwidth.

Note: \subfigcapmargin is a length in v2.1 of subfigure. So we make

\subfigcapwidth a length, too.
355  \newdimen\subfigcapwidth
356  \newcommand*\setsubcapmargin{%
357  \subfigcapwidthfalse
358  \setlength\subfigcapmargin}
359  \newcommand*\setsubcapwidth{%
360  \subfigcapwidthtrue
361  \setlength\subfigcapwidth}
\subcaplabelsep Analogous to \captionlabelsep we define \subcaplabelsep.
362  \newcommand*\subcaplabelsep{\hskip\subfiglabelskip}

\caption@subfig@hss This will be uses within the caption code itself.

363  \let\caption@subfig@hss\hss
364  \fi

Here starts the common code for subfigure v2.0 and v2.1.

\ifsubfigcapwidth \subcapindent \subcaplabeldelim Analogous to \ifcaptionwidth, \captionindent & \captionlabeldelim we de-
fine \ifsubfigcapwidth, \subcapindent & \subcaplabeldelim
365  \newif\ifsubfigcapwidth
366  \newdimen\subcapindent
367  \newcommand*\subcaplabeldelim{}
\subcapstyle Analogous to \captionstyle we define \subcapstyle and set it (via \setsubcapstyle)

to a appropriate value.

368  \newcommand*\subcapstyle[1]{%
369  \expandafter\ifx\csname caption@@#1\endcsname\relax
370  \PackageError{caption2}{Undefined caption style `#1'}{\caption@eh}%
371  \else
372  \def\caption@substyle{#1}%
373  \fi}
374  \setsubcapstyle
\@thesubfigure \@thesubtable The subfigure package makes use of \subcaplabelfont and \subfiglabelskip

within its \@thesubxxx macros. This is totally in contrast to the way the caption2 package handle these settings! So we redefine the \@thesubxxx to be just the plain label and nothing else.

375  \renewcommand*\@thesubfigure{\thesubfigure}
376  \renewcommand*\@thesubtable{\thesubtable}
\@makesubfigurecaption \@makesubtablecaption Now we are ready to redefine \@makesubfigurecaption.
377  \let\@makesubfigurecaption\caption@makesubcaption
378  \let\@makesubtablecaption\caption@makesubcaption
379 \fi}

That’s all folks!

380 \let\caption@package\undefined

References

[1] Anselm Lingnau: An Improved Environment for Floats, 2001/11/08

[2] David Carlisle: The longtable package, 2000/10/22

[3] Sebastian Rahtz and Leonor Barroca: A style option for rotated objects in L^{A}TEX, 1997/09/26

[4] Steven Douglas Cochran: The subfigure package, 2002/02/14

[5] Michel Goossens, Frank Mittelbach and Alexander Samarin: The L^{A}TEX Companion, Addison-Wesley, Reading, Massachusetts, 1994.

[6] Anne Brüggemann-Klein: Einführung in die Dokumentverarbeitung, B.G. Teubner, Stuttgart, 1989

[7] Helmut Kopka: L^{A}TEX– Erweiterungsmöglichkeiten, 3. überarbeitete Auflage, Addison-Wesley, Bonn, 1991