Debian Codegraph @

texmf-dist/source/latex/collectbox/collectbox.dtx

raw
1 % \iffalse meta-comment
2 %<=*COPYRIGHT>
3 %% Copyright (C) 2012-2022 by Martin Scharrer <[email protected]>
4 %% -------------------------------------------------------------------
5 %% This work may be distributed and/or modified under the
6 %% conditions of the LaTeX Project Public License, either version 1.3
7 %% of this license or (at your option) any later version.
8 %% The latest version of this license is in
9 %% http://www.latex-project.org/lppl.txt
10 %% and version 1.3 or later is part of all distributions of LaTeX
11 %% version 2005/12/01 or later.
12 %%
13 %% This work has the LPPL maintenance status `maintained'.
14 %%
15 %% The Current Maintainer of this work is Martin Scharrer.
16 %%
17 %% This work consists of the files collectbox.dtx and collectbox.ins
18 %% and the derived filebase collectbox.sty.
19 %%
20 %<=/COPYRIGHT>
21 % \fi
22 %
23 % \iffalse
24 %<*driver>
25 \ProvidesFile{collectbox.dtx}[%
26 %<=*DATE>
27 2022/10/17
28 %<=/DATE>
29 %<=*VERSION>
30 v0.4c
31 %<=/VERSION>
32 DTX File of collectbox]
33 \documentclass{ydoc}
34 \GetFileInfo{collectbox.dtx}
35 \usepackage{collectbox}[\filedate]
36 \EnableCrossrefs
37 \CodelineIndex
38 \RecordChanges
39 \OnlyDescription
40 \begin{document}
41 \DocInput{collectbox.dtx}
42 \PrintChanges
43 \PrintIndex
44 \end{document}
45 %</driver>
46 % \fi
47 %
48 % \CheckSum{465}
49 %
50 % \CharacterTable
51 % {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
52 % 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
53 % Digits \0\1\2\3\4\5\6\7\8\9
54 % Exclamation \! Double quote \" Hash (number) \#
55 % Dollar \$ Percent \% Ampersand \&
56 % Acute accent \' Left paren \( Right paren \)
57 % Asterisk \* Plus \+ Comma \,
58 % Minus \- Point \. Solidus \/
59 % Colon \: Semicolon \; Less than \<
60 % Equals \= Greater than \> Question mark \?
61 % Commercial at \@ Left bracket \[ Backslash \\
62 % Right bracket \] Circumflex \^ Underscore \_
63 % Grave accent \` Left brace \{ Vertical bar \|
64 % Right brace \} Tilde \~}
65 %
66 %
67 % \changes{v0.1}{2011/07/18}{First release.}
68 % \changes{v0.2}{2011/07/20}{Added \cs{collectboxto} and \cs{@collectboxto} macros.}
69 % \changes{v0.2}{2011/07/23}{Added \cs{collectboxcheckenv}.}
70 % \changes{v0.2a}{2011/07/30}{Small bugfix.}
71 % \changes{v0.3}{2011/08/06}{Some internal modifications required by related packages.}
72 % \changes{v0.3a}{2011/08/11}{Fixed issue with collectbox macros in the end-code of a collectbox environment.}
73 % \changes{v0.4}{2011/08/23}{In environment-mode the begin and end are now placed directly around the content to allow tabular-like environments.
74 % Added support for tabular-like environments in macro-mode.}
75 % \changes{v0.4a}{2012/05/13}{Made use of \cs{noindent} by the environment version optional.}
76 % \changes{v0.4b}{2012/05/17}{Added \cs{@Collectbox} macro.}
77 % \changes{v0.4c}{2022/10/17}{Updated repository URL and email address.}
78 %
79 %
80 % \DoNotIndex{\newcommand,\newenvironment}
81 %
82 % \GetFileInfo{collectbox.dtx}
83 % \author{Martin Scharrer}
84 % \email{[email protected]}
85 % \repository{https://github.com/MartinScharrer/collectbox}
86 % \maketitle
87 %
88 % \begin{abstract}
89 % This package provides macros to collect and process an macro argument (i.e.\ something which looks like a macro argument)
90 % as horizontal box instead as a real macro argument.
91 % These ``arguments'' will be stored like when using \Macro\savebox, \Macro\sbox or the \env{lrbox} environment
92 % and allow verbatim or other special code.
93 % Instead of explicit braces also implicit braces in the form of \Macro\bgroup
94 % and \Macro\egroup are supported. This allows to split the begin and end over different macros or to place them
95 % in the begin and end code of an environment.
96 % The provided macros are mainly intended to be used inside other macros or environments.
97 % \end{abstract}
98 %
99 % \section{Quick overview}
100 % The following macros are provided for users (document authors) and \LaTeX\ package authors/programmers and described in the following sections.
101 % The \MacroArgs* and \MacroArgs[<\ldots>] arguments are as always optional. The \MacroArgs{<box content>} can also be written as
102 % \Macro\bgroup<box content>\AlsoMacro\egroup and be split across macro boundaries.
103 %
104 % \begingroup
105 % \descindent=\parindent
106 % \DescribeMacro\collectbox*[<code at begin>]{<code>}[<code at end>]{<box content>}
107 % \DescribeMacro\collectboxto{<box register>}{<code>}{<box content>}
108 % \DescribeMacro\collectboxcheckenv{<name>}
109 %
110 % \DescribeMacro\collectbox@{<code at begin>}{<code>}{<code at end>}{<box content>}
111 % \DescribeMacro\@collectbox{<code>}{<box content>}
112 % \DescribeMacro\@Collectbox{<code>}{<content>}
113 % \DescribeMacro\@collectboxto{<box register>}{<token>}{<box content>}
114 % \DescribeMacro\nocollectbox@{<code at begin>}{<code>}{<code at end>}
115 % \endgroup
116 %
117 % \section{Dependencies, Compatibility and Installation}
118 % This package does not depend on any other \LaTeX\ package or class.
119 % It should be compatible with all versions of \LaTeX (DVI-\LaTeX, pdf\LaTeX, Xe\LaTeX\ and Lua\LaTeX).
120 % Colored content will be correctly handled.
121 %
122 % This package should be (soon) part of the standard distributions TeXLive and MikTeX and can be installed over the
123 % provided package manager (i.e.\ with TeXLive: |tlmgr install collectcell|).
124 % This package can be also manually unpacked from |collectbox.dtx| by compiling the file |collectbox.ins| with \LaTeX\ or \TeX.
125 % This documentation can be created by compiling |collectbox.dtx| using pdf\LaTeX\ in DVI or PDF mode.
126 % The unpackaged package file |collectbox.sty| should be copied to a newly created directory named |$TEXMF/tex/latex/collectbox/| under Linux
127 % or |%TEXMF%\tex\latex\collectbox\| under MS Windows where |$TEXMF| and |%TEXMF%| represents the local \TeX\ tree.
128 % The documentation and README file can be copied to |$TEXMF/doc/latex/collectbox/|.
129 % Some \TeX\ distributions require to update the list of files in the \TeX\ tree, e.g.\ by running |texmf $TEXMF| afterwards.
130 % MikTeX users can use the graphical interface of the package manager.
131 %
132 %
133 % \section{User Interface}
134 % The following macros are provided on the user level.
135 %
136 % \subsection{Macros to collect boxes}
137 % \DescribeMacro\collectbox{<code>}{<box content>}
138 % \DescribeMacro\collectbox{<code>}\AlsoMacro\bgroup<box content>\AlsoMacro\egroup
139 % In its basic form this macro is written as \Macro\collectbox{<code>} and collects the following `group' in explicit (|{ .. }|) or implicit (|\bgroup .. \egroup|) braces
140 % as box (here represented as \MacroArgs{<box content>}). Afterwards the user provided \MacroArgs<code> is executed. This code is processed inside an internal group
141 % and has access to the just collected content using \Macro\BOXCONTENT and other macros described further below. Usually the code does some calculations and/or modifications
142 % on the collected box and then typesets it using \Macro\BOXCONTENT.
143 %
144 % An example is
145 % \codeline{\Macro\collectbox{\AlsoMacro\fbox{\AlsoMacro\BOXCONTENT}}'{\verb+verbatim stuff \space\empty+}'}
146 % which results in
147 % \codeline{\collectbox{\fbox{\BOXCONTENT}}{\verb+verbatim stuff \space\empty+}}
148 %
149 % \DescribeMacro\collectbox*{<code>}{<box content>}
150 % \DescribeMacro\collectbox*{<code>}\AlsoMacro\bgroup<box content>\AlsoMacro\egroup
151 % Because very often the \Macro\BOXCONTENT is simply fed as argument to a macro at the end of the \MacroArgs<code> a star version exists which adds \MacroArgs{\AlsoMacro\BOXCONTENT} automatically
152 % to the end of the code.
153 %
154 % An above example can be therefore simplified as:
155 % \codeline{\Macro\collectbox*{\AlsoMacro\fbox}'{\verb+verbatim stuff \space\empty+}'}
156 % which results in:
157 % \codeline{\collectbox*{\fbox}{\verb+verbatim stuff \space\empty+}}
158 %
159 % \noindent
160 % Using this macro a \Macro\fbox variant can be defined which reads its content as real box and not as macro argument:
161 % \codeline{\Macro\newcommand{\AlsoMacro\Fbox}{\AlsoMacro\collectbox*{\AlsoMacro\fbox}}}
162 %
163 % \DescribeMacro\collectbox [<code at begin>]{<code>}[<code at end>]{<box content>}
164 % \DescribeMacro\collectbox [<code at begin>]{<code>}[<code at end>]\AlsoMacro\bgroup<box content>\AlsoMacro\egroup
165 % \DescribeMacro\collectbox*[<code at begin>]{<code>}[<code at end>]{<box content>}
166 % \DescribeMacro\collectbox*[<code at begin>]{<code>}[<code at end>]\AlsoMacro\bgroup<box content>\AlsoMacro\egroup
167 % Finally two optional arguments exists which allow the placement of further code at the beginning and end of the collected box.
168 % This code is part of the box and is expanded before the main \MacroArgs<code> which is expanded after the box is fully collected.
169 % In other words \Macro\collectbox!\optionaloff! [<code at begin>]{<code>}[<code at end>]{<box content>} is basically the same as
170 % \Macro\collectbox{<code>}{<code at begin><.><box content><.><code at end>}, with the difference that the first form allows an user defined macro
171 % to set the begin and end code while the box content is provided by the user.
172 % Note that there is also some internal code \meta{.} between \meta{code at begin} and \meta{box content} as well as between \meta{box content} and \meta{code at end}.
173 %
174 % \paragraph{Example:} \leavevmode\Macro\fbox like macro which reads its ``argument'' as box and sets it green first:
175 % \begin{macroquote}
176 % \newcommand!\optionaloff!{\AlsoMacro\GFbox}{\AlsoMacro\collectbox*[\AlsoMacro\color{green}]{\AlsoMacro\fbox}}\\
177 % \GFbox{'test $a=4$ \verb|\relax|'}
178 % \end{macroquote}
179 % will result in:
180 % {\newcommand{\GFbox}{\collectbox*[\color{green}]{\fbox}}%
181 % \GFbox{test $a=4$ \verb|\relax|}}
182 %
183 %
184 % \DescribeMacro\collectedbox
185 % This macro represents the box register defined by \Macro\newsavebox\relax which holds the collected box.
186 % It can be used with the \LaTeX's macro \Macro\usebox{<box register>} or with plain\TeX\
187 % macros like \Macro\box, \Macro\copy, \Macro\unhbox or \Macro\unhcopy.
188 %
189 % \DescribeMacro\BOXCONTENT
190 % This macro is short for \Macro\usebox{\AlsoMacro\collectedbox} and will place the collected box into the document. It can be used multiple times inside \MacroArgs<code>.
191 %
192 % \DescribeMacro\width
193 % \DescribeMacro\height
194 % \DescribeMacro\depth
195 % \DescribeMacro\totalheight
196 % These macros represent the dimension of the collected box and can be used inside \MacroArgs<code>\relax. Here \Macro\height is the height of the box above the baseline
197 % and \Macro\depth the lengths how far the box is going below the baseline. Both values added together are provides as \Macro\totalheight.
198 % The box width is given by \Macro\width.
199 %
200 %
201 % \DescribeMacro\collectboxto{<box register>}[<code at begin>]{<code>}[<code at end>]{<box content>}
202 % \DescribeMacro\collectboxto{<box register>}[<code at begin>]{<code>}[<code at end>]\AlsoMacro\bgroup<box content>\AlsoMacro\egroup
203 % This macro collects its last ``argument'' as horizontal box and stores into into the user provided \meta{box register}.
204 % The \meta{code at begin} and \meta{code at end} are executed at begin and end of the \meta{box content} as part of the box.
205 % Afterwards the user provided \meta{code} is executed. In contrast to \Macro\collectbox no group is added for both the box register assignment or the code
206 % and also none of the above auxiliary macros for the box dimensions and content can be used. The collected box can be typeset using \Macro\usebox{<box register>}
207 % and its width, height and depth dimensions can be accessed using the \TeX\ primitives \Macro\wd<box register>, \Macro\ht<box register>, \Macro\dp<box register>.
208 %
209 % Appropriate grouping must be added manually if the overall code should be kept local. Proper places to open and close the group is somewhere before
210 % the \Macro\collectboxto macro and in the \meta{code} after the box was used, respectively.
211 % The box register must be declared beforehand using \Macro\newsavebox\relax (\LaTeX) or \Macro\newbox (\TeX).
212 % The provided box register \Macro\collectedbox can be used here but will be overwritten by further usages of most of the macros of this package.
213 % The \LaTeX\ code also provides \Macro\@tempboxa for temporary usages.
214 %
215 % This macro can be used to create macros which collect more than one argument as box. For this two box registers are required and the \meta{code} of the
216 % first \Macro\collectboxto usage must call it second time. The second \meta{code} then typesets the two boxes.
217 %
218 % \subsection{Support for environments}
219 %
220 % \DescribeMacro\collectboxcheckenv{<name>}
221 % This macro allows macros which use \Macro\collectbox to also be used as an environment.
222 % For this \Macro\collectboxcheckenv must be used before \Macro\collectbox with the macro name as argument.
223 % It then detects if the macro is used by itself or is called by \Macro\begin{<name>} and adjusts the internal configuration
224 % accordantly. In macro-mode it does not change anything, but in environment-mode a \Macro\begingroup is used to keep the configuration changes local.
225 % The corresponding \Macro\end<name> macro need to close then both the box group using \Macro\egroup and then close the outer group \Macro\engroup.
226 % A suitable version is automatically defined if this macro doesn't exists yet.
227 % In environment-mode starting and trailing spaces are ignored which is also the normal behaviour of similar environments like \env{minipage}.
228 %
229 % \paragraph{Example:} Defines \Macro\foobar macro which can also be used as \env{foobar} environment.
230 % \begin{codequote}
231 % \AlsoMacro\newcommand'*'{\Macro\foobar}'{%'\\
232 % !\mbox{}!~~~~\AlsoMacro\collectboxcheckenv{foobar}'%'\\
233 % !\mbox{}!~~~~\AlsoMacro\collectbox'*'{\Macro\fbox}'%'\\
234 % '}'%
235 % \end{codequote}
236 %
237 %
238 % \section{Programmers Interface}
239 %
240 % For the package author/programmer and more advanced users the following internal macros are provided.
241 % If used inside a document file they must be wrapped inside \Macro\makeatletter and \Macro\makeatother\relax (outside the macro which uses them).
242 %
243 % \DescribeMacro\collectbox@{<code at begin>}{<code>}{<code at end>}{<box content>}
244 % This macro is the internal form of \Macro\collectbox with the optional arguments replaced by mandatory ones.
245 % The \Macro\collectbox macro itself uses it after checking for and reading the optional arguments.
246 % If this macro is to be used in other macros this overhead can be avoided by using the internal form directly.
247 % Not used optional arguments can be simply kept empty. The star version is not supported but can be easily substituted by
248 % manually placing the \MacroArgs{\Macro\BOXCONTENT} at the end of \MacroArgs<code>.
249 %
250 % The above example can therefore be written in the faster processed form:
251 % \begin{macroquote}
252 % \makeatletter\\
253 % \newcommand!\optionaloff!{\AlsoMacro\GFbox}{\AlsoMacro\collectbox@{\AlsoMacro\color{green}}{\AlsoMacro\fbox}{}}\\
254 % \makeatother\\
255 % \GFbox{test $a=4$ \verb|\relax|}%
256 % \end{macroquote}
257 % which results in:
258 % {\makeatletter
259 % \newcommand{\GFbox}{\collectbox@{\color{green}}{\fbox{\BOXCONTENT}}{}}%
260 % \makeatother
261 % \GFbox{test $a=4$ \verb|\relax|}%
262 % }
263 %
264 %
265 % \DescribeMacro\@collectbox{<code>}{<box content>}
266 % This macro is a short version of \Macro\collectbox and only accepts one argument \MacroArgs<code> besides the later \meta{box content}.
267 % It is intended for quick version for macros which do not need to insert code at begin or after the box content.
268 %
269 % \DescribeMacro\@Collectbox{<code>}{<content>}
270 % This macro is similar to \Macro\@collectbox but reads the content as macro argument and
271 % not as a box. This is more efficient but does not allow for special content like verbatim text.
272 % This macro is indented for applications when an already boxed but now modified content needs
273 % to be boxed again, like it is done by \pkg{adjustbox}.
274 %
275 %
276 % \DescribeMacro\collectboxto@{<code at begin>}{<code>}{<code at end>}{<box content>}
277 % This macro is the internal form of \Macro\collectboxto with the optional arguments replaced by mandatory ones.
278 % The \Macro\collectboxto macro itself uses it after checking for and reading the optional arguments.
279 % If this macro is to be used in other macros this overhead can be avoided by using the internal form directly.
280 % Not used optional arguments can be simply kept empty.
281 %
282 % \DescribeMacro\@collectboxto{<box register>}{<code>}{<box content>}
283 % This macro is a short version of \Macro\collectboxto and only accepts one \meta{code} argument.
284 % It is intended for quick version for macros which do not need to insert code at begin or after the box content.
285 %
286 % \DescribeMacro\nocollectbox@{<code at begin>}{<code>}{<code at end>}
287 % Turns a following brace group into basically \Macro\hbox{<code at begin><content><code at end>}<code>.
288 % This is useful if a box needs to be build like that but no actual processing of the content is required, e.g.\ for horizontal alignment only.
289 % Note that there are some internal code between the three code arguments.
290 %
291 %
292 % \StopEventually{}
293 %
294 % \section{Implementation}
295 %
296 % \iffalse
297 %<*collectbox.sty>
298 % \fi
299 % \begin{macrocode}
300 %<!COPYRIGHT>
301 \NeedsTeXFormat{LaTeX2e}[1999/12/01]
302 \ProvidesPackage{collectbox}[%
303 %<!DATE>
304 %<!VERSION>
305 %<*DRIVER>
306 2099/01/01 develop
307 %</DRIVER>
308 Collect macro arguments as boxes]
309 % \end{macrocode}
310 %
311 % \begin{macro}{\collectedbox}
312 % Box register used to store collected box.
313 % \begin{macrocode}
314 \newsavebox\collectedbox
315 % \end{macrocode}
316 % \end{macro}
317 %
318 % \begin{macro}{\collectbox}
319 % User level interface. Scans for potential star and optional arguments.
320 % \begin{macrocode}
321 \newcommand*\collectbox{%
322 \@ifstar
323 {\collectbox@a{{\BOXCONTENT}}}%
324 {\collectbox@a{}}%
325 }
326 % \end{macrocode}
327 % \end{macro}
328 %
329 %
330 % \begin{macro}{\collectbox@a}
331 % \begin{macrocode}
332 \long\def\collectbox@a#1{%
333 \@ifnextchar[%
334 {\collectbox@b{#1}}%
335 {\collectbox@b{#1}[]}%
336 }
337 % \end{macrocode}
338 % \end{macro}
339 %
340 %
341 % \begin{macro}{\collectbox@b}
342 % \begin{macrocode}
343 \long\def\collectbox@b#1[#2]#3{%
344 \@ifnextchar[%
345 {\collectbox@c{#2}{#3#1}}%
346 {\collectbox@c{#2}{#3#1}[]}%
347 }
348 % \end{macrocode}
349 % \end{macro}
350 %
351 %
352 % \begin{macro}{\collectbox@c}
353 % \begin{macrocode}
354 \long\def\collectbox@c#1#2[#3]{%
355 \collectbox@{#1}{#2}{#3}%
356 }
357 % \end{macrocode}
358 % \end{macro}
359 %
360 % \begin{macro}{\collect@box}
361 % The box primitive used. Might be locally redefined to \Macro\vbox if required.
362 % \begin{macrocode}
363 \let\collect@box\hbox
364 % \end{macrocode}
365 % \end{macro}
366 %
367 % \begin{macro}{\collectbox@mode}
368 %
369 % \begin{macrocode}
370 \let\collectbox@mode\leavevmode
371 % \end{macrocode}
372 % \end{macro}
373 %
374 % \begin{macro}{\collectbox@}
375 % The box is saved using |\setbox|. Color is supported by using |\collectbox@setgroup|.
376 % A brace group is then opened using |\bgroup| which will replace the opening brace of the
377 % ``argument''. The end code is installed in this group using |\aftergroup|.
378 % The opening brace written by the user is then removed by assigning it as token to a dummy variable.
379 % If no |{| or |\bgroup| follows the next token is taken as sole content.
380 % \begin{macrocode}
381 \newcommand\collectbox@[3]{%
382 \begingroup
383 \collectbox@mode
384 \@temptokena{#3\collectbox@end#2\endgroup}%
385 \setbox\collectedbox\collect@box\bgroup
386 \collectbox@setgroup
387 #1\bgroup
388 \aftergroup\the
389 \aftergroup\@temptokena
390 \collectbox@bgroup
391 }
392 \let\collectbox@macro\collectbox@
393 % \end{macrocode}
394 % \end{macro}
395 %
396 % \begin{macro}{\collectbox@env}
397 % \begin{macrocode}
398 \newcommand\collectbox@env[3]{%
399 \collectbox@mode
400 \def\collectboxenvend{#3\collectbox@end#2\collectbox@currenvir}%
401 \setbox\collectedbox\collect@box\bgroup
402 \collectbox@setgroup
403 \collectbox@reset
404 \ignorespaces
405 #1%
406 }
407 % \end{macrocode}
408 % \end{macro}
409 %
410 %
411 % \begin{macro}{\collectbox@currenenvir}
412 % \begin{macrocode}
413 \def\collectbox@currenvir{%
414 \edef\@currenvir{\@currenvir}%
415 }
416 % \end{macrocode}
417 % \end{macro}
418 %
419 %
420 % \begin{macro}{\collectbox@bgroup}
421 % \begin{macrocode}
422 \def\collectbox@macro@bgroup{%
423 \@ifnextchar\bgroup
424 {\let\@let@token=}%
425 {\collectbox@arg}%
426 }
427 % \end{macrocode}
428 % \end{macro}
429 %
430 % \begin{macro}{\collectbox@env@bgroup}
431 % \begin{macrocode}
432 \def\collectbox@env@bgroup{%
433 \collectbox@reset
434 \ignorespaces
435 }
436 % \end{macrocode}
437 % \end{macro}
438 %
439 % \begin{macrocode}
440 \let\collectbox@bgroup\collectbox@macro@bgroup
441 % \end{macrocode}
442 %
443 %
444 % \begin{macro}{\collectbox@setgroup}
445 % Used at the very begin of the stored box.
446 % \begin{macrocode}
447 \def\collectbox@setgroup{%
448 \color@setgroup
449 }
450 % \end{macrocode}
451 % \end{macro}
452 %
453 %
454 % \begin{macro}{\collectbox@endgroup}
455 % Used at the very end of the stored box.
456 % \begin{macrocode}
457 \def\collectbox@endgroup{%
458 \collectbox@endcode
459 \color@endgroup
460 }
461 \let\collectbox@endcode\relax
462 % \end{macrocode}
463 % \end{macro}
464 %
465 %
466 % \begin{macro}{\@collectbox}
467 % Simpler and quicker version of \Macro\collectbox.
468 % \begin{macrocode}
469 \newcommand\@collectbox[1]{%
470 \begingroup
471 \collectbox@mode
472 \@temptokena{\collectbox@end#1\endgroup}%
473 \setbox\collectedbox\collect@box\bgroup
474 \collectbox@setgroup\bgroup
475 \aftergroup\the
476 \aftergroup\@temptokena
477 \collectbox@bgroup
478 }
479 % \end{macrocode}
480 % \end{macro}
481 %
482 %
483 % \begin{macro}{\@Collectbox}
484 % Specialised version of \Macro\@collectbox which
485 % reads the content as macro argument.
486 % This form should (only) be used if reading the content
487 % as box is not required, i.e.\ if the content
488 % was already boxed before and is now boxed again,
489 % as it is done by e.g.\ \pkg{adjustbox}.
490 % \begin{macrocode}
491 \newcommand\@Collectbox[2]{%
492 \begingroup
493 \collectbox@mode
494 \sbox\collectedbox{#2}%
495 \collectbox@setdims
496 #1%
497 \endgroup
498 }
499 % \end{macrocode}
500 % \end{macro}
501 %
502 %
503 %
504 % \begin{macro}{\collectbox@arg}
505 % Supports |\collectbox{<code>}\somemacro| in addition to
506 % |\collectbox{<code>}{\somemacro}|. The first following token is read and the inner brace group is closed
507 % to trigger the |\aftergroup| code.
508 % \begin{macrocode}
509 \def\collectbox@arg#1{%
510 #1\egroup
511 }
512 % \end{macrocode}
513 % \end{macro}
514 %
515 %
516 % \begin{macro}{\collectbox@end}
517 % The color group is first closed, followed by the closing brace of the |\setbox| command.
518 % The user dimension macros are then defined.
519 % Direct after this code the users code is processed and concluded with a |\endgroup|.
520 % \begin{macrocode}
521 \def\collectbox@end{%
522 \collectbox@endgroup
523 \egroup
524 \collectbox@setdims
525 \ifcollectboxenv
526 \collectbox@reset
527 \collectbox@currenvir
528 \fi
529 }
530 % \end{macrocode}
531 % \end{macro}
532 %
533 %
534 % \begin{macro}{\collectbox@setdims}
535 % \begin{macrocode}
536 \def\collectbox@setdims{%
537 \collectbox@protecteddef\BOXCONTENT{\usebox\collectedbox}%
538 \collectbox@protecteddef\width{\wd\collectedbox}%
539 \collectbox@protecteddef\height{\ht\collectedbox}%
540 \collectbox@protecteddef\depth{\dp\collectedbox}%
541 \collectbox@protecteddef\totalheight{\@ovri}%
542 \totalheight\height
543 \advance\totalheight\depth
544 }%
545 % \end{macrocode}
546 % \end{macro}
547 %
548 %
549 % \begin{macro}{\collectbox@reset}
550 % \begin{macrocode}
551 \def\collectbox@reset{%
552 \let\collectbox@endcode\relax
553 \let\collectbox@bgroup\collectbox@macro@bgroup
554 \let\collectbox@\collectbox@macro
555 \collectboxenvfalse
556 }
557 % \end{macrocode}
558 % \end{macro}
559 %
560 %
561 % \begin{macro}{\collectbox@protecteddef}
562 % \begin{macrocode}
563 \begingroup
564 \expandafter\ifx\csname protected\endcsname\relax
565 \global\let\collectbox@protecteddef\def%
566 \else
567 \gdef\collectbox@protecteddef{\protected\def}%
568 \fi
569 \endgroup
570 % \end{macrocode}
571 % \end{macro}
572 %
573 %
574 % \begin{macro}{\collectbox@code}
575 % Global storage macro for user code.
576 % \begin{macrocode}
577 \let\collectbox@code\empty
578 % \end{macrocode}
579 % \end{macro}
580 %
581 %
582 % \begin{macro}{\collectboxto}
583 % User level interface. Reads box register and checks for optional argument.
584 % \begin{macrocode}
585 \newcommand\collectboxto[1]{%
586 \@ifnextchar[%
587 {\collectboxto@a{#1}}%
588 {\collectboxto@a{#1}[]}%
589 }
590 % \end{macrocode}
591 % \end{macro}
592 %
593 %
594 % \begin{macro}{\collectboxto@a}
595 % Reads first optional and next mandatory arguments.
596 % \begin{macrocode}
597 \long\def\collectboxto@a#1[#2]#3{%
598 \@ifnextchar[%
599 {\collectboxto@b{#1}{#2}{#3}}%
600 {\collectboxto@b{#1}{#2}{#3}[]}%
601 }
602 % \end{macrocode}
603 % \end{macro}
604 %
605 %
606 % \begin{macro}{\collectboxto@b}
607 % Reads second optional argument.
608 % \begin{macrocode}
609 \long\def\collectboxto@b#1#2#3[#4]{%
610 \collectboxto@{#1}{#2}{#3}{#4}%
611 }
612 % \end{macrocode}
613 % \end{macro}
614 %
615 %
616 % \begin{macro}{\collectboxto@}[4]{box register}{code at begin}{code}{code at end}
617 % Installs code and starts to collect the box.
618 % \begin{macrocode}
619 \newcommand\collectboxto@[4]{%
620 \collectbox@mode
621 \setbox#1\collect@box\bgroup
622 \def\collectbox@code{#4\collectbox@endgroup\egroup#3}%
623 \collectbox@setgroup#2\bgroup
624 \aftergroup\collectbox@code
625 \collectbox@bgroup
626 }
627 % \end{macrocode}
628 % \end{macro}
629 %
630 %
631 % \begin{macro}{\@collectboxto}
632 % Collects the next brace group as hbox into box register |#1| and executes token |#2| afterwards.
633 % \begin{macrocode}
634 \newcommand\@collectboxto[2]{%
635 \collectbox@mode
636 \setbox#1\collect@box\bgroup
637 \def\collectbox@code{\collectbox@endgroup\egroup#2}%
638 \collectbox@setgroup\bgroup
639 \aftergroup\collectbox@code
640 \collectbox@bgroup
641 }
642 % \end{macrocode}
643 % \end{macro}
644 %
645 %
646 % \begin{macro}{\nocollectbox@}[3]{<code at begin>}{<code>}{<code at end>}
647 % Turns a brace group into \Macro\hbox{<code at begin><content><code at end>}<code>.
648 % \begin{macrocode}
649 \newcommand\nocollectbox@[3]{%
650 \collectbox@mode
651 \collect@box\bgroup
652 \def\collectbox@code{#3\egroup#2}%
653 #1\bgroup
654 \aftergroup\collectbox@code
655 \collectbox@bgroup
656 }
657 % \end{macrocode}
658 % \end{macro}
659 %
660 %
661 % \begin{macro}{\nocollectbox@to}[4]{<length>}{<code at begin>}{<code>}{<code at end>}
662 % Turns a brace group into \Macro\hbox' to '<length>{<code at begin><content><code at end>}<code>.
663 % \begin{macrocode}
664 \newcommand\nocollectbox@to[4]{%
665 \collectbox@mode
666 \collect@box to #1\bgroup
667 \def\collectbox@code{#4\egroup#3}%
668 #2\bgroup
669 \aftergroup\collectbox@code
670 \collectbox@bgroup
671 }
672 % \end{macrocode}
673 % \end{macro}
674 %
675 %
676 % \begin{macro}{\collectboxcheckenv}
677 % Checks if current macro is used as environment given by |#1|.
678 % If so the |bgroup| eating macro is redefined.
679 % A suitable end-macro is provided if non exists. All of this is done inside
680 % a group to keep thing local.
681 % The \Macro\@currenvir macro is redefined to contain \Macro\empty so that
682 % macros with the same name can be used inside the environment without collisions.
683 % This requires an extra internal group so that this change is reverted before the
684 % \Macro\end macro.
685 % \begin{macrocode}
686 \newcommand*\collectboxcheckenv[1]{%
687 \begingroup
688 \def\@tempa{#1}%
689 \expandafter\endgroup
690 \ifx\@currenvir\@tempa
691 \@collectboxisenv{#1}%
692 \fi
693 }
694 \def\@collectboxisenv#1{%
695 \collectbox@noindent
696 \collectboxenvtrue
697 \edef\@currenvir{\@currenvir\noexpand\empty}%
698 \let\collectbox@bgroup\collectbox@env@bgroup
699 \let\collectbox@endcode\collectbox@env@endcode
700 \let\collectbox@\collectbox@env
701 \expandafter\ifx\csname end#1\endcsname\relax
702 \expandafter\let\csname end#1\endcsname\collectbox@envend
703 \fi
704 }
705 \newif\ifcollectboxenv
706 \let\collectbox@noindent\noindent
707 % \end{macrocode}
708 % \end{macro}
709 %
710 %
711 % \begin{macro}{\collectbox@env@endcode}
712 % \begin{macrocode}
713 \def\collectbox@env@endcode{\ifhmode\unskip\fi}%
714 % \end{macrocode}
715 % \end{macro}
716 %
717 %
718 % \begin{macro}{\collectbox@envend}
719 % \begin{macrocode}
720 \def\collectbox@envend{\collectboxenvend}%
721 % \end{macrocode}
722 % \end{macro}
723 %
724 %
725 % \begin{macro}{\collectboxenvend}
726 % \begin{macrocode}
727 \def\collectboxenvend{%
728 \egroup
729 \collectbox@currenvir
730 }%
731 % \end{macrocode}
732 % \end{macro}
733 %
734 % \subsection{Tabular collection}
735 %
736 % \begin{macro}{\collectboxtabular}
737 % \begin{macrocode}
738 \newcommand\collectboxtabular[3][c]{%
739 \collectbox@tab{tabular}{[#1]{#2}}{}{#3}{}%
740 }
741 % \end{macrocode}
742 % \end{macro}
743 %
744 %
745 % \begin{macro}{\collectboxtabularstar}
746 % \begin{macrocode}
747 \newcommand\collectboxtabularstar[4][c]{%
748 \collectbox@tab{tabular*}{[#1]{#2}{#3}}{}{#4}{}%
749 }
750 % \end{macrocode}
751 % \end{macro}
752 %
753 %
754 % \begin{macro}{\collectboxarray}
755 % \begin{macrocode}
756 \newcommand\collectboxarray[2]{%
757 \collectbox@tab{array}{{#1}}{\(}{#2}{\)}%
758 }
759 % \end{macrocode}
760 % \end{macro}
761 %
762 %
763 % \begin{macro}{\collectbox@checkarray}
764 % \begin{macrocode}
765 \def\collectbox@checkarray{%
766 \@ifpackageloaded{array}{%
767 \let\collectbox@realarraycr\@arraycr
768 \def\collectbox@setarraycr{%
769 \let\@arraycr\collebox@cr
770 \def\ialign{\let\@arraycr\collectbox@realarraycr \everycr {}\tabskip \z@skip \halign}%
771 }%
772 }{%
773 \let\collectbox@realarraycr\@tabularcr
774 \def\collectbox@setarraycr{%
775 \let\@tabularcr\collebox@cr
776 \def\ialign{\let\noexpand\@tabularcr\noexpand\collectbox@realarraycr \everycr {}\tabskip \z@skip \halign}%
777 }%
778 }%
779 }
780 % \end{macrocode}
781 % \end{macro}
782 %
783 % \begin{macrocode}
784 \collectbox@checkarray
785 \AtBeginDocument{\collectbox@checkarray}%
786 % \end{macrocode}
787 %
788 % \begin{macro}{\collectbox@dorealarraycr}
789 % \begin{macrocode}
790 \def\collectbox@dorealarraycr{%
791 \expandafter\collectbox@realarraycr\collectbox@aftercrstuff
792 }
793 % \end{macrocode}
794 % \end{macro}
795 %
796 % \begin{macrocode}
797 \let\collectbox@aftercrstuff\empty
798 % \end{macrocode}
799 %
800 % \begin{macro}{\collectbox@tab}
801 % \begin{macrocode}
802 \def\collectbox@tab#1#2#3#4#5{%
803 \begingroup
804 \collectbox@mode
805 \def\collectbox@codeafter{#4\endgroup}%
806 \def\collectbox@endtabenv{\end{#1}#5\collectbox@end}%
807 \def\collectbox@dotab{#3\begin{#1}#2}%
808 \setbox\collectedbox\collect@box\bgroup
809 \collectbox@setgroup
810 \collectbox@setarraycr
811 \afterassignment\collectbox@dotab
812 \let\@let@token=%
813 }
814 % \end{macrocode}
815 % \end{macro}
816 %
817 %
818 % \begin{macro}{\collebox@cr}
819 % \begin{macrocode}
820 \def\collebox@cr{%
821 \@ifstar\collebox@@cr\collebox@@cr%
822 }
823 % \end{macrocode}
824 % \end{macro}
825 %
826 %
827 % \begin{macro}{\collebox@@cr}
828 % \begin{macrocode}
829 \def\collebox@@cr{%
830 \@ifnextchar[%
831 \collebox@@cr@opt
832 {\collebox@@@cr{}}%
833 }
834 % \end{macrocode}
835 % \end{macro}
836 %
837 %
838 % \begin{macro}{\collebox@@cr@opt}
839 % \begin{macrocode}
840 \def\collebox@@cr@opt[#1]{%
841 \collebox@@@cr{[#1]}%
842 }
843 % \end{macrocode}
844 % \end{macro}
845 %
846 %
847 % \begin{macro}{\collebox@@@cr}
848 % \begin{macrocode}
849 \def\collebox@@@cr#1{%
850 \def\collectbox@aftercrstuff{#1}%
851 \expandafter\collebox@@@@cr
852 \romannumeral-`0%
853 }
854 % \end{macrocode}
855 % \end{macro}
856 %
857 %
858 % \begin{macro}{\collebox@@@@cr}
859 % \begin{macrocode}
860 \def\collebox@@@@cr{%
861 \@ifnextchar\egroup{\collectbox@dorealarraycr\collectbox@aftertab}{%
862 \ifx\@let@token\noalign
863 \expandafter\collectbox@handlenoalign
864 \else
865 \expandafter\collectbox@dorealarraycr
866 \fi
867 }%
868 }
869 % \end{macrocode}
870 % \end{macro}
871 %
872 %
873 % \begin{macro}{\collectbox@handlenoalign}
874 % \begin{macrocode}
875 \def\collectbox@handlenoalign#1#2{%
876 \collectbox@dorealarraycr
877 \noalign{\bgroup\aftergroup\collectbox@afternoalign#2}%
878 }
879 % \end{macrocode}
880 % \end{macro}
881 %
882 %
883 % \begin{macro}{\collectbox@handlenextnoalign}
884 % \begin{macrocode}
885 \def\collectbox@handlenextnoalign#1#2{%
886 \egroup
887 \noalign{\bgroup\aftergroup\collectbox@afternoalign#2}%
888 }
889 % \end{macrocode}
890 % \end{macro}
891 %
892 %
893 % \begin{macro}{\collectbox@afternoalign}
894 % \begin{macrocode}
895 \def\collectbox@afternoalign{%
896 \expandafter\collectbox@@afternoalign
897 \romannumeral-`0%
898 }
899 % \end{macrocode}
900 % \end{macro}
901 %
902 %
903 % \begin{macro}{\collectbox@@afternoalign}
904 % \begin{macrocode}
905 \def\collectbox@@afternoalign{%
906 \@ifnextchar\egroup{\egroup\collectbox@aftertab}{%
907 \ifx\@let@token\noalign
908 \expandafter\collectbox@handlenextnoalign
909 \else
910 \expandafter\egroup
911 \fi
912 }%
913 }
914 % \end{macrocode}
915 % \end{macro}
916 %
917 %
918 % \begin{macro}{\collectbox@aftertab}
919 % \begin{macrocode}
920 \def\collectbox@aftertab{%
921 \collectbox@endtabenv
922 \afterassignment\collectbox@codeafter
923 \let\@let@token=%
924 }
925 % \end{macrocode}
926 % \end{macro}
927 %
928 % \iffalse
929 %</collectbox.sty>
930 % \fi
931 %
932 % \Finale
933 % \endinput