|
@@ -78,7 +78,6 @@ and the derived files boxgrab.pdf and
|
78
|
78
|
\newcommand*\boxgrab@version{1.0}
|
79
|
79
|
\ProvidesPackage{boxgrab}
|
80
|
80
|
[\boxgrab@date\ v\boxgrab@version\ utilities to get an argument as a box]
|
81
|
|
-\RequirePackage{xparse}
|
82
|
81
|
%</pkg>
|
83
|
82
|
% \fi
|
84
|
83
|
%
|
|
@@ -116,43 +115,57 @@ and the derived files boxgrab.pdf and
|
116
|
115
|
% \marg{next}\oarg{save args}
|
117
|
116
|
% \end{syntax}
|
118
|
117
|
% Grabs the next braced argument inside of the box \meta{box register}. The
|
119
|
|
-% box is of \meta{box type}, which should be either \cs{hbox} or \cs{vbox}.
|
120
|
|
-% \meta{inject pre} will be injected at the beginning of the box and can
|
121
|
|
-% affect its contents, \meta{inject post} will be injected after the box but
|
122
|
|
-% can't be affected by stuff inside of \meta{inject pre}. Unless the \meta{*}
|
123
|
|
-% is given leading and trailing spaces will be stripped from the box. After
|
124
|
|
-% the box is read in \meta{next} will be executed. The usage is therefore
|
125
|
|
-% similar to the one of \cs{@ifstar}. You can specify arguments which should
|
126
|
|
-% be given to \meta{next} using \meta{save args}. The stuff you provide in
|
127
|
|
-% \meta{save args} will be given as is to \meta{next}, so if you want to give
|
128
|
|
-% an argument you should delimit it like with braces, like \verb|[{#1}]|, or
|
129
|
|
-% if you want to give two arguments, like \verb|[{#1}{#2}]|.
|
130
|
|
-%
|
131
|
|
-% All assignments are made local. Currently it is not save to nest macros
|
132
|
|
-% which use \cs{boxgrab}.
|
|
118
|
+% box is of \meta{box type}, which should be one of \cs{hbox} or \cs{vbox} or
|
|
119
|
+% \cs{vtop}. \meta{inject pre} will be injected at the beginning of the box
|
|
120
|
+% and can affect its contents, \meta{inject post} will be injected after the
|
|
121
|
+% box but can't be affected by stuff inside of \meta{inject pre}. Unless the
|
|
122
|
+% \meta{*} is given leading and trailing spaces will be stripped from the box.
|
|
123
|
+% After the box is read in \meta{next} will be executed. The usage is
|
|
124
|
+% therefore similar to the one of \cs{@ifstar}. You can specify arguments
|
|
125
|
+% which should be given to \meta{next} using \meta{save args}. The stuff you
|
|
126
|
+% provide in \meta{save args} will be given as is to \meta{next}, so if you
|
|
127
|
+% want to give an argument you should delimit it with braces, like
|
|
128
|
+% \verb|[{#1}]|, or if you want to give two arguments, like
|
|
129
|
+% \verb|[{#1}{#2}]|.
|
133
|
130
|
% \end{function}
|
134
|
131
|
%
|
|
132
|
+% \smallskip
|
|
133
|
+% All assignments are made local. Currently it is not safe to nest macros
|
|
134
|
+% which use \cs{boxgrab}. It should become save if you're macros use
|
|
135
|
+% \cs{boxgrab} inside of a group, so the local assignments don't affect the
|
|
136
|
+% outer macro.
|
|
137
|
+%
|
|
138
|
+% \cs{boxgrab} uses \cs{afterassignment} and \cs{aftergroup} to do its magic.
|
|
139
|
+% The \cs{afterassignment} should be safe where it is used, the
|
|
140
|
+% \cs{aftergroup} is used inside of the boxed argument before any contents are
|
|
141
|
+% inserted.
|
|
142
|
+%
|
|
143
|
+% Since \cs{boxgrab} works by setting a boxregister using \cs{setbox} (and a
|
|
144
|
+% bunch of temporary macros), it is of course not expandable and defined
|
|
145
|
+% \cs{protected}. Bear in mind that macros created with \cs{boxgrab} are
|
|
146
|
+% not expandable.
|
|
147
|
+%
|
135
|
148
|
% \section{Useless Example!}
|
136
|
149
|
%
|
137
|
150
|
% First we need to reserve us a box register for this example:
|
138
|
151
|
%
|
139
|
|
-%\begin{verbatim}
|
|
152
|
+% \begin{verbatim}
|
140
|
153
|
% \newsavebox\ourbox
|
141
|
|
-%\end{verbatim}
|
|
154
|
+% \end{verbatim}
|
142
|
155
|
%
|
143
|
156
|
% Next we define a macro which takes some arguments and uses our macro:
|
144
|
157
|
%
|
145
|
|
-%\begin{verbatim}
|
|
158
|
+% \begin{verbatim}
|
146
|
159
|
% \newcommand\examplecmd[2]
|
147
|
160
|
% {%
|
148
|
161
|
% \begingroup
|
149
|
162
|
% \boxgrab\ourbox[\itshape]\hbox[ \sffamily is]\examplecmdOut[{#1}{#2}]
|
150
|
163
|
% }
|
151
|
|
-%\end{verbatim}
|
|
164
|
+% \end{verbatim}
|
152
|
165
|
%
|
153
|
166
|
% And we need our helper macro which is executed after \cs{boxgrab}:
|
154
|
167
|
%
|
155
|
|
-%\begin{verbatim}
|
|
168
|
+% \begin{verbatim}
|
156
|
169
|
% \newcommand\examplecmdOut[3]
|
157
|
170
|
% {%
|
158
|
171
|
% \begin{tabular}[t]{@{}ll@{}}
|
|
@@ -163,7 +176,7 @@ and the derived files boxgrab.pdf and
|
163
|
176
|
% \end{tabular}%
|
164
|
177
|
% \endgroup
|
165
|
178
|
% }
|
166
|
|
-%\end{verbatim}
|
|
179
|
+% \end{verbatim}
|
167
|
180
|
%
|
168
|
181
|
% The result is a macro that takes two ordinary arguments, after those a
|
169
|
182
|
% box in horizontal mode and finally another ordinary argument. If we use this
|
|
@@ -187,20 +200,20 @@ and the derived files boxgrab.pdf and
|
187
|
200
|
% }%
|
188
|
201
|
% \examplecmd{Hi,}{my}{\verb|name|}{Steve!}
|
189
|
202
|
% \end{center}
|
190
|
|
-% One can see, that the \verb|\sffamily is| of \meta{inject post} is not
|
191
|
|
-% affected by the |\itshape| in \meta{inject pre}.
|
192
|
|
-% The used code to generate that table was:
|
|
203
|
+% One can see that the \verb|\sffamily is| of \meta{inject post} is not affected
|
|
204
|
+% by the |\itshape| in \meta{inject pre}. The used code to generate that table
|
|
205
|
+% was:
|
193
|
206
|
%
|
194
|
|
-%\begin{verbatim}
|
|
207
|
+% \begin{verbatim}
|
195
|
208
|
% \examplecmd{Hi,}{my}{\verb|name|}{Steve!}
|
196
|
|
-%\end{verbatim}
|
|
209
|
+% \end{verbatim}
|
197
|
210
|
%
|
198
|
211
|
% \section{Useful Example?}
|
199
|
212
|
%
|
200
|
213
|
% This example provides a macro which typesets its mandatory argument in a block
|
201
|
214
|
% of a definable number of lines, it is meant for a single paragraph.
|
202
|
215
|
%
|
203
|
|
-%\begin{verbatim}
|
|
216
|
+% \begin{verbatim}
|
204
|
217
|
% % Getting a box register:
|
205
|
218
|
% \newsavebox\RectangleBox
|
206
|
219
|
% % Defining the main macro
|
|
@@ -218,16 +231,20 @@ and the derived files boxgrab.pdf and
|
218
|
231
|
% \end{minipage}%
|
219
|
232
|
% \endgroup
|
220
|
233
|
% }
|
221
|
|
-%\end{verbatim}
|
|
234
|
+% \end{verbatim}
|
222
|
235
|
%
|
223
|
|
-% And a usage example of our new macro (with the \pkg{duckuments} package
|
|
236
|
+% As you can see, this macro uses \cs{boxgrab} in a group delimited by
|
|
237
|
+% \cs{begingroup} and \cs{endgroup}. It should therefore be safe to nest it
|
|
238
|
+% inside other macros using \cs{boxgrab}.
|
|
239
|
+%
|
|
240
|
+% Finally a usage example of our new macro (with the \pkg{duckuments} package
|
224
|
241
|
% loaded):
|
225
|
242
|
%
|
226
|
|
-%\begin{verbatim}
|
|
243
|
+% \begin{verbatim}
|
227
|
244
|
% \begin{center}
|
228
|
245
|
% \Rectangle[9]{\blindduck}
|
229
|
246
|
% \end{center}
|
230
|
|
-%\end{verbatim}
|
|
247
|
+% \end{verbatim}
|
231
|
248
|
% Results:
|
232
|
249
|
% \newsavebox\RectangleBox
|
233
|
250
|
% \newcommand\Rectangle[1][4]
|
|
@@ -260,62 +277,75 @@ and the derived files boxgrab.pdf and
|
260
|
277
|
% \begin{macrocode}
|
261
|
278
|
\@ifdefinable{\if@boxgrab@spaces@}{\newif\if@boxgrab@spaces@}
|
262
|
279
|
\newcommand*\boxgrab@args{}%
|
263
|
|
-\NewDocumentCommand \boxgrab { s m +O{} m +O{} m }
|
|
280
|
+\newcommand\boxgrab@def[2]
|
264
|
281
|
{%
|
265
|
|
- \IfBooleanTF{#1}
|
266
|
|
- {\@boxgrab@spaces@true}
|
267
|
|
- {\@boxgrab@spaces@false}%
|
268
|
|
- \def\boxgrab@name{#2}%
|
269
|
|
- \if@boxgrab@spaces@
|
270
|
|
- \def\boxgrab@into@pre{#3}%
|
|
282
|
+ \@ifdefinable#1{\protected\def#1{#2}}%
|
|
283
|
+ }
|
|
284
|
+\newcommand\boxgrab@def@step[4]
|
|
285
|
+ {%
|
|
286
|
+ \@ifdefinable#1{\protected\def#1##1{\def#2{##1}\boxgrab@opt#3#4}}%
|
|
287
|
+ }
|
|
288
|
+\long\def\boxgrab@check@bracket #1[#2\endboxgrab@check@bracket
|
|
289
|
+ {%
|
|
290
|
+ \if\relax\detokenize{#2}\relax
|
|
291
|
+ \expandafter\@firstoftwo
|
271
|
292
|
\else
|
272
|
|
- \def\boxgrab@into@pre{#3\ignorespaces}%
|
|
293
|
+ \expandafter\@secondoftwo
|
273
|
294
|
\fi
|
274
|
|
- \def\boxgrab@type{#4}%
|
275
|
|
- \def\boxgrab@into@post{#5}%
|
276
|
|
- \def\boxgrab@final{#6}%
|
|
295
|
+ }
|
|
296
|
+\protected\def\boxgrab@opt#1#2%
|
|
297
|
+ {%
|
277
|
298
|
\@ifnextchar[
|
278
|
|
- {\boxgrab@get@args}
|
279
|
|
- {\def\boxgrab@args{}\boxgrab@a}
|
|
299
|
+ {\boxgrab@opt@get#1#2}
|
|
300
|
+ {\def#1{}#2}%
|
280
|
301
|
}
|
281
|
|
-\newcommand \boxgrab@get@args {}
|
282
|
|
-\long\def\boxgrab@get@args#1]
|
|
302
|
+\long\def\boxgrab@opt@get#1#2#3]%
|
283
|
303
|
{%
|
284
|
|
- \expandafter\boxgrab@get@args@a\expandafter{\@gobble#1}%
|
|
304
|
+ \expandafter\boxgrab@opt@get@a\expandafter{\@gobble#3}#1#2%
|
285
|
305
|
}
|
286
|
|
-\newcommand \boxgrab@get@args@a [1]
|
|
306
|
+\protected\long\def\boxgrab@opt@get@a#1#2#3%
|
287
|
307
|
{%
|
288
|
308
|
\boxgrab@check@bracket #1[\endboxgrab@check@bracket
|
289
|
|
- {\def\boxgrab@args{#1}\boxgrab@a}%
|
290
|
|
- {\boxgrab@get@args@grabon{#1]}}%
|
|
309
|
+ {\def#2{#1}#3}
|
|
310
|
+ {\boxgrab@opt@get@b#2#3{#1]}}%
|
291
|
311
|
}
|
292
|
|
-\newcommand \boxgrab@get@args@grabon {}
|
293
|
|
-\long\def\boxgrab@get@args@grabon#1#2]
|
|
312
|
+\protected\long\def\boxgrab@opt@get@b#1#2#3#4]%
|
294
|
313
|
{%
|
295
|
|
- \boxgrab@check@bracket #2[\endboxgrab@check@bracket
|
296
|
|
- {\def\boxgrab@args{#1#2}\boxgrab@a}
|
297
|
|
- {\boxgrab@get@args@grabon{#1#2]}}%
|
|
314
|
+ \boxgrab@check@bracket #4[\endboxgrab@check@bracket
|
|
315
|
+ {\def#1{#3#4}#2}
|
|
316
|
+ {\boxgrab@opt@get@b#1#2{#3#4]}}%
|
298
|
317
|
}
|
299
|
|
-\newcommand \boxgrab@check@bracket {}
|
300
|
|
-\long\def\boxgrab@check@bracket #1[#2\endboxgrab@check@bracket
|
|
318
|
+\boxgrab@def\boxgrab@unpack
|
301
|
319
|
{%
|
302
|
|
- \if\relax\detokenize{#2}\relax
|
|
320
|
+ \expandafter\ifx\boxgrab@type\hbox
|
303
|
321
|
\expandafter\@firstoftwo
|
304
|
322
|
\else
|
305
|
323
|
\expandafter\@secondoftwo
|
306
|
324
|
\fi
|
|
325
|
+ \unhbox
|
|
326
|
+ \unvbox
|
|
327
|
+ \boxgrab@name
|
307
|
328
|
}
|
308
|
|
-\newcommand* \boxgrab@a
|
|
329
|
+\boxgrab@def\boxgrab
|
|
330
|
+ {%
|
|
331
|
+ \@ifstar
|
|
332
|
+ {\@boxgrab@spaces@true\boxgrab@a}
|
|
333
|
+ {\@boxgrab@spaces@false\boxgrab@a}%
|
|
334
|
+ }
|
|
335
|
+\boxgrab@def@step\boxgrab@a\boxgrab@name\boxgrab@into@pre\boxgrab@b
|
|
336
|
+\boxgrab@def@step\boxgrab@b\boxgrab@type\boxgrab@into@post\boxgrab@c
|
|
337
|
+\boxgrab@def@step\boxgrab@c\boxgrab@final\boxgrab@args\boxgrab@d
|
|
338
|
+\boxgrab@def\boxgrab@d
|
309
|
339
|
{%
|
310
|
340
|
\afterassignment\boxgrab@intermediate
|
311
|
341
|
\setbox\boxgrab@name\boxgrab@type
|
312
|
342
|
}
|
313
|
|
-\newcommand*\boxgrab@intermediate
|
|
343
|
+\boxgrab@def\boxgrab@intermediate
|
314
|
344
|
{%
|
315
|
345
|
\aftergroup\boxgrab@after
|
316
|
346
|
\boxgrab@into@pre
|
317
|
347
|
}
|
318
|
|
-\newcommand*\boxgrab@after
|
|
348
|
+\boxgrab@def\boxgrab@after
|
319
|
349
|
{%
|
320
|
350
|
\if@boxgrab@spaces@
|
321
|
351
|
\setbox\boxgrab@name\boxgrab@type
|
|
@@ -333,21 +363,11 @@ and the derived files boxgrab.pdf and
|
333
|
363
|
\fi
|
334
|
364
|
\expandafter\boxgrab@final@exp@args\expandafter{\boxgrab@args}%
|
335
|
365
|
}
|
336
|
|
-\newcommand*\boxgrab@unpack
|
337
|
|
- {%
|
338
|
|
- \csname
|
339
|
|
- un%
|
340
|
|
- \expandafter\expandafter\expandafter\@gobble
|
341
|
|
- \expandafter\string\boxgrab@type
|
342
|
|
- \endcsname
|
343
|
|
- \boxgrab@name
|
344
|
|
- }
|
345
|
366
|
\newcommand\boxgrab@final@exp@args[1]
|
346
|
367
|
{%
|
347
|
368
|
\def\boxgrab@args{}%
|
348
|
369
|
\boxgrab@final#1%
|
349
|
370
|
}
|
350
|
|
-
|
351
|
371
|
% \end{macrocode}
|
352
|
372
|
%
|
353
|
373
|
% \begin{macrocode}
|