Browse Source

added prepre

Jonathan Spratte 1 month ago
parent
commit
69a3c3c4fe
2 changed files with 61 additions and 41 deletions
  1. 61
    41
      grabbox.dtx
  2. BIN
      grabbox.pdf

+ 61
- 41
grabbox.dtx View File

@@ -77,8 +77,8 @@ and the derived files           grabbox.pdf and
77 77
 \end{document}
78 78
 %</driver>
79 79
 %<*pkg>
80
-\newcommand*\grabbox@date{2018-11-29}
81
-\newcommand*\grabbox@version{1.2}
80
+\newcommand*\grabbox@date{2018-12-29}
81
+\newcommand*\grabbox@version{1.3}
82 82
 \ProvidesPackage{grabbox}
83 83
   [%
84 84
     \grabbox@date\space v\grabbox@version\space utilities to get an argument as
@@ -104,7 +104,7 @@ and the derived files           grabbox.pdf and
104 104
 % \section{Introduction}
105 105
 %
106 106
 % Sometimes I happen to write macros and environments which don't care for the
107
-% exact contents of an argument but only for that contents typeset
107
+% exact contents of an argument but only for that contents' typeset
108 108
 % representation and its dimensions. In that case I personally dislike the fact
109 109
 % that those arguments couldn't contain verbatim material if coded straight
110 110
 % forward for macros. For environments this is quite easy to create thanks to
@@ -128,23 +128,39 @@ and the derived files           grabbox.pdf and
128 128
 % further -- because the user interface would've been too clumsy and require
129 129
 % strange markup like |\foo arg}| -- and therefore this package wouldn't have
130 130
 % been created.
131
+%
132
+% Additionally I want to thank David Carlisle for helping me making \cs{grabbox}
133
+% respect surrounding text colours.
131 134
 % 
132 135
 % \section{The macro}
133 136
 %
134 137
 % \begin{function}{\grabbox}
135 138
 %   \begin{syntax}
136
-%     \cs{grabbox}\meta{*}\marg{box register}\ignorespaces^^A
137
-%       \oarg{inject pre}\marg{box type}\oarg{inject post}\ignorespaces^^A
138
-%       \marg{afterwards}
139
+%     \cs{grabbox}\meta{*}\oarg{inject pre pre}\ignorespaces^^A
140
+%       \marg{box register}\oarg{inject pre}\marg{box type}\ignorespaces^^A
141
+%       \oarg{inject post}\marg{afterwards}
139 142
 %   \end{syntax}
140 143
 %   grabs the next braced argument and stores it inside of the box \meta{box
141 144
 %   register}. The box is of \meta{box type}, which should be one of \cs{hbox}
142
-%   or \cs{vbox} or \cs{vtop}. \meta{inject pre} will be injected at the
143
-%   beginning of the box and can affect its contents, \meta{inject post} will be
144
-%   injected at the end of the box but can't be affected by stuff inside of
145
-%   \meta{inject pre} or added content unless they are using global definitions.
145
+%   or \cs{vbox} or \cs{vtop}. The contents of the box except for
146
+%   \meta{inject pre pre} and \meta{inject post} will be contained in an
147
+%   additional level of grouping to ensure colour safety (similar to \LaTeX's
148
+%   \cs{sbox}). \meta{inject pre pre} will be injected at the beginning of the
149
+%   box before this additional group is opened, \meta{inject pre} will be
150
+%   injected at the beginning of the box and can affect its contents,
151
+%   \meta{inject post} will be injected at the end of the box but can't be
152
+%   affected by stuff inside of \meta{inject pre} or added content unless they
153
+%   are using global definitions -- \meta{inject pre pre} however can affect the
154
+%   contents of \meta{inject post}.
146 155
 %   Unless the \meta{*} is given leading and trailing spaces will be stripped
147 156
 %   from the box. After the box is read in \meta{afterwards} will be inserted.
157
+%   The complete contents of the box will be something like:
158
+%   \begin{center}
159
+%     \csname verbatim@font\endcsname
160
+%     \meta{inject pre pre}^^A
161
+%     \{\cs{set@color}\meta{inject pre}\meta{argument}\}^^A
162
+%     \meta{inject post}
163
+%   \end{center}
148 164
 % \end{function}
149 165
 %
150 166
 %   \smallskip
@@ -159,8 +175,7 @@ and the derived files           grabbox.pdf and
159 175
 %
160 176
 %   Since \cs{grabbox} works by setting a boxregister using \cs{setbox} (and a
161 177
 %   bunch of temporary macros), it is of course not expandable and defined
162
-%   \cs{protected}. Bear in mind that macros created with \cs{grabbox} are
163
-%   not expandable.
178
+%   \cs{protected}.
164 179
 %
165 180
 % \section{Useless Example!}
166 181
 %
@@ -212,7 +227,7 @@ and the derived files           grabbox.pdf and
212 227
 %   }%
213 228
 % \examplecmd{Hi,}{my}{\verb|\name|}{Steve!}
214 229
 % \end{center}
215
-% One can see that \verb|\sffamily is| of \meta{inject post} is not affected by
230
+% One can see that \verb|\sffamily is| in \meta{inject post} is not affected by
216 231
 % the |\itshape| in \meta{inject pre}. The used code to generate that table was:
217 232
 %
218 233
 % \begin{verbatim}
@@ -246,8 +261,8 @@ and the derived files           grabbox.pdf and
246 261
 % \end{verbatim}
247 262
 %
