Debian Codegraph @

texmf-dist/doc/latex/verbatimbox/verbatimbox.tex

raw
1 \documentclass{article}
2 % Copyright 2013 Steven B. Segletes
3 %
4 % This work may be distributed and/or modified under the
5 % conditions of the LaTeX Project Public License, either version 1.3
6 % of this license or (at your option) any later version.
7 % The latest version of this license is in
8 % http://www.latex-project.org/lppl.txt
9 % and version 1.3c or later is part of all distributions of LaTeX
10 % version 2005/12/01 or later.
11 %
12 % This work has the LPPL maintenance status `maintained'.
13 %
14 % The Current Maintainer of this work is Steven B. Segletes.
15 %
16 \parskip 1em
17 \parindent 0em
18 \usepackage{verbatimbox}
19 \usepackage{etoolbox}
20 \usepackage{color}
21 \makeatletter
22 \preto{\@verbatim}{\topsep=0pt \partopsep=0pt }
23 \makeatother
24
25 \newcommand\rl{\rule{1em}{0in}}
26 \def\vbx{\textsf{verbatimbox}}
27 \let\vb\verb
28 \def\bs{{\ttfamily\char'134}}
29 \reversemarginpar
30 \marginparwidth 1.5in
31 \newcommand\margcmd[1]{\marginpar{\hfill\ttfamily\char'134#1}}
32 \newcommand\margenv[1]{\marginpar{\hfill\ttfamily#1}}
33
34 \begin{document}
35 \begin{myverbbox}{\bgn}\begin\end{myverbbox}
36 \begin{myverbbox}{\edng}\end\end{myverbbox}
37 \begin{myverbbox}{\vrbox}verbbox\end{myverbbox}
38 \begin{myverbbox}{\vrnobox}verbnobox\end{myverbbox}
39 \begin{myverbbox}{\myvrbox}myverbbox\end{myverbbox}
40 \begin{myverbbox}{\ovrbox}origverbbox\end{myverbbox}
41 \begin{myverbbox}{\vbenv}\begin{verbbox}\end{myverbbox}
42 \begin{myverbbox}{\evbenv}\end{verbbox}\end{myverbbox}
43 \begin{myverbbox}{\myvb}\begin{myverbbox}\end{myverbbox}
44 \begin{myverbbox}{\emyvb}end{myverbbox}\end{myverbbox}
45 \begin{myverbbox}{\vbfbox}\verbfilebox\end{myverbbox}
46 \begin{myverbbox}{\vbfnobox}\verbfilenobox\end{myverbbox}
47 \begin{myverbbox}{\tvb}\theverbbox\end{myverbbox}
48 \begin{myverbbox}{\avb}\addvbuffer\end{myverbbox}
49 \begin{myverbbox}{\bts}\boxtopsep\end{myverbbox}
50 \begin{myverbbox}{\bbs}\boxbottomsep\end{myverbbox}
51 \begin{myverbbox}{\eq} = \end{myverbbox}
52 \begin{myverbbox}{\vln}VerbboxLineNo\end{myverbbox}
53 \begin{myverbbox}{\lb}[\end{myverbbox}
54 \begin{myverbbox}{\rb}]\end{myverbbox}
55 \begin{myverbbox}{\lbr}{\end{myverbbox}
56 \begin{myverbbox}{\rbr}}\end{myverbbox}
57 \begin{myverbbox}[\footnotesize]{\fsvrbox}verbbox\end{myverbbox}
58 \begin{myverbbox}[\footnotesize]{\fbx}\fbox\end{myverbbox}
59 \begin{myverbbox}[\footnotesize]{\fbs}\fboxsep\end{myverbbox}
60
61 \begin{center}
62 \LARGE The {\vbx} Package\\
63 \rule{0em}{.7em}\small
64 {\centering
65 \begin{minipage}{.7\textwidth}%
66 Routines for placing stylized verbatim text into boxes, useful in
67 places where the verbatim environment is inaccessible. Secondarily,
68 for adding vertical buffer around an object.
69 \end{minipage}
70 }\\
71 \rule{0em}{2.7em}\large Steven B. Segletes\\
72 [email protected]\\
73 \rule{0em}{1.7em}\today\\
74 \verbatimboxVersionNumber
75 \end{center}
76
77 \section{Description and Commands}
78
79 The {\vbx} package allows verbatim material to be placed into \LaTeX{}
80 boxes, for later recall. This function is useful in several situations,
81 primarily in places where the verbatim environment is otherwise
82 inaccessible (for example, in the midst of a tabular environment). It
83 is also useful if a given verbatim text needs to be recalled multiple
84 times within a document.
85
86 One very nice feature of this package is the optional argument to both
87 environments and macros that allows custom stylization of the verbatim
88 text (fontshapes, sizes, numbering lines, \textit{etc.}) through the use
89 of pre-commands. For those who prefer this form of optional-argument
90 customization, the environments and macros are \textit{also} provided in
91 a ``no-box'' form that is output directly, rather than being saved in a
92 box. The no-box forms can be used when the verbatim has to span across
93 page breaks.
94
95 While there is application for its use within the {\vbx} application,
96 this package also offers an independent command for conveniently
97 providing vertical buffer space above and below an object.
98
99 The environments and macros provided by this package are given as
100 follows:\\\itshape
101 \rl%
102 \addvbuffer[1ex 1ex]%
103 {\parbox{4in}{%
104 \vbenv\lb pre-commands\rb\ldots\evbenv\\
105 \myvb\lb pre-commands\rb\lbr token\rbr\ldots\bs\emyvb\\
106 \vbfbox\lb pre-commands\rb\lbr filename\rbr\\
107 \tvb\lb\texttt{\upshape t}\rb\\
108 \bts\eq length\\
109 \bbs\eq length\\
110 \avb\lb length\rb\lbr object\rbr{} \textup{OR}\\
111 \mbox{}\hspace{3ex}\avb\lb above-length below-length\rb\lbr object\rbr\\
112 \bgn\lbr\vrnobox\rbr\lb pre-commands\rb\ldots\edng\lbr\vrnobox\rbr\\
113 \vbfnobox\lb pre-commands\rb\lbr filename\rbr
114 }}\\[10pt]
115 \upshape
116 In addition, there is a counter, \vln\margenv{\vln}, which contains the
117 current verbatim line number being processed. It's use as part of
118 the optional pre-commands to \vrbox, \myvrbox, or {\vbfbox} (in the form
119 of\\ \rl\verb|[\arabic{VerbboxLineNo}:]|\\
120 will cause the lines of the environment to be numbered.
121
122 \subsection{The \texttt{verbbox} Environment}
123
124 The {\vrbox} environment places the content of the environment into a
125 verbatim box. The box is not printed when the environment is closed.
126 However, the box can be later recalled (even in a verbatim-restricted
127 environment) by way of the command \tvb\margenv{\tvb}. Being placed
128 into a box, it is important to remember that a \LaTeX{} box cannot span
129 across page boundaries.
130
131 The width of the box into which the contents of the environment are
132 placed is the width of the longest line in your environment, rather than
133 the width of the page. This feature can be useful if the box is being
134 recalled inline with other text,
135 \begin{verbbox}[\scriptsize\scshape]
136 as in This Example
137 \end{verbbox}
138 \theverbbox, shown immediately to the left.
139
140 The environment has one optional argument, which can contain commands
141 that will be executed before each line of verbatim text is output.
142 Thus, they can include font size, series, shape, or family changes
143 affecting the appearance of the verbatim text in the box. They can also
144 include printed matter, like a bullet
145
146 \begin{verbatim}
147 \begin{verbbox}[\(\bullet\)\hspace{1ex}]
148 first line
149 second line
150 third line
151 \end{verbbox}
152 \theverbbox
153 \end{verbatim}
154
155 \begin{verbbox}[\(\bullet\)\hspace{1ex}]
156 first line
157 second line
158 third line
159 \end{verbbox}
160 \rl\theverbbox
161
162 or, as mentioned earlier, the lines can be numbered:
163
164 \begin{verbatim}
165 \begin{verbbox}[\arabic{VerbboxLineNo}:\hspace{1ex}]
166 first line
167 second line
168 third line
169 \end{verbbox}
170 \theverbbox
171 \end{verbatim}
172
173 \begin{verbbox}[\arabic{VerbboxLineNo}:\hspace{1ex}]
174 first line
175 second line
176 third line
177 \end{verbbox}
178 \rl\theverbbox
179
180 The customization can make use of the \verb|VerbboxLineNo| counter in
181 ways that make it line specific. Note that the optional argument
182 must be contained on a single line.
183
184 \begin{verbatim}
185 \newcommand*\ifline[3]{%
186 \ifthenelse{\value{VerbboxLineNo} = #1}{#2}{#3}}
187 \newcommand*\highlight{%
188 \color{red}\rmfamily\itshape\bfseries\large\(\bullet~\)}
189 \newcommand\nohighlight{\arabic{VerbboxLineNo}:\hspace{1ex}}
190 \begin{verbbox}[\ifline{2}{\highlight}{\nohighlight}]
191 Line 1
192 Line 2
193 Line 3
194 \end{verbbox}
195 \theverbbox
196 \end{verbatim}
197
198 \newcommand*\ifline[3]{%
199 \ifthenelse{\value{VerbboxLineNo} = #1}{#2}{#3}}
200 \newcommand*\highlight{%
201 \color{red}\rmfamily\itshape\bfseries\large\(\bullet~\)}
202 \newcommand\nohighlight{\arabic{VerbboxLineNo}:\hspace{1ex}}
203 \begin{verbbox}[\ifline{2}{\highlight}{\nohighlight}]
204 Line 1
205 Line 2
206 Line 3
207 \end{verbbox}
208 \rl\theverbbox
209
210 \begin{verbbox}[\arabic{VerbboxLineNo}:\hspace{1ex}]
211 first line
212 second line
213 third line
214 \end{verbbox}
215
216 \subsection{The \texttt{myverbbox} Environment}
217
218 With the {\vrbox} environment, one is limited to one verbatimbox at a
219 time, since each new environment invocation overwrites the prior box. A
220 new environment, {\myvrbox}, has therefore been introduced with version
221 3.0 of the package. It is very similar to \vrbox, except that it is not
222 recalled with the use of \tvb. In addition to the optional argument
223 which functions like that in \vrbox, {\myvrbox} must be supplied a
224 mandatory argument which is the command name that will recall the box
225 contents.
226
227 This environment was created for the situation when there is a need to
228 recall more than one verbatimbox in a given restricted environment.
229 Here is an example where two verbatimboxes must be inserted into the
230 tabular environment (where verbatim is prohibited):
231
232 \begin{verbatim}
233 \begin{myverbbox}{\vtheta}\theta\end{myverbbox}
234 \begin{myverbbox}{\valpha}\alpha\end{myverbbox}
235 \begin{tabular}{|c|c|} \hline
236 \valpha & $\alpha$ \\ \hline
237 \vtheta & $\theta$ \\ \hline
238 \end{tabular}
239 \end{verbatim}
240
241 \begin{myverbbox}{\vtheta}\theta\end{myverbbox}
242 \begin{myverbbox}{\valpha}\alpha\end{myverbbox}
243 \rl\begin{tabular}{|c|c|} \hline
244 \valpha & $\alpha$ \\ \hline
245 \vtheta & $\theta$ \\ \hline
246 \end{tabular}
247
248 In this example the the command \verb|\valpha| will recall a verbatimbox
249 with the verbatim content \valpha. Likewise for \verb|\vtheta|.
250
251 \subsection{The \bs\texttt{verbfilebox} Command}
252
253 The {\vbfbox} command is comparable to the aforementioned environments,
254 except that it is a command which takes the contents of a file specified
255 in the mandatory argument and places it in a verbatimbox. Like
256 \vrbox, the boxed content can be recalled with an invocation of \tvb.
257 The optional argument contains pre-commands and functions in the same
258 manner as described for the {\vrbox} environment.
259
260 \subsection{The \bs\texttt{theverbbox} Command}
261
262 As already mentioned in the prior sections, the {\tvb} command is issued
263 to recall the contents of a box created by either the {\vrbox}
264 environment or the {\vbfbox} command. This command can be issued in
265 places where the verbatim environment is otherwise inaccessible, such as
266 in the tabular environment or inside footnotes.
267
268 There is a \verb|[t]| option to this command, which preconditions {\tvb}
269 for environments where it will be framed, by adding 3pt (by default) of
270 space above the boxed content, since the top of the box is otherwise
271 clipped to the verbatim contents. So in the following example, the left
272 invocation of {\tvb} is done without the \verb|[t]| option, whereas for
273 the right invocation, it is done with the \verb|[t]| option. As you can
274 see, the framing on the right-hand box is more symmetric.
275
276 \rl\begin{tabular}{|c|}
277 \hline
278 \theverbbox\\
279 \hline
280 \end{tabular}
281 %
282 \rl\begin{tabular}{|c|}
283 \hline
284 \theverbbox[t]\\
285 \hline
286 \end{tabular}
287
288 If one wishes\margenv{\bts} a different vertical-buffer preconditioning
289 of {\tvb}, one can either reset the lengths {\bts} and
290 {\bbs}\margenv{\bbs} or else one can use the {\avb}\margenv{\avb}
291 command described below.
292
293 \subsection{The \bs\texttt{addvbuffer} Command}
294
295 The prior section already showed how when a verbatimbox is framed, it
296 may need some buffer space added above the box which is otherwise (by
297 design) clipped to the verbatim content. While the \verb|[t]| option
298 will work for \tvb, that option does not exist for any of the boxes
299 created through the {\myvrbox} environment. Furthermore, the user may
300 sometimes wish to add an arbitrary vertical buffer above and below an
301 object, without changing the underlying {\bts} and {\bbs} lengths
302 associated with the \verb|[t]| option of \tvb.
303
304 The {\avb} command is made to do this, and is useful in many situations
305 inside and outside of verbatimbox manipulations. The mandatory argument
306 to the command is the object over and under which to place vertical
307 buffer. As of version 3.0 of this package, the command comes with an
308 optional argument to specify how much buffer to use. If the optional
309 argument is omitted, the buffer will be the amounts specified by the
310 {\bts} and {\bbs} lengths.
311
312 The optional argument can take two forms. If the optional argument
313 comprises a single length, that length is symmetrically added above and
314 below the object. So, for example,
315 \verb|\fbox{\addvbuffer[5pt]{\theverbbox}}|
316 will produce
317
318 \rl\fbox{\addvbuffer[5pt]{\theverbbox}}
319
320 On the other hand, the optional argument may comprise two lengths, in
321 which case the first length is added above the object and the second is
322 added below the object. Thus,
323 \verb|\fbox{\addvbuffer[15pt 5pt]{\theverbbox}}|
324 will produce
325
326 \rl\fbox{\addvbuffer[15pt 5pt]{\theverbbox}}
327
328 But {\avb} need not only be used for \tvb. It can be used on other
329 objects, for example a \verb|\parbox|
330
331 \rl\fbox{\addvbuffer[15pt 5pt]{\parbox{1in}{This is a test of the
332 emergency broadcast system}}}
333
334 or even on just plain text \verb|\fbox{\addvbuffer[15pt 5pt]{A test}}|:
335
336 \rl\fbox{\addvbuffer[15pt 5pt]{A test}}
337
338 It can even be used to remove vertical space from an objects size, by
339 adding negative buffer \verb|\fbox{\addvbuffer[-5pt -5pt]{A test}}|:
340
341 \rl\fbox{\addvbuffer[-5pt -5pt]{A test}}
342
343 This ability to work on a variety of objects makes {\avb} a powerful
344 command in many applications.
345
346 Likewise, if two length variables are to be used in the optional
347 argument, a non-absorbed space must exist between them.
348 Thus, if the first of the two lengths is a macro or length register,
349 an empty group \verb|{}| should be placed at the end of it, so that
350 the subsequent space is not absorbed:
351
352 \begin{verbatim}
353 \fbox{\addvbuffer[\baselineskip{} .5\baselineskip]{A test}}
354 \end{verbatim}
355
356 \rl\fbox{\addvbuffer[\baselineskip{} .5\baselineskip]{A test}}
357
358 One may not use a ``\verb|\ |'' hard space to separate the
359 two arguments, but instead the method prescribed above.
360
361 \subsection{The ``nobox'' Alternatives}
362
363 Alternatives are provided for the {\vrbox} environment and the {\vbfbox}
364 macro, which output the content directly, rather than place it into a
365 box. These alternatives are named {\vrnobox}\margenv{\vrnobox} and
366 {\vbfnobox}\margenv{\vbfnobox}. Like their boxed counterparts, these
367 alternatives make use of the convenient optional pre-commands that allow
368 for custom stylization of the verbatim content.
369
370 Because they are not placed into a box, but instead output directly,
371 their content cannot be recalled at a later time in the document. While
372 the boxed versions have a particular use in environments where verbatim is
373 inaccessible, the no-boxed alternatives may be used when the content
374 must span a page break.
375
376 \section{Quirks}\fboxsep=0pt
377
378 The use of a single optional argument for the {\vrbox} environment has
379 caused problems in the past, if the first item in the environment was a
380 command, rather than text. This problem has been resolved as of version
381 3.0 of the package (for backward compatibility, we have kept the earlier
382 incarnation of {\vrbox}, now renamed as \ovrbox). However, a few minor
383 quirks remain even with the new version (which, incidentally, can be
384 avoided by using the {\myvrbox} environment). Note that the commands of
385 this package do not enclose the created boxes in frames.\footnote{In all
386 these examples, the {\fsvrbox} material is explicitly placed in an \fbx,
387 to expressedly show the limits of the boxed material. Throughout this
388 section, {\fbs} has been set to 0pt.} Thus, frames shown in these
389 examples are for illustrative purposes only.
390
391 The primary quirk of {\vrbox} is that its optional argument does not
392 take effect until the second line (on the line following the environment
393 invocation), so that
394
395 \begin{verbatim}
396 \begin{verbbox}[\LARGE]first line
397 second line
398 \end{verbbox}
399 \fbox{\theverbbox}
400 \end{verbatim}
401
402 would look like:\\
403 \begin{verbbox}[\LARGE]first line
404 second line
405 \end{verbbox}
406 \rl\addvbuffer[3pt 0pt]{\fbox{\theverbbox}}
407
408 The solution, in the presence of an optional argument, is to begin the
409 first line of the environment contents on the following line. Thus,
410
411 \begin{verbatim}
412 \begin{verbbox}[\LARGE]
413 first line
414 second line
415 \end{verbbox}
416 \fbox{\theverbbox}
417 \end{verbatim}
418
419 will yield the expected result:\\
420 \begin{verbbox}[\LARGE]
421 first line
422 second line
423 \end{verbbox}
424 \rl\addvbuffer[3pt 0pt]{\fbox{\theverbbox}}
425
426 No such requirement is necessary if there are no optional arguments.
427 So, for example,
428 \verb|\begin{verbbox}Hello World\end{verbbox}|
429 \begin{verbbox}Hello World\end{verbbox}
430 will produce, upon issue of \tvb, the box ``\theverbbox.'' As with all
431 verbatim environments, the {\evbenv} must be followed by a linebreak, or
432 the subsequent text on that line will be lost.
433
434 The {\myvrbox} environment does not suffer the above quirk, so that\\
435 \verb|\begin{myverbbox}[\scriptsize]{\mybox}Hello World\end{myverbbox}|\\
436 gives, \begin{myverbbox}[\scriptsize]{\mybox}Hello World\end{myverbbox}
437 upon issue of \verb|\mybox|, the expected result: \mybox.
438
439 The optional argument of {\vrbox} may contain both printing or
440 nonprinting material. The printing material will appear (as \LaTeX{},
441 not as \verb|verbatim|) at the beginning of every line of the \vrbox.
442 However, it is recommended to place nonprinting material at the lead of
443 the optional argument, or the box width may be improperly set, as in
444 this following example:
445
446 \begin{verbatim}
447 \begin{verbbox}[\rmfamily\textsc{Debug}:\footnotesize]
448 first line
449 second line
450 \end{verbbox}
451 \fbox{\theverbbox}
452 \end{verbatim}
453 \begin{verbbox}[\rmfamily\textsc{Debug}:\footnotesize]
454 first line
455 second line
456 \end{verbbox}
457 \rl\fbox{\theverbbox}
458
459 Note how the box does not extend to the end of the text line. When
460 placing printing commands before non-printing commands that change the
461 fontsize, the reverse is also possible, where the box is set too large
462 for the actual text.
463
464 The {\myvrbox} environment behaves identically in this regard. Below we
465 show the prior example, this time created in the {\myvrbox} environment,
466 except that the non-printing optional material has been placed before
467 the printing optional material, so that the box size is correctly
468 interpreted:
469
470 \begin{myverbbox}[\rmfamily\footnotesize\textsc{Debug}:]{\mybox}
471 first line
472 second line
473 \end{myverbbox}
474 \rl\fbox{\mybox}
475
476 There are differences, however, in how {\vrbox} and {\myvrbox} process
477 the optional argument, which relate to overcoming the problems
478 associated with a single optional argument for the {\vrbox} environment.
479 In particular, while the {\myvrbox} optional argument should behave as
480 one would otherwise expect, the optional argument of {\vrbox} will
481 ignore spaces (which can be overcome with \verb|\hspace{}|) and will
482 choke on the dollar symbol (\verb|$|) as a way to enter and exit math
483 mode (which can be overcome by using the \verb|\(|\ \verb|\)| syntax
484 instead). Thus, an optional argument like\\
485 \rl\verb|[\arabic{VerbboxLineNo}$\cdot$ ]|\\
486 which works fine for \myvrbox, would need to appear as\\
487 \rl\verb|[\arabic{VerbboxLineNo}\(\cdot\)\hspace{1ex}]|\\
488 in the {\vrbox} environment. In that case, it would number the lines of
489 the environment in the following manner:
490
491 \begin{verbbox}[\arabic{VerbboxLineNo}\(\cdot\)\hspace{1ex}]
492 first line
493 second line
494 third line
495 \end{verbbox}
496 \rl\theverbbox
497
498 There is also a quirk with the macro \verb|\addvbuffer|.
499 If the \verb|\addvbuffer| macro adds negative space below an argument,
500 the argument is
501 vertically shifted downward by the same amount. For example,
502 compare the following constructions,
503 \verb|\fbox{gb}| and \verb|\fbox{\addvbuffer[-3pt]{gb}}|:
504 \fbox{gb}\fbox{\addvbuffer[-3pt]{gb}}.
505 In the latter case, the box is bottom-trimmed, but the result is shifted downward.
506 This adverse outcome can generally be overcome with a countering
507 \verb|\raisebox|,
508 \fbox{gb}\fbox{\raisebox{3pt}{\addvbuffer[-3pt]{gb}}}.
509 However, if the \verb|\raisebox| would otherwise place
510 the new box above the baseline, the effect of the
511 \verb|\addvbuffer| on the bottom of the box is spoiled, as in the
512 comparison given by the constructions
513 \verb|\fbox{\addvbuffer[-3pt]{X}}| versus
514 \verb|\fbox{\raisebox{3pt}{\addvbuffer[-3pt]{X}}}|
515 which produces this result:
516 \fbox{\addvbuffer[-3pt]{X}}\fbox{\raisebox{3pt}{\addvbuffer[-3pt]{X}}}.
517 Note how 3pt are not trimmed from the bottom of the second box.
518 For this reason, the \verb|\raisebox| remedy has not been incorporated into the definition
519 of \verb|\addvbuffer|; instead, the quirk has been allowed to stand.
520
521
522 \section{Acknowledgements}
523
524 I would like to thank Dr. David Carlisle for his great assistance
525 in helping me to understand the nuances of optional arguments used in
526 verbatim environments, allowing me to correct a longstanding bug in the
527 {\vrbox} environment:\\
528 \rl\addvbuffer[1.1ex 1.4ex]{\parbox{\textwidth}{%
529 http://tex.stackexchange.com/questions/109030/ \\
530 optional-arguments-in-verbatim-environments
531 }}\\
532 I would also like to acknowledge the use of three lines of code created
533 by Prof. Enrico Gregorio, to strip the leading backslash from a
534 \LaTeX{} command name\\
535 \rl\addvbuffer[1.1ex 0ex]{\parbox{\textwidth}{%
536 http://tex.stackexchange.com/questions/42318/ \\
537 removing-a-backslash-from-a-character-sequence
538 }}
539
540 \section{Code Listing}
541
542 \verbatiminput{verbatimbox.sty}
543
544 \end{document}
545
546
547