summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/grabbox/grabbox.dtx
blob: 66a814cb7246a402f6d982d7948ccb9ba1fa642f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
% \iffalse meta-comment
%
% File: grabbox.dtx Copyright (C) 2018 Jonathan P. Spratte
%
% It may be distributed and/or modified under the conditions of the LaTeX
% Project Public License (LPPL), either version 1.3c of this license or (at your
% option) any later version.  The latest version of this license is in the file
%
%   https://www.latex-project.org/lppl.txt
%
% ------------------------------------------------------------------------------
%
%<*driver>
\def\nameofplainTeX{plain}
\ifx\fmtname\nameofplainTeX\else
  \expandafter\begingroup
\fi
\input l3docstrip.tex
\askforoverwritefalse
\preamble

--------------------------------------------------------------
grabbox -- utilities to get an argument as a box
E-mail: jspratte@yahoo.de
Released under the LaTeX Project Public License v1.3c or later
See http://www.latex-project.org/lppl.txt
--------------------------------------------------------------

Copyright (C) 2018 Jonathan P. Spratte

This  work may be  distributed and/or  modified under  the conditions  of the
LaTeX Project Public License (LPPL),  either version 1.3c  of this license or
(at your option) any later version.  The latest version of this license is in
the file:

  http://www.latex-project.org/lppl.txt

This work is "maintained" (as per LPPL maintenance status) by
  Jonathan P. Spratte.

This work consists of the file  grabbox.dtx
and the derived files           grabbox.pdf and
                                grabbox.sty.

\endpreamble
% stop docstrip adding \endinput
\postamble
\endpostamble
\generate{\file{grabbox.sty}{\from{grabbox.dtx}{pkg}}}
\ifx\fmtname\nameofplainTeX
  \expandafter\endbatchfile
\else
  \expandafter\endgroup
\fi
%</driver>
%
%<*driver>
\ProvidesFile{grabbox.dtx}
  [%
    \csname grabbox@date\endcsname\ 
    utilities to get an argument as a box%
  ]