248 263
 % As you can see, this macro uses \cs{grabbox} in a group delimited by
249
-% \cs{begingroup} and \cs{endgroup}. It should therefore be safe to nest it
250
-% inside other macros using \cs{grabbox}.
264
+% \cs{begingroup} and \cs{endgroup} -- like the useless example. It should
265
+% therefore be safe to nest it inside other macros using \cs{grabbox}.
251 266
 %
252 267
 % Finally a usage example of our new macro (with the \pkg{duckuments} package
253 268
 % loaded):
@@ -287,31 +302,46 @@ and the derived files           grabbox.pdf and
287 302
 %
288 303
 %    \begin{macrocode}
289 304
 \@ifdefinable{\if@grabbox@spaces@}{\newif\if@grabbox@spaces@}
290
-
291
-\@ifdefinable{\grabbox@def}{\long\def\grabbox@def#1#2#{\grabbox@def@a{#1}{#2}}}
305
+\@ifdefinable{\grabbox@def}
306
+  {\long\def\grabbox@def#1#2#{\grabbox@def@a{}#1{#2}}}
307
+\@ifdefinable{\grabbox@ldef}
308
+  {\long\def\grabbox@ldef#1#2#{\grabbox@def@a\long#1{#2}}}
292 309
 \@ifdefinable{\grabbox@def@a}
293 310
   {%
294
-    \protected\long\def\grabbox@def@a#1#2#3%
295
-      {\@ifdefinable#1{\protected\def#1#2{#3}}}%
311
+    \protected\long\def\grabbox@def@a#1#2#3#4%
312
+      {\@ifdefinable#2{\protected#1\def#2#3{#4}}}%
296 313
   }
297 314
 \newcommand\grabbox@def@step[4]
298 315
   {%
299
-    \@ifdefinable#1{\grabbox@def#1##1{\def#2{##1}\grabbox@opt#3#4}}%
316
+    \grabbox@def#1##1{\def#2{##1}\grabbox@opt#3#4}%
300 317
   }
301 318
 \long\def\grabbox@afterelsefi#1\else#2\fi{\fi#1}
302 319
 \long\def\grabbox@afterfi#1\fi{\fi#1}
303
-\long\def\grabbox@afterelsefiA\else#1\fi#2#3{\fi#2}
304
-\long\def\grabbox@afterfiB\fi#1#2{\fi#2}
305 320
 \grabbox@def\grabbox@opt#1#2%
306 321
   {%
307 322
     \@ifnextchar[
308 323
       {\grabbox@opt@get#1#2}
309 324
       {\def#1{}#2}%
310 325
   }
311
-\protected\long\def\grabbox@opt@get#1#2[#3]%
326
+\grabbox@ldef\grabbox@opt@get#1#2[#3]%
312 327
   {%
313 328
     \def#1{#3}#2%
314 329
   }
330
+\grabbox@def\grabbox@set@color
331
+  {%
332
+    \@ifundefined{set@color}{}
333
+      {\global\let\grabbox@set@color\set@color\grabbox@set@color}%
334
+  }
335
+\AtBeginDocument
336
+  {%
337
+    \@ifundefined{set@color}
338
+      {\gdef\grabbox@set@color{}}
339
+      {\global\let\grabbox@set@color\set@color}%
340
+  }%
341
+\newcommand*\grabbox@unskip@space
342
+  {%
343
+    \ifhmode\unskip\fi
344
+  }
315 345
 \grabbox@def\grabbox@unpack
316 346
   {%
317 347
     \begingroup
@@ -331,31 +361,21 @@ and the derived files           grabbox.pdf and
331 361
       {\@grabbox@spaces@true\grabbox@a}
332 362
       {\@grabbox@spaces@false\grabbox@a}%
333 363
   }
334
-\grabbox@def@step\grabbox@a\grabbox@name\grabbox@into@pre\grabbox@b
335
-\grabbox@def@step\grabbox@b\grabbox@type\grabbox@into@post\grabbox@c
336
-\protected\long\def\grabbox@c#1%
364
+\grabbox@def\grabbox@a
365
+  {%
366
+    \grabbox@opt\grabbox@into@prepre\grabbox@b
367
+  }
368
+\grabbox@def@step\grabbox@b\grabbox@name\grabbox@into@pre\grabbox@c
369
+\grabbox@def@step\grabbox@c\grabbox@type\grabbox@into@post\grabbox@d
370
+\grabbox@ldef\grabbox@d#1%
337 371
   {%
338 372
     \def\grabbox@final{#1}%
339 373
     \afterassignment\grabbox@intermediate
340 374
     \setbox\grabbox@name\grabbox@type
341 375
   }
342
-\grabbox@def\grabbox@set@color
343
-  {%
344
-    \@ifundefined{set@color}{}
345
-      {\global\let\grabbox@set@color\set@color\grabbox@set@color}%
346
-  }
347
-\AtBeginDocument
348
-  {%
349
-    \@ifundefined{set@color}
350
-      {\gdef\grabbox@set@color{}}
351
-      {\global\let\grabbox@set@color\set@color}%
352
-  }%
353
-\newcommand*\grabbox@unskip@space
354
-  {%
355
-    \ifhmode\unskip\fi
356
-  }
357 376
 \grabbox@def\grabbox@intermediate
358 377
   {%
378
+    \grabbox@into@prepre
359 379
     \bgroup
360 380
     \if@grabbox@spaces@
361 381
     \else

BIN
grabbox.pdf View File