Debian Codegraph @

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

raw
1 % \iffalse
2 %<*internal>
3 \iffalse
4 %</internal>
5 %<*readme>
6 ___________________
7 The ENVIRON package
8 v0.2
9
10 Provides two things: a new way of defining
11 environments that read their contents before
12 processing; and, a \long version of amsmath's
13 \collect@body macro called \Collect@Body.
14
15 Here's an example:
16
17 \NewEnviron{test}[3][]{"#1,#2,\BODY,#3"}
18 \begin{test}[zero]{one}{last}
19 three\par four
20 \end{test}
21
22 Produces the equivalent of:
23
24 "zero,one,three\par four,last"
25
26 ______________
27 Will Robertson
28 wspr 81 [at] gmail [dot] com
29
30 Copyright 2007-2014
31 Distributed under the LaTeX Project Public License
32 %</readme>
33 %<*internal>
34 \fi
35 \begingroup
36 %</internal>
37 %<*batchfile>
38 \input docstrip.tex
39 \keepsilent
40 \preamble
41
42 __________________________________
43 Copyright (C) 2007 Will Robertson
44
45 License information appended.
46
47
48 \endpreamble
49 \postamble
50
51 Copyright (C) 2007-2014 by Will Robertson <[email protected]>
52
53 Distributable under the LaTeX Project Public License,
54 version 1.3c or higher (your choice). The latest version of
55 this license is at: http://www.latex-project.org/lppl.txt
56
57 This work is "maintained" (as per LPPL maintenance status)
58 by Will Robertson.
59
60 This work consists of the file environ.dtx
61 and the derived files environ.pdf,
62 environ.sty, and
63 environ.ins.
64
65 \endpostamble
66 \askforoverwritefalse
67 \generate{\file{\jobname.sty}{\from{\jobname.dtx}{package}}}
68 %</batchfile>
69 %<batchfile>\endbatchfile
70 %<*internal>
71 \generate{\file{\jobname.ins}{\from{\jobname.dtx}{batchfile}}}
72 \nopreamble\nopostamble
73 \generate{\file{README.txt}{\from{\jobname.dtx}{readme}}}
74 \generate{\file{dtx-style.sty}{\from{\jobname.dtx}{dtx-style}}}
75 \endgroup
76 \immediate\write18{makeindex -s gind.ist -o \jobname.ind \jobname.idx}
77 \immediate\write18{makeindex -s gglo.ist -o \jobname.gls \jobname.glo}
78 %</internal>
79 %
80 %<*driver>
81 \documentclass[a4paper]{ltxdoc}
82 \EnableCrossrefs
83 \CodelineIndex
84 \RecordChanges
85 %\OnlyDescription
86 \usepackage{dtx-style}
87 \begin{document}
88 \DocInput{\jobname.dtx}
89 \end{document}
90 %</driver>
91 %
92 %
93 % \fi
94 %
95 % \GetFileInfo{\jobname.sty}
96 % \CheckSum{0}
97 % \makeatletter
98 %
99 % \title{A couple of things involving environments}
100 % \author{Will Robertson}
101 % \date{\filedate \qquad \fileversion}
102 %
103 % \maketitle
104 %
105 % \begin{abstract}
106 % \noindent This package provides two things, one for document authors and one for macro authors. For the document authors, a new method, \cs{NewEnviron}, fo defining environments that might be more convenient on occasion. And for the package writers, \pkg{amsmath}'s \cmd\collect@body\ command, and a long version of the same, \cmd\Collect@Body.
107 % \end{abstract}
108 %
109 % \section{Introduction}
110 %
111 % This packages provides new commands for defining environments:
112 %
113 % \begin{example}{}
114 % \NewEnviron{test}{%
115 % \fbox{\parbox{1.5cm}{\BODY}}\color{red}
116 % \fbox{\parbox{1.5cm}{\BODY}}}
117 % \begin{test}
118 % par\par graf
119 % \end{test}
120 % \end{example}
121 % \cmd\RenewEnviron\ has the same syntax to redefine a pre-existing environment.
122 %
123 % \section{For the document author}
124 %
125 % \LaTeX's standard method of defining environments looks like this (ignoring arguments for now):
126 % \codeline \cmd\newenvironment\marg{name}\marg{pre code}\marg{post code} .
127 %
128 % The advantage to using environments is that their contents are not treated as a macro argument, so there are fewer restrictions on what can exist inside, and the processing can be more efficient for long pieces of document text.
129 %
130 % The disadvantage of environments is that sometimes you really do want to collect up their body and apply some sort of command to the whole thing. This package provides a way to define such environments:
131 % \codeline \cmd\NewEnviron\marg{name}\marg{macro code}\oarg{final code} .\\
132 % You saw an example in the introduction; the body of the environment is contained within the macro |\BODY|, and \oarg{final code} is the code executed at |\end|\marg{name} (more on this later).
133 %
134 % \subsection{Environment arguments}
135 %
136 % If you want to use arguments to the environment, these are specified as usual:
137 % \codeline \cmd\NewEnviron\marg{name}\oarg{N.\,args}\oarg{opt.\,arg.}\marg{macro code}\oarg{final code} \\
138 % where \marg{macro code} has arguments |#1|, |#2|, \dots, as per traditional \LaTeX\ environment mandatory and optional arguments.
139 %
140 % Here's an example with two arguments; one optional argument (|#1|, which is |\today| if omitted) and one mandatory argument (|#2|):
141 %
142 % \begin{example}{}
143 % \NewEnviron{test}[2][\today]{%
144 % \fbox{\parbox{3cm}{%
145 % \textbf{#2}\\
146 % \BODY\\
147 % (#1)}}}
148 %
149 % \begin{test}{Title}
150 % par\par graf
151 % \end{test}
152 %
153 % \begin{test}[Yesterday]{Title}
154 % par\par graf
155 % \end{test}
156 % \end{example}
157 %
158 % \subsection{\oarg{final code}}
159 % This is the code executed at |\end|\marg{name} of the environment.
160 % For the purposes of this package it is only designed (but is very useful indeed) for cleanup code such as space gobbling in the input text.
161 %
162 % \DescribeMacro{\environfinalcode}
163 % This macro sets a default value for the \oarg{final code} (unless manually specified) in each subsequent environment created with \cmd\NewEnviron. The default is to define each new environment postfixed by \cmd\ignorespacesafterend, like this:
164 % \codeline |\environfinalcode{\ignorespacesafterend}| \\
165 % Here's a silly example:
166 %
167 % \begin{example}{}
168 % \environfinalcode{(finish)}
169 % \NewEnviron{test}{\fbox{\parbox{3cm}{\BODY}}}
170 % \begin{test}
171 % par\par
172 % graf
173 % \end{test}
174 % \end{example}
175 %
176 % Careful, \cmd\environfinalcode\ cannot contain square brackets without first protecting them with braces (\eg,
177 % |\environfinalcode{[end]}|
178 % will not work but
179 % |\environfinalcode{{[end]}}|
180 % will). This is because the optional argument to \cmd\NewEnviron\ itself uses square brackets as argument delimiters.
181 %
182 % \subsection{The \cs{BODY} command}
183 %
184 % \DescribeMacro{\environbodyname}
185 % Using \cs{BODY} as the body of the environment might clash with a command defined by another package.
186 % To overcome such conflicts, rename this command with
187 % \codeline |\environbodyname|\cs{\meta{command}}\\
188 % at which point \cs{NewEnviron} will use \cs{\meta{command}} instead of \cs{BODY}.
189 % Here's an example:
190 %
191 % \begin{example}{}
192 % \NewEnviron{FOO}{\fbox{\BODY}}
193 % \environbodyname\envbody
194 % \NewEnviron{foo}{\fbox{\envbody}}
195 %
196 % \begin{FOO}FOO\end{FOO}
197 % \begin{foo}foo\end{foo}
198 % \end{example}
199 %
200 % \section{For the macro author}
201 %
202 % The \pkg{amsmath} package contains a macro that facilitates the functionality in the previous section, which package writers may wish to use directly. The canonical command is \cmd\collect@body, which I've also defined in \cmd\long\ form to be useable for multi-paragraph environments (\cmd\Collect@Body). Here's how it's used:
203 %
204 % \begin{example}{}
205 % \long\def\wrap#1{[#1]}
206 % \newenvironment{test}{\Collect@Body\wrap}{}
207 % \begin{test}
208 % hello
209 %
210 % there
211 % \end{test}
212 % \end{example}
213 %
214 % And here's a crude example with environment arguments:
215 %
216 % \begin{example}{}
217 % \long\def\wrap#1{[\arg#1]}
218 % \def\arg#1{---#1---\par}
219 % \newenvironment{test}{\Collect@Body\wrap}{}
220 % \begin{test}{arg}
221 % hello
222 %
223 % there
224 % \end{test}
225 % \end{example}
226 %
227 % \newpage
228 % \section{Test}
229 %
230 % Here's an example or two to ensure everything that you'd think should work, in fact, does:
231 %
232 % \begin{example}{}
233 % \NewEnviron{test}{%
234 % \fbox{\parbox{\linewidth-
235 % 0.1cm*\currentgrouplevel}{\BODY}}
236 % \setlength\fboxrule{2\fboxrule}
237 % \fbox{\parbox{\linewidth-
238 % 0.1cm*\currentgrouplevel}{``\BODY''}}}
239 %
240 % \begin{test}
241 % outer\par
242 % \def\tmp#1{*#1*}%
243 % \tmp{aa}\par
244 % \begin{test}
245 % inner\par
246 % \def\tmp#1{(#1)}\tmp{bb}
247 % \end{test}
248 % \end{test}
249 % \end{example}
250 %
251 %
252 %% ^^A Test that \RenewEnvironment works correctly:
253 % \newenvironment{foobar}{}{}
254 % \RenewEnviron{foobar}{\BODY}
255 %
256 %
257 % \StopEventually{}
258 %
259 % \clearpage
260 % \part{\pkg{\jobname} implementation}
261 %\iffalse
262 %<*package>
263 %\fi
264 %
265 % This is the package.
266 % \begin{macrocode}
267 \ProvidesPackage{environ}[2014/05/04 v0.3 A new way to define environments]
268 \RequirePackage{trimspaces}
269 % \end{macrocode}
270 %
271 % \PrintChanges
272 %
273 % \section{Begin}
274 %
275 % \begin{macro}{\environbodyname}
276 % \darg{control sequence}
277 % Changes the control sequence used to represent the environment body in its definition. Not to be used as a user command; but maybe one day it will be. Don't change this after defining any |\NewEnviron| environments!
278 % \begin{macrocode}
279 \def\environbodyname#1{\def\env@BODY{#1}}
280 \environbodyname\BODY
281 % \end{macrocode}
282 % \changes{v0.3}{2015/05/04}{Works properly and now documented.}
283 % \end{macro}
284 %
285 % \begin{macro}{\environfinalcode}
286 % \darg{code}
287 % This is the \marg{code} that's executed by default at \cs{end}\marg{env.\,name}:
288 % \begin{macrocode}
289 \def\environfinalcode#1{%
290 \def\env@finalcode{#1}}
291 \environfinalcode{\ignorespacesafterend}
292 % \end{macrocode}
293 % \end{macro}
294 %
295 % \begin{macro}{\longdef@c}
296 % \LaTeX3-inspired shorthands.
297 % \begin{macrocode}
298 \def\longdef@c#1{%
299 \expandafter\long\expandafter\def\csname#1\endcsname}
300 % \end{macrocode}
301 % \end{macro}
302 %
303 % \section{\cs{collect@body}-related code}
304 %
305 % \begin{macro}{\collect@body}
306 % Now, \pkg{amsmath} defines \cmd\collect@body\ for us. But that package may not be loaded, and we don't want to have to load the whole thing just for this one macro.
307 % \begin{macrocode}
308 \unless\ifdefined\collect@body
309 \newtoks\@envbody
310 \def\collect@body#1{%
311 \@envbody{\expandafter#1\expandafter{\the\@envbody}}%
312 \edef\process@envbody{\the\@envbody\noexpand\end{\@currenvir}}%
313 \@envbody={}%
314 \def\begin@stack{b}%
315 \begingroup
316 \expandafter\let\csname\@currenvir\endcsname\collect@@body
317 \edef\process@envbody{%
318 \expandafter\noexpand\csname\@currenvir\endcsname}%
319 \process@envbody
320 }
321 \def\push@begins#1\begin#2{%
322 \ifx\end#2\else
323 b\expandafter\push@begins
324 \fi}
325 \def\addto@envbody#1{%
326 \global\@envbody\expandafter{\the\@envbody#1}}
327 \def\collect@@body#1\end#2{%
328 \edef\begin@stack{%
329 \push@begins#1\begin\end \expandafter\@gobble\begin@stack}%
330 \ifx\@empty\begin@stack
331 \endgroup
332 \@checkend{#2}%
333 \addto@envbody{#1}%
334 \else
335 \addto@envbody{#1\end{#2}}%
336 \fi
337 \process@envbody}
338 \fi
339 % \end{macrocode}
340 % \end{macro}
341 %
342 % \begin{macro}{\Collect@Body}
343 % And now we define our own `long' version.
344 % \begin{macrocode}
345 \long\def\Collect@Body#1{%
346 \@envbody{\expandafter#1\expandafter{\the\@envbody}}%
347 \edef\process@envbody{\the\@envbody\noexpand\end{\@currenvir}}%
348 \@envbody={}%
349 \def\begin@stack{b}%
350 \begingroup
351 \expandafter\let\csname\@currenvir\endcsname\Collect@@Body
352 \edef\process@envbody{%
353 \expandafter\noexpand\csname\@currenvir\endcsname}%
354 \process@envbody
355 }
356 \long\def\Push@Begins#1\begin#2{%
357 \ifx\end#2\else
358 b\expandafter\Push@Begins
359 \fi}
360 \long\def\Addto@Envbody#1{%
361 \global\@envbody\expandafter{\the\@envbody#1}}
362 \long\def\Collect@@Body#1\end#2{%
363 \edef\begin@stack{%
364 \Push@Begins#1\begin\end\expandafter\@gobble\begin@stack}%
365 \ifx\@empty\begin@stack
366 \endgroup
367 \@checkend{#2}%
368 \Addto@Envbody{#1}%
369 \else
370 \Addto@Envbody{#1\end{#2}}%
371 \fi
372 \process@envbody}
373 % \end{macrocode}
374 % \end{macro}
375 %
376 % \section{User-level syntax}
377 % \begin{macro}{\RenewEnviron}
378 % \changes{v0.3}{2015/05/04}{Fixed for non-environ commands.}
379 % \begin{macro}{\NewEnviron}
380 % This is the new one.
381 % \begin{macrocode}
382 \def\NewEnviron{%
383 \let\env@newenvironment\newenvironment
384 \env@NewEnviron}
385 \def\RenewEnviron{%
386 \let\env@newenvironment\renewenvironment
387 \env@NewEnviron}
388 % \end{macrocode}
389 % \paragraph{Input argument parsing}
390 % The first optional argument:
391 % \begin{macrocode}
392 \def\env@NewEnviron#1{%
393 \@ifnextchar[
394 {\env@new@i{#1}}
395 {\env@new@iii{#1}{}}}
396 % \end{macrocode}
397 % And the second:
398 % \begin{macrocode}
399 \def\env@new@i#1[#2]{%
400 \@ifnextchar[
401 {\env@new@ii{#1}[#2]}
402 {\env@new@iii{#1}{[#2]}}}
403 % \end{macrocode}
404 % And the second: (cont.)
405 % \begin{macrocode}
406 \def\env@new@ii#1[#2][#3]{%
407 \env@new@iii{#1}{[#2][#3]}}
408 % \end{macrocode}
409 % The final optional argument:
410 % \begin{macrocode}
411 \long\def\env@new@iii#1#2#3{%
412 \@temptokena={\env@new{#1}{#2}{#3}}%
413 \@ifnextchar[{%
414 \the\@temptokena
415 }{%
416 \expandafter\the\expandafter
417 \@temptokena\expandafter[\env@finalcode]%
418 }}
419 % \end{macrocode}
420 % \paragraph{Environment creation code}
421 % \begin{macro}{\env@new}
422 % \darg{name of the environment}
423 % \darg{possible optional args (either `\meta{empty}' or `\texttt{[N]}' or `\texttt{[N][default]}')}
424 % \darg{environment code}
425 % \doarg{final code}
426 % \begin{macrocode}
427 \long\def\env@new#1#2#3[#4]{%
428 % \end{macrocode}
429 % Save the definition of \cs{env@BODY} so we know what to look for.
430 % \begin{macrocode}
431 \longdef@c{env@#1@BODY\expandafter}\expandafter{\env@BODY}%
432 % \end{macrocode}
433 % Define the new environment to Collect its body and execute |env@#1@parse| on it.
434 % \begin{macrocode}
435 \env@newenvironment{#1}{%
436 \expandafter\Collect@Body\csname env@#1@parse\endcsname
437 }{#4}%
438 % \end{macrocode}
439 % |env@#1@parse| executes the body twice: the first time to save the body while ignoring the arguments; and the second time to process the environment definition itself while ignoring the environment body:
440 % \begin{macrocode}
441 \longdef@c{env@#1@parse}##1{%
442 \csname env@#1@save@env\endcsname##1\env@nil
443 \csname env@#1@process\endcsname##1\env@nil}%
444 % \end{macrocode}
445 % These must be defined on a per-environment basis in order to get the argument gobbling right: (because there are a variable number of arguments)
446 % \begin{macrocode}
447 \expandafter\let\csname env@#1@save@env\endcsname\relax
448 \expandafter\let\csname env@#1@process\endcsname\relax
449 \expandafter\newcommand
450 \csname env@#1@save@env\endcsname#2{%
451 \expandafter\expandafter\expandafter
452 \env@save\csname env@#1@BODY\endcsname}%
453 \expandafter\newcommand
454 \csname env@#1@process\endcsname#2{#3\env@ignore}%
455 }
456 % \end{macrocode}
457 % \end{macro}
458 % \begin{macro}{\env@save}
459 % If \cmd{\env@BODY} were variable, this macro would have to be saved for every environment definition individually; at the moment we just use a global definition.
460 % Use \cmd\trim@spaces\ to remove surrounding space:
461 % \begin{macrocode}
462 \long\def\env@save#1#2\env@nil{%
463 \edef#1{%
464 \unexpanded\expandafter
465 \expandafter\expandafter{\trim@spaces{#2}}}}
466 % \end{macrocode}
467 % \end{macro}
468 % This is the same as a \cmd\@gobblenil\ but long and less likely to exist in the environment body:
469 % \begin{macrocode}
470 \long\def\env@ignore#1\env@nil{}
471 % \end{macrocode}
472 % \changes{v0.2}{2008/06/16}{Added.}
473 % \end{macro}
474 % \end{macro}
475 %
476 %\iffalse
477 %</package>
478 %\fi
479 %
480 %
481 %\iffalse
482 %<*dtx-style>
483 % \begin{macrocode}
484 \ProvidesPackage{dtx-style}
485
486 \errorcontextlines=999
487
488 \def\@dotsep{1000}
489 \setcounter{tocdepth}{2}
490 \setlength\columnseprule{0.1pt}
491 \renewcommand\tableofcontents{\relax
492 \begin{multicols}{2}[\section*{\contentsname}]\small
493 \@starttoc{toc}\relax
494 \end{multicols}}
495
496 \setcounter{IndexColumns}{2}
497 \renewenvironment{theglossary}
498 {\small\list{}{}
499 \item\relax
500 \glossary@prologue\GlossaryParms
501 \let\item\@idxitem \ignorespaces
502 \def\pfill{\hspace*{\fill}}}
503 {\endlist}
504
505 \usepackage{booktabs,calc,color,enumitem,fancyvrb,graphicx,ifthen,refstyle,subfig,varioref}
506 \expandafter\usepackage\expandafter{\jobname}
507
508 \usepackage{url}
509
510 \usepackage[sc,osf]{mathpazo}
511
512 \linespread{1.1} % A bit more space between lines
513 \frenchspacing % Remove ugly extra space after punctuation
514
515 \definecolor{niceblue}{rgb}{0.2,0.4,0.8}
516 \newenvironment{example}[1]
517 {\VerbatimEnvironment
518 \def\Options{#1}%
519 \begin{VerbatimOut}[gobble=2]{\examplefilename}}
520 {\end{VerbatimOut}\relax
521 \typesetexample}
522
523 \fvset{formatcom=\color{niceblue}}
524 \DefineShortVerb{\|}
525
526 \def\theCodelineNo{\textcolor{niceblue}{\sffamily\tiny\arabic{CodelineNo}}}
527
528 \let\examplesize\normalsize
529 \let\auxwidth\relax
530
531 \newlength\examplewidth\newlength\verbatimwidth
532 \newlength\exoutdent \newlength\exverbgap
533 \setlength\exverbgap{1em}
534 \setlength\exoutdent{-0.15\textwidth}
535 \newsavebox\verbatimbox
536 \edef\examplefilename{\jobname.example}
537
538 \newcommand\typesetexample{\relax
539 \smallskip
540 \noindent
541 \begin{minipage}{\linewidth}
542 \color{niceblue}
543 \hrulefill\par
544 \edef\@tempa{[gobble=0,fontsize=\noexpand\small,\Options]}%
545 \begin{lrbox}{\verbatimbox}\relax
546 \expandafter\BVerbatimInput\@tempa{\examplefilename}%
547 \end{lrbox}
548 \begin{list}{}{\setlength\itemindent{0pt}
549 \setlength\leftmargin\exoutdent
550 \setlength\rightmargin{0pt}}\item
551 \ifx\auxwidth\relax
552 \setlength\verbatimwidth{\wd\verbatimbox}%
553 \else
554 \setlength\verbatimwidth{\auxwidth}%
555 \fi
556 \begin{minipage}[c]{\textwidth-\exoutdent-\verbatimwidth-\exverbgap}
557 \catcode`\%=14\centering\linespread{1.6}\input\examplefilename\relax
558 \end{minipage}\hfill
559 \begin{minipage}[c]{\verbatimwidth}
560 \usebox\verbatimbox
561 \end{minipage}
562 \end{list}
563 \par\noindent\hrulefill
564 \end{minipage}\par
565 \smallskip\noindent}
566
567 \newcommand*\setverbwidth[1]{\def\auxwidth{#1}}
568
569 \newcommand*\name[1]{{#1}}
570 \newcommand*\pkg[1]{\textsf{#1}}
571 \newcommand*\feat[1]{\texttt{#1}}
572 \newcommand*\opt[1]{\texttt{#1}}
573
574 \newcommand*\ltx[1]{%
575 \ifx3#1\relax
576 \textsc{ltx3}%
577 \else
578 \LaTeXe
579 \fi}
580
581 \newcommand\note{\unskip\footnote}
582
583 \let\latin\textit
584 \def\eg{\latin{e.g.}}
585 \def\Eg{\latin{E.g.}}
586 \def\ie{\latin{i.e.}}
587 \def\etc{\@ifnextchar.{\latin{etc}}{\latin{etc.}\@}}
588
589 \def\STIX{\textsc{stix}}
590 \def\MacOSX{Mac~OS~X}
591 \def\ascii{\textsc{ascii}}
592 \def\OMEGA{Omega}
593 \def\CTAN{\textsc{ctan}}
594
595 \newcounter{argument}
596 \g@addto@macro\endmacro{\setcounter{argument}{0}}
597 \newcommand*\darg[1]{%
598 \stepcounter{argument}%
599 {\ttfamily\char`\{\char`\#\theargument\char`\}:~}#1\par\noindent\ignorespaces}
600 \newcommand*\doarg[1]{%
601 \stepcounter{argument}%
602 {\ttfamily[\char`\#\theargument]:~}#1\par\noindent\ignorespaces}
603
604 \def\codeline{\par\hspace{0.5\parindent}}
605
606 \newcommand\unichar[2]{\textsc{\MakeLowercase{u+#1: #2}}}
607
608 \setlength\parindent{2em}
609
610 % \end{macrocode}
611 %</dtx-style>
612 %\fi
613 %
614 % \typeout{*************************************************************}
615 % \typeout{*}
616 % \typeout{* To finish the installation you have to move the following}
617 % \typeout{* file into a directory searched by TeX:}
618 % \typeout{*}
619 % \typeout{* \space\space\space environ.sty}
620 % \typeout{*}
621 % \typeout{*************************************************************}
622 %
623 \endinput