\expandafter\def\csname @classoptionslist\endcsname{}
\RequirePackage[british]{babel}
\documentclass{l3doc}
\usepackage{duckuments}
\usepackage{microtype}
\usepackage{grabbox}
\renewcommand*{\thefootnote}{\fnsymbol{footnote}}
\let\metaOrig\meta
\renewcommand\meta[1]
  {%
    \texttt{\metaOrig{#1}}%
  }
\begin{document}
  \DocInput{grabbox.dtx}
\end{document}
%</driver>
%<*pkg>
\newcommand*\grabbox@date{2018-10-18}
\newcommand*\grabbox@version{1.0b}
\ProvidesPackage{grabbox}
  [\grabbox@date\ v\grabbox@version\ utilities to get an argument as a box]
%</pkg>
% \fi
%
% \begin{center}
%   \LARGE The \pkg{grabbox} package\\[\bigskipamount]
%   \large
%   \setcounter{footnote}{1}%
%   Jonathan P. Spratte\footnotemark\\[\medskipamount]
%   Version \csname grabbox@version\endcsname\\[\smallskipamount]
%   Released \csname grabbox@date\endcsname
% \end{center}
% \footnotetext{E-mail: jspratte@yahoo.de}
%
% \tableofcontents
%
% \begin{documentation}
%
% \section{Introduction}
%
% Sometimes I happen to write macros and environments which don't care for the
% exact contents of an argument but only for that contents typeset
% representation and its dimensions. In that case I personally dislike the fact
% that those arguments couldn't contain verbatim material if coded straight
% forward. Thus the macros distributed hereby came into existence.
%
% This package provides \cs{grabbox} to grab an argument inside of a box. The
% used mechanism allows category code changes in that argument as long as it is
% used in a place allowing category code changes (so not inside of another
% argument).
%
% It is written as a docstrip file: executing |latex grabbox.dtx| generates the
% \file{grabbox.sty} file and typesets this documentation; execute
% |tex grabbox.dtx| to only generate \file{grabbox.sty}.
%
% \section{Acknowledgement}
%
% I want to thank Enrico Gregorio for helping me develop first versions of the
% used mechanisms for the second iteration of my \pkg{ducksay} package. If he
% hadn't helped me back then, I wouldn't have considered the used method
% further -- because the user interface would've been too clumsy and require
% strange markup like |\foo arg}| -- and therefore this package wouldn't have
% been created.
% 
% \section{The macro}
%
% \begin{function}{\grabbox}
%   \begin{syntax}
%     \cs{grabbox}\meta{*}\marg{box register}\ignorespaces^^A
%       \oarg{inject pre}\marg{box type}\oarg{inject post}\ignorespaces^^A
%       \marg{afterwards}
%   \end{syntax}
%   grabs the next braced argument and stores it inside of the box \meta{box
%   register}. The box is of \meta{box type}, which should be one of \cs{hbox}
%   or \cs{vbox} or \cs{vtop}. \meta{inject pre} will be injected at the
%   beginning of the box and can affect its contents, \meta{inject post} will be
%   injected after the box but can't be affected by stuff inside of \meta{inject
%   pre}. Unless the \meta{*} is given leading and trailing spaces will be
%   stripped from the box. After the box is read in \meta{afterwards} will be
%   inserted.
% \end{function}
%
%   \smallskip
%   All assignments are made local. Currently it is not safe to nest macros
%   which use \cs{grabbox}. It should become safe if your macros use
%   \cs{grabbox} inside of a group, so the inner \cs{grabbox} doesn't affect the
%   outer one.
%
%   \cs{grabbox} uses \cs{afterassignment} and \cs{aftergroup} to do its magic.
%   The former should be safe where it is used, the latter is used inside of the
%   boxed argument before any contents are inserted.
%
%   Since \cs{grabbox} works by setting a boxregister using \cs{setbox} (and a
%   bunch of temporary macros), it is of course not expandable and defined
%   \cs{protected}. Bear in mind that macros created with \cs{grabbox} are
%   not expandable.
%
% \section{Useless Example!}
%
% First we need to reserve us a box register for this example:
% \begin{verbatim}
% \newsavebox\ourbox
% \end{verbatim}
% Next we define a macro which takes some arguments and uses \cs{grabbox}:
% \begin{verbatim}
% \newcommand\examplecmd[2]
%   {%
%     \begingroup
%     \grabbox\ourbox[\itshape]\hbox[ \sffamily is]{\examplecmdOut{#1}{#2}}
%   }
% \end{verbatim}
% And we need our helper macro which is executed after \cs{grabbox}:
% \begin{verbatim}
% \newcommand\examplecmdOut[3]
%   {%
%     \begin{tabular}[t]{@{}ll@{}}
%       Arg1: & #1\\
%       Arg2: & #2\\
%       Box:  & \unhbox\ourbox\\
%       Arg3: & #3
%     \end{tabular}%
%     \endgroup
%   }
% \end{verbatim}
%
% The result is a macro that takes two ordinary arguments, after those a
% box in horizontal mode and finally another ordinary argument. If we use this
% macro we get the following:
% \begin{center}
% \newsavebox\ourbox
% \newcommand\examplecmd[2]
%   {%
%     \begingroup
%     \grabbox\ourbox[\itshape]\hbox[ \sffamily is]{\examplecmdOut{#1}{#2}}
%   }%
% \newcommand\examplecmdOut[3]
%   {%
%     \begin{tabular}[t]{@{}ll@{}}
%       Arg1: & #1\\
%       Arg2: & #2\\
%       Box:  & \unhbox\ourbox\\
%       Arg3: & #3
%     \end{tabular}%
%     \endgroup
%   }%
% \examplecmd{Hi,}{my}{\verb|name|}{Steve!}
% \end{center}
% One can see that \verb|\sffamily is| of \meta{inject post} is not affected by
% the |\itshape| in \meta{inject pre}. The used code to generate that table was:
%
% \begin{verbatim}
% \examplecmd{Hi,}{my}{\verb|name|}{Steve!}
% \end{verbatim}
%
% \section{Useful Example?}
%
% This example provides a macro which typesets its mandatory argument in a block
% of a definable number of lines, it is meant for a single paragraph.
%
% \begin{verbatim}
% % Getting a box register:
% \newsavebox\RectangleBox
% % Defining the main macro:
% \newcommand\Rectangle[1][4]
%   {%
%     \begingroup
%     \grabbox\RectangleBox\hbox
%       {%
%         % Since we don't want to read more arguments after the box,
%         % we don't need a second macro and can put the output routine
%         % here.
%         \begin{minipage}{\dimexpr\wd\RectangleBox/#1\relax}
%           \parfillskip0pt
%           \unhbox\RectangleBox
%         \end{minipage}%
%         \endgroup
%       }%
%   }
% \end{verbatim}
%
% As you can see, this macro uses \cs{grabbox} in a group delimited by
% \cs{begingroup} and \cs{endgroup}. It should therefore be safe to nest it
% inside other macros using \cs{grabbox}.
%
% Finally a usage example of our new macro (with the \pkg{duckuments} package
% loaded):
%
% \begin{verbatim}
% \begin{center}
%   \Rectangle[9]{\blindduck}
% \end{center}
% \end{verbatim}
% Results in:
% \begin{center}
% \newsavebox\RectangleBox
% \newcommand\Rectangle[1][4]
%   {%
%     \begingroup
%     \grabbox\RectangleBox\hbox
%       {%
%         \begin{minipage}{\dimexpr\wd\RectangleBox/#1\relax}
%           \parfillskip0pt
%           \unhbox\RectangleBox
%         \end{minipage}%
%         \endgroup
%       }%
%   }
%   \Rectangle[9]{\blindduck}
% \end{center}
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{Implementation}
%
%    \begin{macrocode}
%<*pkg>
%    \end{macrocode}
%
%    \begin{macrocode}
\@ifdefinable{\if@grabbox@spaces@}{\newif\if@grabbox@spaces@}
\newcommand\grabbox@def[2]
  {%
    \@ifdefinable#1{\protected\def#1{#2}}%
  }
\newcommand\grabbox@def@step[4]
  {%
    \@ifdefinable#1{\protected\def#1##1{\def#2{##1}\grabbox@opt#3#4}}%
  }
\long\def\grabbox@check@bracket #1[#2\endgrabbox@check@bracket
  {%
    \if\relax\detokenize{#2}\relax
      \expandafter\@firstoftwo
    \else
      \expandafter\@secondoftwo
    \fi
  }
\protected\def\grabbox@opt#1#2%
  {%
    \@ifnextchar[
      {\grabbox@opt@get#1#2}
      {\def#1{}#2}%
  }
\long\def\grabbox@opt@get#1#2#3]%
  {%
    \expandafter\grabbox@opt@get@a\expandafter{\@gobble#3}#1#2%
  }
\protected\long\def\grabbox@opt@get@a#1#2#3%
  {%
    \grabbox@check@bracket #1[\endgrabbox@check@bracket
      {\def#2{#1}#3}
      {\grabbox@opt@get@b#2#3{#1]}}%
  }
\protected\long\def\grabbox@opt@get@b#1#2#3#4]%
  {%
    \grabbox@check@bracket #4[\endgrabbox@check@bracket
      {\def#1{#3#4}#2}
      {\grabbox@opt@get@b#1#2{#3#4]}}%
  }
\grabbox@def\grabbox@unpack
  {%
    \expandafter\ifx\grabbox@type\hbox
      \expandafter\@firstoftwo
    \else
      \expandafter\@secondoftwo
    \fi
    \unhbox
    \unvbox
    \grabbox@name
  }
\grabbox@def\grabbox
  {%
    \@ifstar
      {\@grabbox@spaces@true\grabbox@a}
      {\@grabbox@spaces@false\grabbox@a}%
  }
\grabbox@def@step\grabbox@a\grabbox@name\grabbox@into@pre\grabbox@b
\grabbox@def@step\grabbox@b\grabbox@type\grabbox@into@post\grabbox@c
\protected\long\def\grabbox@c#1%
  {%
    \def\grabbox@final{#1}%
    \afterassignment\grabbox@intermediate
    \setbox\grabbox@name\grabbox@type
  }
\grabbox@def\grabbox@intermediate
  {%
    \aftergroup\grabbox@after
    \grabbox@into@pre
    \if@grabbox@spaces@
    \else
      \ignorespaces
    \fi
  }
\grabbox@def\grabbox@after
  {%
    \if@grabbox@spaces@
      \setbox\grabbox@name\grabbox@type
        {%
          \grabbox@unpack
          \grabbox@into@post
        }%
    \else
      \setbox\grabbox@name\grabbox@type
        {%
          \grabbox@unpack
          \ifhmode\unskip\fi
          \grabbox@into@post
        }%
    \fi
    \grabbox@final
  }
%    \end{macrocode}
%
%    \begin{macrocode}
\endinput
%    \end{macrocode}
%
% \end{implementation}
%
%    \begin{macrocode}
%</pkg>
%    \end{macrocode}