| 1 |
%% $Id: xcomment-doc.tex 1022 2024-11-03 08:24:24Z herbert $ |
| 2 |
\documentclass{article} |
| 3 |
\usepackage{libertinus} |
| 4 |
\usepackage{url} |
| 5 |
\usepackage{pstricks,xcomment} |
| 6 |
\usepackage{hyperref} |
| 7 |
\let\XCfv\fileversion |
| 8 |
\let\meta\texttt |
| 9 |
|
| 10 |
\usepackage{fancyvrb,shortvrb,hvlogos} |
| 11 |
\MakeShortVerb{|} |
| 12 |
|
| 13 |
|
| 14 |
\title{Documentation for xcomment.sty% |
| 15 |
\thanks{This file borrows much from verbatim.sty, v.1.4c (90/10/18) |
| 16 |
Copyright (C) 1989, 1990 by Rainer Sch\"opf.}} |
| 17 |
|
| 18 |
\author{Timothy Van Zandt \url{[email protected]}\and Herbert Voß \url{[email protected]}} |
| 19 |
|
| 20 |
\date{\today} |
| 21 |
|
| 22 |
\begin{document} |
| 23 |
|
| 24 |
\maketitle |
| 25 |
\clearpage |
| 26 |
\tableofcontents |
| 27 |
|
| 28 |
\clearpage |
| 29 |
\fvset{numbers=left,frame=lines,fontsize=\small,fontfamily=tt} |
| 30 |
|
| 31 |
\begin{abstract} |
| 32 |
The \LaTeX{} package \verb|verbatim.sty|, by Rainer Sch\"opf, allows one to |
| 33 |
redefine an environment to be a comment, and thereby selectively omit |
| 34 |
certain environments when typesetting a document. |
| 35 |
Suppose instead that one wants to typeset only selected environments? For |
| 36 |
example, one might want to print only a document's tables and figures, |
| 37 |
without having to enclose all the text outside these environments within |
| 38 |
comments. This package allows such selection. |
| 39 |
\end{abstract} |
| 40 |
|
| 41 |
\section{Usage notes} |
| 42 |
|
| 43 |
This package defines a new environment, |xcomment|, which permits one to |
| 44 |
typeset only selected environments, without having to enclose all the text |
| 45 |
outside these environments within comments. The main interest in such a |
| 46 |
feature is that it allows document styles to have great control over what |
| 47 |
parts of a document are typeset, thus extending the modularity of \LaTeX. |
| 48 |
For example, this option was originally written for use in a document class |
| 49 |
for seminar notes and slides. The main text of the input file consists of |
| 50 |
the notes for a seminar, and each slide goes in a |slide| environment. A |
| 51 |
simple modification of the style options allows one to typeset only the |
| 52 |
slides, only the notes, or both together, in a variety of styles. |
| 53 |
However, this package can also be used in default document classes. |
| 54 |
|
| 55 |
|
| 56 |
The |xcomment| environment has as a single mandatory argument a list |
| 57 |
(possibly empty) of environments, separated by commas and with no spaces. |
| 58 |
Within the |xcomment| environment, only text within each of the specified |
| 59 |
environments is typeset. |
| 60 |
The |\xcomment| command can also be used directly, with the same argument, |
| 61 |
and it would typically go in the preamble. Invoking the |\xcomment| command |
| 62 |
is equivalent to putting |\begin{xcomment}| at the invocation of the command |
| 63 |
or immediately after |\begin{document}|, whichever occurs later in the |
| 64 |
document, and |\end{xcomment}| just before the end of the document. |
| 65 |
|
| 66 |
For example, if |\xcomment{table,figure}| is put in the preamble, only |
| 67 |
tables and figures are typeset (but see |\nofloat| below). If the list of |
| 68 |
environments is empty, as in |\begin{xcomment}{}|, the |xcomment| |
| 69 |
environment is essentially like the |comment| environment in verbatim.sty. |
| 70 |
|
| 71 |
|xcomment| environments can be nested, but if the nested environments |
| 72 |
have the same name, the inner environments must be inside text that is |
| 73 |
typeset as specified by the |xcomment| environment the next level up. |
| 74 |
|
| 75 |
Here is an example of such nesting. Suppose we want to include only figures, |
| 76 |
but we also want to be able to comment out some of the figures so that they |
| 77 |
are not included. This is achieved in the following example: |
| 78 |
|
| 79 |
\begin{Verbatim} |
| 80 |
\newxcomment[]{mycomment} |
| 81 |
\begin{xcomment}{figure,mycomment} |
| 82 |
This is stuff that is not included. |
| 83 |
\begin{figure}[!htb] |
| 84 |
This figure is included. |
| 85 |
\end{figure} |
| 86 |
More stuff that is not included. |
| 87 |
\begin{mycomment} % Out and back into comment mode |
| 88 |
\begin{figure}[!htb] % Ignored by mycomment envir. |
| 89 |
This figure is NOT included. |
| 90 |
\end{figure} % Also ignored by mycomment envir. |
| 91 |
\end{mycomment} % In and back out of comment mode. |
| 92 |
More stuff that is not included. |
| 93 |
\end{xcomment} |
| 94 |
\end{Verbatim} |
| 95 |
|
| 96 |
|
| 97 |
\newxcomment[]{mycomment} |
| 98 |
\begin{xcomment}{figure,mycomment} |
| 99 |
This is stuff that is not included. |
| 100 |
\begin{figure}[!htb] |
| 101 |
This figure is included. |
| 102 |
\end{figure} |
| 103 |
More stuff that is not included. |
| 104 |
\begin{mycomment} % Out and back into comment mode |
| 105 |
\begin{figure}[!htb] % Ignored by mycomment envir. |
| 106 |
This figure is NOT included. |
| 107 |
\end{figure} % Also ignored by mycomment envir. |
| 108 |
\end{mycomment} % In and back out of comment mode. |
| 109 |
More stuff that is not included. |
| 110 |
\end{xcomment} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|xcomment| will follow |\input| and |\include| commands. You must use the |
| 115 |
\LaTeX{} syntax |\input{file}| (as opposed to \verb*+\input file +), and the |
| 116 |
inputted file must end with |\endinput|. |
| 117 |
You should be in |xcomment| mode when the |\endinput| command occurs |
| 118 |
if and only if you were in |\xcomment| mode when the |\input| or |\include| |
| 119 |
command was found. |
| 120 |
|
| 121 |
|xcomment| is searching literally for |\begin{foo}| and |\end{foo}| when |
| 122 |
determining whether to switch in or out of comment mode. It will not find, |
| 123 |
e.g., |!begin[foo]|, even if |!|, |[| or |]| have category codes 0, 1 and 2, |
| 124 |
respectively. (If you don't understand this, you can safely ignore it.) |
| 125 |
|
| 126 |
The comment character, |%|, is still a comment character in |xcomment| |
| 127 |
environments (even if used with |\%|). You can change the command character by |
| 128 |
redefining |\xcommentchar|. For example, if I want to use |"| as a comment |
| 129 |
character: |
| 130 |
\begin{Verbatim} |
| 131 |
\renewcommand{\xcommentchar}{\"} |
| 132 |
\end{Verbatim} |
| 133 |
If you define |\xcommentchar| to be empty, then no comment character is used. |
| 134 |
|
| 135 |
In the |xcomment| environment, text is processed a line at a time and |
| 136 |
discarded until |\end{xcomment}| or |\begin{|\meta{environment}|}| is |
| 137 |
encountered, where {\em environment} is to be included. The remaining text |
| 138 |
on the line is not thrown away, as it is in verbatim.sty. Instead, it is |
| 139 |
rescanned, and the only restrictions are that it have balanced braces and |
| 140 |
that it not contain commands that again shift into {\em and} out of |
| 141 |
``comment'' mode. |
| 142 |
|
| 143 |
This is an important feature, because the included environments may have |
| 144 |
arguments that are best placed on the same line as the |
| 145 |
|\begin{|\meta{environment}|}| command. However, the rescanning creates a |
| 146 |
temporary file. By default, the file is |\jobname.tmp|. The command |
| 147 |
|\rescanfile{|\meta{file}|}| causes \meta{file} to be created instead. |
| 148 |
|\rescanfile{}| suppresses the creation of a temporary file; the leftover |
| 149 |
text is simply discarded. |\norescanfile| also suppresses the creation |
| 150 |
of a temporary file, but in this case the text is simply inserted |
| 151 |
without being rescanned (i.e., with category codes 0 (escape |
| 152 |
|\|), 1 (begin group |{|), 2 (end group |}|) and 6 (parameter |#|) switched |
| 153 |
to 12 (other). |
| 154 |
|
| 155 |
The command |\envirsep| is executed between the environments that are |
| 156 |
typeset. Its default definition is |\par|. |
| 157 |
|
| 158 |
The command |
| 159 |
\begin{Verbatim} |
| 160 |
\newxcomment[|\meta{environment list}|]{|\meta{name}|} |
| 161 |
\end{Verbatim} |
| 162 |
defines |\name| and the |name| environment to work like |\xcomment| and the |
| 163 |
|xcomment| environment. A list of environments to be included can be given |
| 164 |
as an optional first argument to |\newxcomment|, in which case the new |
| 165 |
command and environment do not take an argument. For example, if you put |
| 166 |
|\newxcomment[]{mycomment}| in the preamble, the |mycomment| environment |
| 167 |
works like the |comment| environment in verbatim.sty, except for the |
| 168 |
rescanning. |
| 169 |
|
| 170 |
The command |\nofloat{|\meta{environment list}|}| is provided to disable the |
| 171 |
floating of environments in the list (also separated by commas and without |
| 172 |
spaces), since if there are only floats and no text, the floats will |
| 173 |
accumulate to the end of the document and \TeX{} may run out of memory. |
| 174 |
|
| 175 |
\paragraph{Caveats:} |
| 176 |
\begin{itemize} |
| 177 |
\item If |\xcomment| is invoked in the preamble, |\document| should not be |
| 178 |
subsequently redefined. |
| 179 |
\item Be careful what argument you give to |\rescanfile|, since any existing |
| 180 |
file with that name will be destroyed, and the extension |tex| is added if |
| 181 |
no extension is given. |
| 182 |
\end{itemize} |
| 183 |
|
| 184 |
\paragraph{Changes to v1.2:} |
| 185 |
\begin{itemize} |
| 186 |
\item |\include| now allowed. |
| 187 |
\item Comment characters not obeyed, as determined by |\xcommentchar|. |
| 188 |
\end{itemize} |
| 189 |
|
| 190 |
\paragraph{Changes to v1.1:} |
| 191 |
\begin{itemize} |
| 192 |
\item Fixed bug in |\@xcomment| that caused problems when invoking |\xcomment| |
| 193 |
in the preamble. |
| 194 |
\end{itemize} |
| 195 |
|
| 196 |
\paragraph{Changes to v1.0:} |
| 197 |
\begin{itemize} |
| 198 |
\item Main loop rewritten. |
| 199 |
\item |\rescanfile{trash}| changed to |\rescanfile{}|. |
| 200 |
\item |\rescanfile{bounce}| no longer supported. Use |\norescanfile|. |
| 201 |
\item |\input| now allowed. |
| 202 |
\end{itemize} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
\section{The implementation} |
| 208 |
The code is an adaption of code in verbatim.sty, and in fact the latter's |
| 209 |
structure has been preserved when possible. On the one hand, the code is |
| 210 |
simpler than in verbatim.sty because we throw away text from the input file |
| 211 |
rather than typeset it verbatim. On the other hand, it is more involved |
| 212 |
because we have to check for more possible endings to the |xcomment| than in |
| 213 |
verbatim.sty and we have to preserve the input that follows the |\begin|ning |
| 214 |
of an included environment on the same line. |
| 215 |
|
| 216 |
As in verbatim.sty and the |\comment\endcomment| commands in \AmSTeX, the |
| 217 |
basic strategy is to change the category codes so that control sequences and |
| 218 |
other troublesome special characters are neutralized, and then to scan for |
| 219 |
the strings that mark the end of the comment. This would be a 3-liner in |
| 220 |
|awk|, but this is \TeX, and the amount of code required to do the scanning |
| 221 |
increases rapidly as we allow for a greater variety of ending strings. |
| 222 |
|
| 223 |
We begin by ensuring that file is not read in twice, and then we identify |
| 224 |
the file on the VDU and the transcript file. |
| 225 |
|
| 226 |
\fvset{fontsize=\footnotesize} |
| 227 |
|
| 228 |
\begin{Verbatim} |
| 229 |
\@ifundefined{xcomment@@@}{}{\endinput} |
| 230 |
\typeout{Style-Option: `xcomment' v\fileversion \space <\filedate> (tvz) } |
| 231 |
\end{Verbatim} |
| 232 |
|
| 233 |
|
| 234 |
\subsection{User commands} |
| 235 |
|
| 236 |
The main toplevel macro is |\@xcomment|, which is defined in the next |
| 237 |
section. It works like |\xcomment| as described above, except that it has as |
| 238 |
a first argument an {\em environment} so that it stops when it encounters |
| 239 |
|\end{|{\em environment}|}|. |\newxcomment| then has a simple definition, |
| 240 |
and |\xcomment| is defined using the |\newxcomment| command. |
| 241 |
\begin{Verbatim} |
| 242 |
\def\newxcomment{\@ifnextchar [{\@newxcommentwitharg}% |
| 243 |
{\@newxcomment}} |
| 244 |
\def\@newxcomment#1{% |
| 245 |
\expandafter\def\csname #1\endcsname##1{\@xcomment{#1}{##1}}}% |
| 246 |
\def\@newxcommentwitharg[#1]#2{% |
| 247 |
\expandafter\def\csname #2\endcsname{\@xcomment{#2}{#1}}} |
| 248 |
\newxcomment{xcomment} |
| 249 |
\end{Verbatim} |
| 250 |
% |
| 251 |
|\envirsep| is inserted between the environments that are typeset, and is |
| 252 |
set to |\par| by default. |\rescanfile| is the name of the temporary file |
| 253 |
used for rescanning, and is set to |\jobname.tmp| by default. |
| 254 |
\begin{Verbatim} |
| 255 |
\def\envirsep{\par} |
| 256 |
\def\rescanfile#1{\def\@rescanfile{#1}} |
| 257 |
\rescanfile{\jobname.tmp} |
| 258 |
\def\norescanfile{\let\@rescanfile\relax} |
| 259 |
\end{Verbatim} |
| 260 |
% |
| 261 |
|\nofloat| disables floating, and |\vfill| is inserted forcefully on each |
| 262 |
side of each float. The macro is just a hack, and is mainly for printing |
| 263 |
only floats. It may not work well even for that. |\envirsep| is still |
| 264 |
inserted between floats, and if it is set to |\vspace{}|, the added space is |
| 265 |
inserted only between floats on the same page, given the user some control |
| 266 |
over spacing. |
| 267 |
\begin{Verbatim} |
| 268 |
\def\@nofloat#1{\hrule height\z@\nobreak\vfill\vbox\bgroup\def\@captype{#1}} |
| 269 |
\def\end@nofloat{\egroup\nobreak\vfill\nobreak\hrule height\z@\medbreak} |
| 270 |
\def\nofloat#1{\@for\@tempa:=#1\do{\@namedef{#1}{\@nofloat{#1}}% |
| 271 |
\@namedef{end#1}{\end@nofloat}}} |
| 272 |
\end{Verbatim} |
| 273 |
|
| 274 |
|
| 275 |
\subsection{Preliminaries} |
| 276 |
% |
| 277 |
|\xc@makeother| takes as argument a character and changes |
| 278 |
its category code to $12$ (other) if its category code is originally 0, 1, 2 |
| 279 |
or 6. We do not have to change as many catcodes as in Verbatim.sty, because |
| 280 |
the input is either thrown away or rescanned. The fewer codes we change the |
| 281 |
better. |
| 282 |
\begin{Verbatim} |
| 283 |
\def\xc@makeother#1{% |
| 284 |
\ifnum\the\catcode`#1=0\catcode`#112% |
| 285 |
\else \ifnum\the\catcode`#1=1\catcode`#112% |
| 286 |
\else \ifnum\the\catcode`#1=2\catcode`#112% |
| 287 |
\else \ifnum\the\catcode`#1=6\catcode`#112% |
| 288 |
\fi\fi\fi\fi\relax} |
| 289 |
\end{Verbatim} |
| 290 |
|
| 291 |
This macro changes the category codes of a token list that is already in |
| 292 |
\TeX's stomach. The code is a modification of a |\retokenize| macro by |
| 293 |
Raymond Chen. |
| 294 |
\begin{Verbatim} |
| 295 |
\newwrite\tokout |
| 296 |
\newread\tokin |
| 297 |
\end{Verbatim} |
| 298 |
The argument of |\rescan| is a token list register, say, |\mytoks|. |
| 299 |
|\mytoks| presumably contains a string from the input file, sent to |\TeX|'s |
| 300 |
stomach as a stream of tokens. |\rescan| gives |\mytoks| the token list that |
| 301 |
would have arisen if |\TeX| had had the current catcodes in effect when it |
| 302 |
read the input string, with the following exceptions: |
| 303 |
|
| 304 |
\begin{itemize} |
| 305 |
\item The token list must have balanced braces under the new catcodes. |
| 306 |
\item Parameter tokens in the original list are written twice, and so mess |
| 307 |
things up. |
| 308 |
\item Some characters with catcode 10 (space) under the old catcodes are |
| 309 |
lost, and all that remain are treated as character 32 (\verb*+ +) under the |
| 310 |
new catcodes, whatever this character's new catcode is. |
| 311 |
\item Escape characters under the old catcodes are treated as escape |
| 312 |
characters even if their catcodes have changed. (This can be fixed by |
| 313 |
setting |\escapchar| during the write to the escape character under the old |
| 314 |
catcodes, assuming there was only one and it is known) |
| 315 |
\item Leading spaces under the new catcodes are not ignored, which is wrong |
| 316 |
if and only if the string started at the beginning of a line and the space |
| 317 |
characters were not spaces under the old catcodes. |
| 318 |
\end{itemize} |
| 319 |
|
| 320 |
Only the balanced braces exception is a problem in this application, but the |
| 321 |
other exceptions are pointed out since such a rescanning macro has other |
| 322 |
applications. |
| 323 |
\begin{Verbatim} |
| 324 |
\def\rescan#1{% |
| 325 |
\end{Verbatim} |
| 326 |
First we check if |\rescanfile|, which contains the name of the temporary |
| 327 |
file to be used, has a special meaning. If |\relax|, do nothing. If empty, |
| 328 |
empty the token register. |
| 329 |
\begin{Verbatim} |
| 330 |
\ifx\@rescanfile\relax\else |
| 331 |
\ifx\@rescanfile\@empty #1{}\else |
| 332 |
\end{Verbatim} |
| 333 |
Put the list of tokens in braces and write them to the temporary file.% |
| 334 |
\begin{Verbatim} |
| 335 |
\immediate\openout\tokout=\@rescanfile |
| 336 |
\immediate\write\tokout{{\the#1}\relax}% |
| 337 |
\immediate\closeout\tokout |
| 338 |
\end{Verbatim} |
| 339 |
Read the contents of the file |\@tempd|. |
| 340 |
\begin{Verbatim} |
| 341 |
\openin\tokin=\@rescanfile |
| 342 |
\read\tokin to\@tempd |
| 343 |
\closein\tokin |
| 344 |
\end{Verbatim} |
| 345 |
Suppose |#1| is |\mytok|, and \meta{token list} is the list of tokens with |
| 346 |
the current catcodes. Then the next line expands to |\mytok{|\meta{token |
| 347 |
list}|}\relax|. |
| 348 |
\begin{Verbatim} |
| 349 |
\expandafter#1\@tempd% |
| 350 |
\fi\fi} |
| 351 |
\end{Verbatim} |
| 352 |
% |
| 353 |
% |
| 354 |
\subsection{Toplevel macros} |
| 355 |
% |
| 356 |
|\@xcomment| checks whether it was invoked before |\begin{document}| and |
| 357 |
outside of an environment. If so, it modifies |\begin{document}| to invoke |
| 358 |
|\@xcomment| with the same arguments. Otherwise, it processes its arguments |
| 359 |
and goes into ``comment'' mode by invoking |\xc@begin|. |
| 360 |
|
| 361 |
\begin{Verbatim} |
| 362 |
\def\@xcomment#1#2{% |
| 363 |
\ifx\@preamblecmds\@notprerr |
| 364 |
\def\xc@csname{#1}% |
| 365 |
\edef\xc@envirlist{#2}% |
| 366 |
\ifx\xc@envirlist\@empty \@bsphack \else |
| 367 |
\begingroup |
| 368 |
\end{Verbatim} |
| 369 |
|\@envirsep| is what is actually placed before each environment. Initially, |
| 370 |
it is empty because nothing should precede the first environment that is |
| 371 |
typeset. |
| 372 |
\begin{Verbatim} |
| 373 |
\def\@envirsep{}% |
| 374 |
\end{Verbatim} |
| 375 |
|\do@end| is appended to the meaning of |\end|, and it is initially set to |
| 376 |
|\xc@begin|. This means that the |\end| of an included environment switches |
| 377 |
us back into ``comment'' mode, as desired. The |\end| of nested environments |
| 378 |
should have their usual meaning, however. Therefore, |\do@begin| is |
| 379 |
prepended to the definition of |\begin|, and it sets |\do@end| to |\relax|. |
| 380 |
Included environment are begun with |\normal@begin| because we only want to |
| 381 |
reset |\do@end| for nested environements. The group begun above keeps the |
| 382 |
|\end| of the |xcomment| environment from be being affected by these |
| 383 |
changes. |
| 384 |
|
| 385 |
\begin{Verbatim} |
| 386 |
\@ifundefined{normal@begin}{\let\normal@begin\begin}{}% |
| 387 |
\@ifundefined{normal@end}{\let\normal@end\end}{}% |
| 388 |
\def\begin##1{\do@begin{##1}\normal@begin{##1}}% |
| 389 |
\def\end##1{\normal@end{##1}\do@end}% |
| 390 |
\def\do@begin##1{\@ifundefined{##1}{}{\def\do@end{}}}% |
| 391 |
\let\do@end\xc@begin |
| 392 |
\fi |
| 393 |
\let\next\xc@begin |
| 394 |
\else |
| 395 |
\expandafter\@temptokena\expandafter{\document\@xcomment{@@@}{#2}}% |
| 396 |
\edef\document{\the\@temptokena}% |
| 397 |
\let\next\relax |
| 398 |
\fi |
| 399 |
\next} |
| 400 |
\def\end@xcomment{\ifx\xc@envirlist\@empty \@esphack \else \endgroup \fi}% |
| 401 |
\end{Verbatim} |
| 402 |
% |
| 403 |
|\xc@begin| starts up ``comment'' mode by changing the category codes and |
| 404 |
invoking |\xcomment@|. The |\begingroup| keeps these catcode changes local. |
| 405 |
\begin{Verbatim} |
| 406 |
\def\xc@begin{% |
| 407 |
\begingroup |
| 408 |
\let\do\xc@makeother |
| 409 |
\dospecials |
| 410 |
\ifx\xcommentchar\@empty\else |
| 411 |
\expandafter\catcode\expandafter`\xcommentchar=14 |
| 412 |
\fi |
| 413 |
\catcode`\^^M\active |
| 414 |
\xcomment@} |
| 415 |
\def\xcommentchar{\%} |
| 416 |
\end{Verbatim} |
| 417 |
|
| 418 |
|
| 419 |
\end{document} |
| 420 |
|
| 421 |
\subsection{The main loop} |
| 422 |
|
| 423 |
Since the code in the main loop uses literal |\|, |{|, |}|, |%| and newline |
| 424 |
characters, we represent them with |!|, |[|, |]| |"| and |~| by setting the |
| 425 |
corresponding lccodes to the correponding character but with catcode 12, and |
| 426 |
then changing the code to lowercase before executing the macro definitions. |
| 427 |
These catcode changes are kept local, and the macros must therefore be |
| 428 |
defined globally. |
| 429 |
\begin{Verbatim} |
| 430 |
\begingroup |
| 431 |
\catcode`\!=12 |
| 432 |
\catcode`\[=12 |
| 433 |
\catcode`\]=12 |
| 434 |
\catcode`\"=12 |
| 435 |
\lccode`\!=`\\ |
| 436 |
\lccode`\[=`\{ |
| 437 |
\lccode`\]=`\} |
| 438 |
\lccode`\"=`\% |
| 439 |
\catcode`\~=\active |
| 440 |
\lccode`\~=`\^^M |
| 441 |
\lowercase{ |
| 442 |
\end{Verbatim} |
| 443 |
|
| 444 |
The three macros |\xcomment@|, |\xcomment@@|, and |\xcomment@@@| form the |
| 445 |
main loop when in ``comment'' mode. |\xcomment@| reads a line of input. |
| 446 |
|\xcomment@@| searches for the first occurence of a backslash. |
| 447 |
|\xcomment@@@| checks what the next character is, and depending an the |
| 448 |
answer various macros are invoked to see if one of the termination strings |
| 449 |
has been found. |
| 450 |
% |
| 451 |
|\xcomment@| takes the line it has read, puts |\@nnil| |\| and |\@nil| as |
| 452 |
delimiters at the end, and then calls |\xcomment@@|. |
| 453 |
\begin{Verbatim} |
| 454 |
\gdef\xcomment@#1~{\xcomment@@#1\@nnil!\@nil} |
| 455 |
\end{Verbatim} |
| 456 |
% |
| 457 |
|\xcomment@@| takes everything up to the next occurrence of |\| as its |
| 458 |
argument and throws it away together with the |\|. Then |\xcomment@@@| reads |
| 459 |
the next token on the line, including the delimiters added by |\xcomment@|. |
| 460 |
\begin{Verbatim} |
| 461 |
\gdef\xcomment@@#1!{\xcomment@@@} |
| 462 |
\end{Verbatim} |
| 463 |
|
| 464 |
|\xcomment@@@| checks for key strings. |
| 465 |
\begin{Verbatim} |
| 466 |
\gdef\xcomment@@@#1\@nil{% |
| 467 |
\def\@tempa{#1}% |
| 468 |
\ifx\@tempa\@empty |
| 469 |
\let\next\xcomment@ |
| 470 |
\else |
| 471 |
\def\next{\xcomment@@#1\@nil}% |
| 472 |
\@testtrue |
| 473 |
\xc@checkbegin#1\relax begin[]\relax\relax |
| 474 |
\if@test |
| 475 |
\xc@checkend#1\relax end[]\relax\relax |
| 476 |
\if@test |
| 477 |
\xc@checkinput#1\relax input[]\relax\relax |
| 478 |
\if@test |
| 479 |
\xc@checkinclude#1\relax include[]\relax\relax |
| 480 |
\if@test |
| 481 |
\xc@checkendinput#1\relax endinputss\relax\relax |
| 482 |
\fi\fi\fi\fi\fi |
| 483 |
\next} |
| 484 |
\end{Verbatim} |
| 485 |
% |
| 486 |
Check for |\begin| and |\end|. |
| 487 |
\begin{Verbatim} |
| 488 |
\gdef\xc@checkbegin#1begin[#2]#3\relax#4\relax{% |
| 489 |
\def\@tempa{#1}% |
| 490 |
\ifx\@tempa\@empty |
| 491 |
\def\@tempa{#2}% |
| 492 |
\@for\@tempb:=\xc@envirlist\do{% |
| 493 |
\ifx\@tempa\@tempb\def\next{\xc@end{#2}#3\@nil}\fi}% |
| 494 |
\fi} |
| 495 |
\gdef\xc@checkend#1end[#2]#3\relax#4\relax{% |
| 496 |
\def\@tempa{#1}% |
| 497 |
\ifx\@tempa\@empty |
| 498 |
\@testfalse |
| 499 |
\def\@tempa{#2}\def\@tempb{document}% |
| 500 |
\ifx\@tempa\@tempb |
| 501 |
\def\next{\endgroup\end@xcomment\end{document}}% |
| 502 |
\else |
| 503 |
\ifx\@tempa\xc@csname |
| 504 |
\def\next{\end@@xcomment{#2}#3\@nil}% |
| 505 |
\fi\fi\fi} |
| 506 |
\end{Verbatim} |
| 507 |
|
| 508 |
Check for |\input| and |\endinput|. |
| 509 |
\begin{Verbatim} |
| 510 |
\gdef\xc@checkinput#1input[#2]#3\relax#4\relax{% |
| 511 |
\def\@tempa{#1}% |
| 512 |
\ifx\@tempa\@empty |
| 513 |
\def\next{\expandafter\xcomment@\@@input #2 \xcomment@@#3\@nil}% |
| 514 |
\@testfalse |
| 515 |
\fi} |
| 516 |
\gdef\xc@checkendinput#1endinput#2#3\relax#4\relax{% |
| 517 |
\def\@tempa{#1}% |
| 518 |
\ifx\@tempa\@empty\ifcat\noexpand#2a\relax\else |
| 519 |
\let\next\endinput |
| 520 |
\fi\fi} |
| 521 |
\end{Verbatim} |
| 522 |
|
| 523 |
Check for |\input| and |\endinput|. |
| 524 |
\begin{Verbatim} |
| 525 |
\gdef\xc@checkinclude#1include[#2]#3\relax#4\relax{% |
| 526 |
\def\@tempa{#1}% |
| 527 |
\ifx\@tempa\@empty |
| 528 |
\def\next{% |
| 529 |
\global\let\xc@savedinput\@@input |
| 530 |
\def\@@input{% |
| 531 |
\global\let\@@input\xc@savedinput |
| 532 |
\expandafter\xcomment@\@@input}% |
| 533 |
\include{#2}% |
| 534 |
\global\let\@@input\xc@savedinput |
| 535 |
\xcomment@@#3\@nil}% |
| 536 |
\@testfalse |
| 537 |
\fi} |
| 538 |
\end{Verbatim} |
| 539 |
|
| 540 |
|
| 541 |
When exiting the |xcomment| environment, we only have to be careful to |
| 542 |
rescan the line. Note that if get this far, all three delimiters appended to |
| 543 |
the current line are intact. We append a space before rescanning, because of |
| 544 |
a \meta{return} that was stripped by |\xcomment@|. |
| 545 |
\begin{Verbatim} |
| 546 |
\gdef\end@@xcomment#1#2\@nnil!\@nil{% |
| 547 |
\endgroup |
| 548 |
\toks@{#2 }\rescan{\toks@}% |
| 549 |
\edef\next{\noexpand\end@xcomment\noexpand\end{#1}\the\toks@}% |
| 550 |
\next}% |
| 551 |
\end{Verbatim} |
| 552 |
% |
| 553 |
% |
| 554 |
When exiting ``comment'' mode to typeset an included environment, we have to |
| 555 |
get rid of the delimiters added to the current line. The |\endgroup| ends |
| 556 |
the scope of the catcodes changes. The first time an environment is typeset, |
| 557 |
|\@envirsep| is null. Thereafter, |
| 558 |
it is set to |\envirsep|. |
| 559 |
\begin{Verbatim} |
| 560 |
\gdef\xc@end#1#2\@nnil!\@nil{% |
| 561 |
\endgroup |
| 562 |
\@envirsep \let\@envirsep\envirsep |
| 563 |
\toks@{#2 }\rescan{\toks@}% |
| 564 |
\edef\next{\noexpand\normal@begin{#1}\the\toks@}% |
| 565 |
\next}% |
| 566 |
\end{Verbatim} |
| 567 |
|
| 568 |
|
| 569 |
Now we end the lowercase command and the group that were used to insert |
| 570 |
literal |\|, |{| and |}| characters in the code. |
| 571 |
\begin{Verbatim} |
| 572 |
} END OF LOWERCASE |
| 573 |
\endgroup |
| 574 |
\end{Verbatim} |
| 575 |
% |
| 576 |
\end{document} |