| 1 |
\documentclass{article} |
| 2 |
\usepackage[T1]{fontenc} |
| 3 |
\usepackage{xcolor,tokcycle,verbatimbox,tabstackengine,lipsum,lmodern} |
| 4 |
\colorlet{bred}{red!90!blue} |
| 5 |
\usepackage{caption} |
| 6 |
\captionsetup{font={sf, footnotesize},labelfont={sf},textfont={footnotesize}} |
| 7 |
\fboxsep=1.5pt\relax |
| 8 |
\makeatletter |
| 9 |
\let\svlt< |
| 10 |
\let\svgt> |
| 11 |
\catcode`>=\active % |
| 12 |
\catcode`<=\active % |
| 13 |
\def\openesc#1{\if\svlt#1\itshape\rmfamily\let\xsvgt\@empty\else |
| 14 |
\svlt\itshape\rmfamily#1\let\xsvgt\svgt\fi} |
| 15 |
\def\closeesc{\upshape\ttfamily\xsvgt} |
| 16 |
\def\vbdelim{\small\catcode`<=\active\catcode`>=\active |
| 17 |
\def<{\openesc} |
| 18 |
\def>{\closeesc}} |
| 19 |
\catcode`>=12 % |
| 20 |
\catcode`<=12 % |
| 21 |
\newcommand\TokCycle{\textsf{tokcycle}} |
| 22 |
\newcommand\Character{\textit{Character}} |
| 23 |
\newcommand\Group{\textit{Group}} |
| 24 |
\newcommand\Macro{\textit{Macro}} |
| 25 |
\newcommand\Space{\textit{Space}} |
| 26 |
\newcommand\CGMS{\textit{C-G-M-S}} |
| 27 |
\newcommand\CGMnS{\textit{C}, \textit{G}, \textit{M}, and \textit{S}} |
| 28 |
\begingroup |
| 29 |
\catcode`|=\active |
| 30 |
\gdef|{\textbackslash} |
| 31 |
\gdef\xmacaux#1{\ttfamily\char92#1\endgroup{}} |
| 32 |
\endgroup |
| 33 |
\gdef\macname{\begingroup\catcode`|=\active \xmacaux} |
| 34 |
\newcommand\thevbox{\medskip\theverbbox\par\medskip\noindent} |
| 35 |
\newenvironment{specfig} |
| 36 |
{\par\bigskip\vbox\bgroup\small\sffamily\centering} |
| 37 |
{\egroup\medskip\par} |
| 38 |
\newtoks\gtoks |
| 39 |
\newcommand\svgtoks[1]{\global\gtoks\expandafter{\the#1}} |
| 40 |
\frenchspacing |
| 41 |
|
| 42 |
\begin{document} |
| 43 |
|
| 44 |
%%% AGAIN |
| 45 |
|
| 46 |
%%% |
| 47 |
|
| 48 |
\begin{myverbbox}{\argone}#1\end{myverbbox} |
| 49 |
\begin{myverbbox}[\vbdelim\normalsize]{\vbnewcytoks} |
| 50 |
\newcytoks{<new token register>} |
| 51 |
\end{myverbbox} |
| 52 |
|
| 53 |
{\centering{\LARGE Package \TokCycle{} \Large (v\tcver)\par} |
| 54 |
Steven B Segletes |
| 55 |
{\footnotesize $<[email protected]$>$} |
| 56 |
\\\small{contributor: Christian Tellechea}\footnote{ |
| 57 |
{I am extremely grateful to Christian $<[email protected]$>$} |
| 58 |
for his assistance in the development of this package. |
| 59 |
The \macname{addcytoks} macro was provided by him. |
| 60 |
He gave constant reminders on what the parser should be able |
| 61 |
to achieve, thus motivating me to spend the extra time striving for |
| 62 |
a generality of application that would not come naturally to me. |
| 63 |
I value highly his collegiality and hold his expertise in the highest |
| 64 |
regard. |
| 65 |
}% |
| 66 |
\\\today\par} |
| 67 |
|
| 68 |
\begin{quote} |
| 69 |
The \TokCycle{} package helps one to build tools to process |
| 70 |
tokens from an input stream. |
| 71 |
If a macro to process an arbitrary single |
| 72 |
(non-macro, non-space) token can be built, then |
| 73 |
\TokCycle{} can provide a wrapper for cycling through an |
| 74 |
input stream (including macros, spaces, and groups) |
| 75 |
on a token-by-token basis, using the |
| 76 |
provided macro on each successive character. |
| 77 |
|
| 78 |
\TokCycle{} characterizes each successive token |
| 79 |
in the input stream as a \Character, a \Group, a \Macro, or |
| 80 |
a \Space. |
| 81 |
Each of these |
| 82 |
token categories are processed with a unique directive, |
| 83 |
to bring about the desired effect of the token cycle. |
| 84 |
\textit{If}-condition flags are provided to identify active, |
| 85 |
implicit, and catcode-6 tokens as they are digested. |
| 86 |
The package provides a number of options |
| 87 |
for handling groups. |
| 88 |
\end{quote} |
| 89 |
\enlargethispage{1.4\baselineskip} |
| 90 |
|
| 91 |
\tableofcontents |
| 92 |
|
| 93 |
\makeatother |
| 94 |
\section{The \TokCycle{} macros and environments}\label{s:intro} |
| 95 |
|
| 96 |
The purpose of the \TokCycle{} package is to provide a tool |
| 97 |
to assist the user in developing token-processing macros and |
| 98 |
environments. |
| 99 |
The types of processing are limited only by the creativity of the user, |
| 100 |
but examples might include letter-case-operations, letter spacing, |
| 101 |
dimensional manipulation, |
| 102 |
simple-ciphering, \{group\} manipulation, macro removal, etc. |
| 103 |
In one sense, it can be thought of as a streaming editor that operates |
| 104 |
on \LaTeX{} input streams. |
| 105 |
|
| 106 |
The package can be loaded into both plain \TeX{}, by way of the |
| 107 |
invocation \verb|\input tokcycle.tex| |
| 108 |
as well as \LaTeX, via \verb|\usepackage{tokcycle}|. |
| 109 |
It provides a total of 6 macros/pseudo environments, based on |
| 110 |
three criteria: |
| 111 |
\begin{itemize} |
| 112 |
\item Two pseudo-environments with the phrase ``\texttt{tokencycle}'' |
| 113 |
in the name, and four macros containing the phrase ``\texttt{tokcycle}''. |
| 114 |
The pseudo-\hspace{0pt}environments operate within a group and typeset |
| 115 |
their result upon completion. |
| 116 |
The macros operate within the document's current scope, but do not typeset the |
| 117 |
result automatically. |
| 118 |
In the case of both macros and pseudo-environments, the transformed result |
| 119 |
is available for later use, being stored in the package |
| 120 |
token list named \macname{cytoks}. |
| 121 |
|
| 122 |
\item Two macros and one pseudo-environment containing the |
| 123 |
phrase ``\texttt{xpress}''. |
| 124 |
Without the phrase, the macro/environment requires four |
| 125 |
\textit{processing directives} to be \textit{explicitly} specified, |
| 126 |
followed by the input stream. |
| 127 |
With the phrase present, only the input stream is to be provided. |
| 128 |
In the \texttt{xpress} case, the \textit{processing directives} are |
| 129 |
to have been separately specified via external macro and/or are taken |
| 130 |
from the most recent \TokCycle{} macro invocation (failing that, |
| 131 |
are taken from the package initialization). |
| 132 |
|
| 133 |
\item Two macros containing the phrase ``\texttt{expanded}''. |
| 134 |
When present, the input stream of the token cycle is subject to the |
| 135 |
new \TeX{} primitive, \macname{expanded}, prior to processing by |
| 136 |
the \TokCycle{} macro. |
| 137 |
Expansion of specific macros in the input stream can be inhibited |
| 138 |
in the input stream with the use of \macname{noexpand}. |
| 139 |
Note that there are no \texttt{expanded} environments in \TokCycle, |
| 140 |
as \TokCycle{} environments do not pre-tokenize their |
| 141 |
input stream. |
| 142 |
|
| 143 |
\end{itemize} |
| 144 |
|
| 145 |
The basic approach of all \TokCycle{} macros/environments is to |
| 146 |
grab each successive token (or group) from the input stream, |
| 147 |
decide what category it is, and use the currently active |
| 148 |
processing directives to make any desired transformation of the |
| 149 |
token (or group). |
| 150 |
Generally, with rare exception, the processed tokens should be stored |
| 151 |
in the token register \macname{cytoks}, using the tools provided |
| 152 |
by the package. |
| 153 |
The cycle continues until the input stream is terminated. |
| 154 |
|
| 155 |
As tokens/groups are read from the input stream, they are |
| 156 |
categorized according to four type classifications, and are |
| 157 |
subjected to the user-specified processing directive associated |
| 158 |
with that category of token/group. |
| 159 |
The \TokCycle{} categories by which the input stream |
| 160 |
is dissected include \Character{}, |
| 161 |
\Group{}, \Macro{}, and \Space{} (alphabetized for easy recall). |
| 162 |
|
| 163 |
Catcode-0 tokens are directed to the \Macro{} directive for processing. |
| 164 |
Catcode-10 tokens are directed to the \Space{} directive. |
| 165 |
When an explicit catcode-1 token\footnote{ |
| 166 |
Throughout this document the terms \textit{catcode-} and \textit{cat-} |
| 167 |
are used interchangeably. |
| 168 |
} |
| 169 |
is encountered in the \TokCycle{} input |
| 170 |
stream, the contents of the entire group (sans the grouping) are directed |
| 171 |
to the \Group{} directive for further processing, which may in turn, |
| 172 |
redirect the individual tokens to the other categories. |
| 173 |
The handling options of implicit cat-1 and 2 tokens are described later in |
| 174 |
this document (section~\ref{s:impgrp}). |
| 175 |
Valid tokens that are neither catcode 0, 1, 2, nor 10, except where noted, |
| 176 |
are directed to the \Character{} directive for processing. |
| 177 |
|
| 178 |
\subsection{Provided (built-in) \TokCycle{} macros and environments} |
| 179 |
|
| 180 |
The syntax of the non-\texttt{xpress} macros/environments is\medskip |
| 181 |
|
| 182 |
\begin{verbbox}[\vbdelim] |
| 183 |
\tokcycle <<or>> \expandedtokcycle |
| 184 |
{<Character processing directive>} |
| 185 |
{<Group-content processing directive>} |
| 186 |
{<Macro processing directive>} |
| 187 |
{<Space processing directive>} |
| 188 |
{<token input stream>} |
| 189 |
\end{verbbox} |
| 190 |
\theverbbox\medskip\par\noindent |
| 191 |
or, alternately, for the pseudo-environment, |
| 192 |
|
| 193 |
\begin{verbbox}[\vbdelim] |
| 194 |
\tokencycle |
| 195 |
{<Character processing directive>} |
| 196 |
{<Group-content processing directive>} |
| 197 |
{<Macro processing directive>} |
| 198 |
{<Space processing directive>}<token input stream>\endtokencycle |
| 199 |
\end{verbbox} |
| 200 |
\thevbox |
| 201 |
For the \texttt{xpress} macros, the syntax is |
| 202 |
|
| 203 |
\begin{verbbox}[\vbdelim] |
| 204 |
\tokcyclexpress <<or>> \expandedtokcyclexpress |
| 205 |
{<token input stream>} |
| 206 |
\end{verbbox} |
| 207 |
\thevbox |
| 208 |
or, alternately, for the \texttt{xpress}-pseudo-environment, |
| 209 |
|
| 210 |
\begin{verbbox}[\vbdelim] |
| 211 |
\tokencyclexpress<token input stream>\endtokencyclexpress |
| 212 |
\end{verbbox} |
| 213 |
\thevbox |
| 214 |
\begin{sloppypar} |
| 215 |
|
| 216 |
\subsection{Create your own \TokCycle{} environments} |
| 217 |
|
| 218 |
In addition to the above macros/environments, the means is |
| 219 |
provided to define new \TokCycle{} environments: |
| 220 |
|
| 221 |
\begin{verbbox}[\vbdelim] |
| 222 |
\tokcycleenvironment\environment_name |
| 223 |
{<Character processing directive>} |
| 224 |
{<Group-content processing directive>} |
| 225 |
{<Macro processing directive>} |
| 226 |
{<Space processing directive>} |
| 227 |
\end{verbbox} |
| 228 |
\thevbox |
| 229 |
This will then permit simplified invocations of the form |
| 230 |
|
| 231 |
\begin{verbbox}[\vbdelim] |
| 232 |
\environment_name<token input stream>\endenvironment_name |
| 233 |
\end{verbbox} |
| 234 |
\thevbox |
| 235 |
More recently, an even more versatile \textit{extended} version has |
| 236 |
been made available: |
| 237 |
|
| 238 |
\begin{verbbox}[\vbdelim] |
| 239 |
\xtokcycleenvironment\environment_name |
| 240 |
{<Character processing directive>} |
| 241 |
{<Group-content processing directive>} |
| 242 |
{<Macro processing directive>} |
| 243 |
{<Space processing directive>} |
| 244 |
{<Set-up code>} |
| 245 |
{<Close-out code>} |
| 246 |
\end{verbbox} |
| 247 |
\thevbox |
| 248 |
This will also permit simplified invocations of the form |
| 249 |
|
| 250 |
\begin{verbbox}[\vbdelim] |
| 251 |
\environment_name<token input stream>\endenvironment_name |
| 252 |
\end{verbbox} |
| 253 |
\thevbox |
| 254 |
However, with this extended environment definition, additional |
| 255 |
set-up and close-out code will be executed prior to and following |
| 256 |
the token cycle, within the scope of the environment. |
| 257 |
The close-out code, while executed after the token cycle, occurs |
| 258 |
before the \macname{cytoks} token list is actually typeset; therefore |
| 259 |
the close-out code can both execute macros and/or add tokens |
| 260 |
to the imminently-typeset token list. |
| 261 |
Any definitions or counters that change as a result of executing |
| 262 |
the \CGMS{} directives are, in fact, \textit{past tense} when the |
| 263 |
close-out code is executed. |
| 264 |
|
| 265 |
Finally, the set-up and close-out code, as well as the \CGMS{} directives |
| 266 |
can invoke \macname{tcafterenv}, whose argument is executed upon the |
| 267 |
exit from the scope of \textit{this} defined environment.\footnote{ |
| 268 |
The \macname{tcafterenv} macro is recognized \textit{only} in the |
| 269 |
context of an extended \macname{xtokcycleenvironment} definition. |
| 270 |
It is not available for direct use in the package's \macname{tokencycle} |
| 271 |
pseudo-environment, nor in \macname{tokcycleenvironment} definitions. |
| 272 |
} |
| 273 |
As with all \TokCycle{} environments, those created by way |
| 274 |
of \macname{tokcycleenvironment} and \macname{xtokcycleenvironment}, |
| 275 |
while operating in the scope of a group, will preserve the contents |
| 276 |
of \macname{cytoks} when exiting their scope. |
| 277 |
|
| 278 |
\subsection{Externally specified directives and directive resets} |
| 279 |
|
| 280 |
For use in \texttt{xpress} mode, the directives for the \CGMS{} |
| 281 |
categories may be externally pre-specified, respectively, |
| 282 |
via the four macros \macname{Characterdirective}, |
| 283 |
\macname{Groupdirective}, \macname{Macrodirective}, |
| 284 |
and \macname{Spacedirective}, each taking an argument containing |
| 285 |
the particulars of the directive specification. |
| 286 |
|
| 287 |
Each of these directives may be individually reset to the package |
| 288 |
default with the following argument-free invocations: |
| 289 |
\macname{resetCharacterdirective}, \macname{resetGroupdirective}, |
| 290 |
\macname{resetMacrodirective}, or \macname{resetSpacedirective}. |
| 291 |
In addition, \macname{resettokcycle} is also provided, which |
| 292 |
not only resets all four directives collectively, but it also |
| 293 |
resets, to the default configuration, the manner in which explicit |
| 294 |
and implicit group tokens are processed. |
| 295 |
Finally, it resets the \macname{aftertokcycle} macro to empty. |
| 296 |
\end{sloppypar} |
| 297 |
|
| 298 |
The default directives at package outset and upon reset are |
| 299 |
|
| 300 |
\begin{verbbox}[\vbdelim] |
| 301 |
\Characterdirective{\addcytoks{#1}} |
| 302 |
\Groupdirective{\processtoks{#1}} |
| 303 |
\Macrodirective{\addcytoks{#1}} |
| 304 |
\Spacedirective{\addcytoks{#1}} |
| 305 |
\aftertokcycle{} |
| 306 |
\stripgroupingfalse |
| 307 |
\stripimplicitgroupingcase{0} |
| 308 |
\end{verbbox} |
| 309 |
\thevbox |
| 310 |
The interpretation of these directives will be explained in the |
| 311 |
remainder of this document. |
| 312 |
Let it suffice for now to say that the default directive settings |
| 313 |
pass through the input stream to the output stream, without |
| 314 |
change.\footnote{ |
| 315 |
Except, possibly, in the case of catcode-6 tokens, which will be |
| 316 |
later addressed in sections~\ref{s:cat6} and \ref{s:whennot}. |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
\section{Commands in the \TokCycle{} directives}\label{s:coms} |
| 322 |
|
| 323 |
The document-level token cycling tools provided in the package are |
| 324 |
listed in section~\ref{s:intro}. |
| 325 |
For each of those commands and/or pseudo-environments, the |
| 326 |
user must (explicitly or implicitly) detail a set of directives to |
| 327 |
specify the manner in which the \Character{}, \Group{}, \Macro{}, |
| 328 |
and \Space{} tokens found in the input stream are to be processed. |
| 329 |
The \CGMS{} processing |
| 330 |
directives consist of normal \TeX/\LaTeX{} commands and user-defined |
| 331 |
macros to accomplish the desired effect. |
| 332 |
There are, however, several macros provided by the package to assist |
| 333 |
the user in this endeavor. |
| 334 |
|
| 335 |
The recommended way to apply this package is to collect the |
| 336 |
\TokCycle-transformed results of the input stream in a token register |
| 337 |
provided by the package, named \macname{cytoks}. |
| 338 |
Its contents can then be typeset via \verb|\the\cytoks|.\footnote{% |
| 339 |
If a token-cycle input stream contains no macros, or is to be detokenized, |
| 340 |
or if the input-stream tokens are not to be typeset, it may be possible |
| 341 |
(though not required) to bypass \macname{cytoks} and typeset the output |
| 342 |
directly. |
| 343 |
} |
| 344 |
The macro for appending things to \macname{cytoks}, to be used in |
| 345 |
the package directives, is \macname{addcytoks}. |
| 346 |
|
| 347 |
\subsection{Adding tokens to the output stream: \macname{addcytoks}} |
| 348 |
|
| 349 |
The macro provided to append tokens to the \macname{cytoks} token register is |
| 350 |
named \macname{addcytoks[]\{\}}. |
| 351 |
Its mandatory argument consists of tokens denoting \textit{what} you |
| 352 |
would like to append to the \macname{cytoks} register, while the |
| 353 |
optional argument denotes \textit{how} you would like them appended (valid |
| 354 |
options include positive integers \verb|[<|\textit{n}\verb|>]| and |
| 355 |
the letter \texttt{[x]}). |
| 356 |
|
| 357 |
When the optional argument is omitted, the specified tokens are appended |
| 358 |
\textit{literally} to the register (without expansion). |
| 359 |
An integer option, call it \textit{n}, takes the |
| 360 |
the mandatory argument, and expands it $n$-times, and appends the |
| 361 |
result to \macname{cytoks}. |
| 362 |
The \texttt{[x]} option employs the \macname{expanded} primitive to maximally |
| 363 |
expand the argument before appending it to \macname{cytoks}. |
| 364 |
|
| 365 |
The \texttt{[x]} option will prove useful when the \Character{} or other |
| 366 |
directives involve a transformation that is fully expandable. |
| 367 |
Its use will allow the expanded |
| 368 |
result of the transformation to be placed in the token list, rather |
| 369 |
than the unexpanded transformation instructions. |
| 370 |
|
| 371 |
\subsubsection{\argone} |
| 372 |
|
| 373 |
In the context of the \CGMnS{} processing directives, one may refer to |
| 374 |
\argone{} (for example, in the argument to \macname{addcytoks} or |
| 375 |
\macname{processtoks}). |
| 376 |
\TeX{} users know that the first parameter to a \TeX{} macro is denoted |
| 377 |
as \argone. |
| 378 |
The specification of all \TokCycle{} processing directives is structured |
| 379 |
in such a way that ``\argone{}'' may be directly employed to indicate |
| 380 |
the current token (or group) under consideration in the input stream. |
| 381 |
|
| 382 |
\subsubsection{Transforming the input stream} |
| 383 |
|
| 384 |
Within the \TokCycle{} \CGMS{} directives, the command |
| 385 |
\verb|\addcytoks{#1}| is used to echo the token being processed to |
| 386 |
the output stream that is being collected in the \macname{cytoks} |
| 387 |
token list. |
| 388 |
However, one may do more than merely echo the input---one may transform |
| 389 |
it. |
| 390 |
If the user creates a macro, let's call it \macname{xform}, to take |
| 391 |
a single character of input and convert it in some way to something |
| 392 |
else, then this macro can be used inside the \Character{} directive |
| 393 |
as \verb|\addcytoks{\xform{#1}}|. |
| 394 |
In this case, if an `\texttt{e}' were the token under consideration |
| 395 |
by the \Character{} directive, `\verb|\xform{e}|' would be added |
| 396 |
to the \macname{cytoks} token list. |
| 397 |
Since \TokCycle{} operates upon each successive token in the input |
| 398 |
stream, this \macname{xform} macro that operates on a single token |
| 399 |
can be used to completely transform the \TokCycle{} input stream. |
| 400 |
|
| 401 |
If the \macname{xform} macro is expandable, reaching its termination in |
| 402 |
a known number, $n$, of expansions, \macname{addcytoks} can take |
| 403 |
advantage of that with its optional argument, performing the |
| 404 |
said number of expansions of \macname{xform} \textit{before} |
| 405 |
adding the result to \macname{cytoks}. |
| 406 |
If the expandability of \macname{xform} is known, but the number $n$ is |
| 407 |
not, the optional argument \verb|[x]| to \macname{addcytoks} may |
| 408 |
be used to fully expand the argument (by way of the |
| 409 |
\macname{expanded} primitive). |
| 410 |
In that case, the resulting \macname{cytoks} result will show |
| 411 |
no evidence of the \macname{xform} macro, but only the results |
| 412 |
of its transformation. |
| 413 |
|
| 414 |
The accompanying examples document, \texttt{tokcycle-examples.pdf}, |
| 415 |
is full of examples showing specific \TokCycle-induced transformations. |
| 416 |
|
| 417 |
\subsection{\Group{} directive: \macname{ifstripgrouping}, and \macname{processtoks}} |
| 418 |
|
| 419 |
The \Group{} directive is unique, in that it is the only directive whose |
| 420 |
argument (\argone) may consist of more than a single token. |
| 421 |
There are two issues to consider when handling the tokens comprising a group: |
| 422 |
do I retain or remove the grouping (cat-1,2 tokens)? |
| 423 |
Do I process the group's token content |
| 424 |
individually through the token cycle, or collectively as a unit? |
| 425 |
|
| 426 |
Grouping in the output stream is determined by the externally set |
| 427 |
condition \macname{ifstripgrouping}. |
| 428 |
The package default is \macname{stripgroupingfalse}, such that any |
| 429 |
explicit grouping that appears in the input stream will be echoed |
| 430 |
in the output stream. |
| 431 |
The alternative, \macname{stripgroupingtrue}, is dangerous |
| 432 |
in the sense that it will strip the cat-1,2 grouping from the |
| 433 |
group's tokens, thereby |
| 434 |
affecting or even breaking code that utilizes such grouping for macro arguments. |
| 435 |
Apply \macname{stripgroupingtrue} with care. |
| 436 |
|
| 437 |
The issue of treating the tokens comprising the content of a |
| 438 |
group individually or collectively |
| 439 |
is determined by the choice of macro inside the \Group{} directive. |
| 440 |
Within the \Group{} directive, the argument \argone{} contains the |
| 441 |
collective tokens of the group (absent the outer cat-1,2 grouping). |
| 442 |
The default directive \macname{processtoks\{\argone\}} will recommit the |
| 443 |
group's tokens individually to be processed through the token cycle. |
| 444 |
In contrast, the command \macname{addcytoks\{\argone\}} in the \Group{} |
| 445 |
directive would directly add the collective group |
| 446 |
of tokens to the \macname{cytoks} register, without being processed |
| 447 |
individually by \TokCycle. |
| 448 |
|
| 449 |
\subsubsection{The \macname{groupedcytoks} macro for more sophisticated |
| 450 |
group processing} |
| 451 |
\label{s:grpcy} |
| 452 |
|
| 453 |
\begin{sloppypar} |
| 454 |
To this point, there have been presented two options for handling grouped |
| 455 |
material in the token cycle: process each of the grouped tokens (via |
| 456 |
\macname{processtoks}) or echo the grouped tokens (via |
| 457 |
\macname{addcytoks}). Obviously, one may also discard the grouped |
| 458 |
tokens altogether by employing neither of the above choices in the |
| 459 |
context of the \Group{} directive. |
| 460 |
\end{sloppypar} |
| 461 |
|
| 462 |
However, there are times where you might wish to perform a macro task |
| 463 |
(for example, one that might add additional tokens to \macname{cytoks}) |
| 464 |
immediately \textit{outside} of the group, before or after. |
| 465 |
If you were to add the task macro before or after the \macname{processtoks} |
| 466 |
invocation (anywhere in the \Group{} directive), it will still be |
| 467 |
performed, by default, \textit{inside} the \macname{cytoks} group. |
| 468 |
|
| 469 |
The macro \macname{groupedcytoks} allows one to manually specify the |
| 470 |
grouping duties of the \Group{} directive. |
| 471 |
Thus, the following two group-processing configurations are |
| 472 |
functionally identical: the default |
| 473 |
|
| 474 |
\begin{verbbox}[\vbdelim] |
| 475 |
\stripgroupingfalse |
| 476 |
\Groupdirective{\processtoks{#1}} |
| 477 |
\end{verbbox} |
| 478 |
\thevbox |
| 479 |
and |
| 480 |
|
| 481 |
\begin{verbbox}[\vbdelim] |
| 482 |
\stripgroupingtrue |
| 483 |
\Groupdirective{\groupedcytoks{\processtoks{#1}}} |
| 484 |
\end{verbbox} |
| 485 |
\thevbox |
| 486 |
Therefore, unless the goal is to introduce additional nesting levels |
| 487 |
into the input stream, the first rule of using \macname{groupedcytoks} |
| 488 |
is to \macname{stripgroupingtrue} before entering the token cycle. |
| 489 |
The argument of \macname{groupedcytoks} specifies the tasks that are to |
| 490 |
occur inside a \macname{cytoks} grouping. |
| 491 |
So, in the following example |
| 492 |
|
| 493 |
\begin{verbbox}[\vbdelim] |
| 494 |
\stripgroupingtrue |
| 495 |
\Groupdirective{\taskA |
| 496 |
\groupedcytoks{\taskB\processtoks{#1}\taskC}% |
| 497 |
\taskD} |
| 498 |
\end{verbbox} |
| 499 |
\thevbox |
| 500 |
the tasks A and D will occur \textit{outside} of the \macname{cytoks} |
| 501 |
group. |
| 502 |
Tasks B and C occur inside the group, immediately before and after processing |
| 503 |
the tokens of the group, respectively. |
| 504 |
Thus, the use of \macname{groupedcytoks} in this way permits \TokCycle{} |
| 505 |
taskings to occur outside of the grouping applied to the output. |
| 506 |
|
| 507 |
If \macname{stripgroupingtrue} had been omitted while still using |
| 508 |
\macname{groupedcytoks}, tasks A and D would have been in the |
| 509 |
explicit group, and tasks B and C, along with the grouped tokens |
| 510 |
\argone{}, would have been nested within an additional group. |
| 511 |
|
| 512 |
\subsubsection{Implicit grouping tokens: \macname{stripimplicitgroupingcase}} |
| 513 |
\label{s:impgrp} |
| 514 |
|
| 515 |
Implicit grouping tokens (e.g., \macname{bgroup} \& \macname{egroup}) |
| 516 |
can be handled in one of \textit{three} separate ways. |
| 517 |
Therefore, rather than using an \textit{if}-condition, the external |
| 518 |
declaration \macname{stripimplicitgroupingcase\{\}} is provided, which takes |
| 519 |
one of 3 integers as its argument (0, 1, or $-$1). |
| 520 |
The package-default case of ``0'' indicates that the implicit-group tokens |
| 521 |
will not be stripped, but rather echoed directly into the |
| 522 |
output stream. |
| 523 |
The case of ``1'' indicates that the implicit-group tokens |
| 524 |
will be stripped and not placed into the output stream |
| 525 |
(as with explicit grouping, this is a dangerous case and should be |
| 526 |
specified with care). |
| 527 |
|
| 528 |
Finally, the special case of $-$1 indicates that the implicit-group |
| 529 |
tokens should instead be passed to the \Character{} directive for |
| 530 |
further handling (note that the \macname{implicittoktrue} condition will |
| 531 |
be set\footnote{as well as the internal condition \macname{tc@implicitgrptrue}}). |
| 532 |
Such a special treatment has limited application---for example, |
| 533 |
when the intent is to detokenize these tokens. |
| 534 |
|
| 535 |
\subsection{Looking ahead at the input stream} |
| 536 |
|
| 537 |
In the normal mode of \TokCycle{} operation, each token of the input stream |
| 538 |
is successively digested and sent to one of four user-defined directives |
| 539 |
for processing. |
| 540 |
Such an approach works well if there is no interdependency of adjacent tokens |
| 541 |
in the input stream. |
| 542 |
When such an interdepency exists (for example, the argument associated with |
| 543 |
an invoked macro), one might typically use flags that are set or cleared |
| 544 |
in one directive that can then be status-checked when the subsequent token |
| 545 |
is processed. |
| 546 |
While such an approach is wholly valid, it can create a complex web of |
| 547 |
flag setting/checking that can span across multiple directives. |
| 548 |
|
| 549 |
With the release of v1.4 of the package, an alternative approach to handle |
| 550 |
such dependencies has been developed: look-ahead features, so that the |
| 551 |
occurrence of a particular token or condition can provoke an immediate |
| 552 |
examination of the input stream to handle a possible dependency, without |
| 553 |
waiting for the next iteration of the token cycle. |
| 554 |
|
| 555 |
It should be noted that the use of look-ahead is perhaps more |
| 556 |
\textit{dangerous} than the traditional use of flags---for if it is not |
| 557 |
performed with care, tampering with the input stream can destroy its |
| 558 |
integrity. |
| 559 |
The commands developed for these situations will now be described. |
| 560 |
They are designed to be employed as part of \TokCycle's \Character, \Macro, |
| 561 |
and \Space{} directives for checking and/or handling successively linked |
| 562 |
tokens in the input stream. |
| 563 |
They cannot be used inside the \Group{} directive. |
| 564 |
|
| 565 |
\subsubsection{\macname{tcpeek}} |
| 566 |
|
| 567 |
When issued witin a directive as, for example \macname{tcpeek|z}, the next |
| 568 |
token from the input stream is \macname{futurelet} into \macname{z}. |
| 569 |
This has several implications. |
| 570 |
The input stream remains undisturbed, so that the peeked-at token will |
| 571 |
still be the token to be digested in the next iteration of the token cycle. |
| 572 |
Because it has been \macname{let}, \macname{z} does not \textit{contain} the token |
| 573 |
from the input stream in the manner of a \macname{def}, but rather it is a |
| 574 |
separate token \textit{possessing the same properties} as the input-stream token. |
| 575 |
As such, it is ideal to be used for \macname{ifx} comparisons. |
| 576 |
For example, \macname{ifx|bgroup|z} will detect whether a cat-1 group-opening |
| 577 |
token is at the head of the input stream, e.g., an opening brace, `\{'; |
| 578 |
the comparison \macname{ifx|tcsptoken|z} will detect whether a cat-10 space |
| 579 |
is at the head of the input stream. |
| 580 |
Such comparisons can be useful in directing the logic of the \TokCycle{} |
| 581 |
directive. |
| 582 |
|
| 583 |
\subsubsection{\macname{tcpop}} |
| 584 |
|
| 585 |
When issued as, for example \macname{tcpop|z}, the next token(s) from the |
| 586 |
input stream is \textit{immediately} digested as an argument and the |
| 587 |
tokens are placed into the macro \macname{z}. |
| 588 |
This token (or tokens) will no longer be digested as part of the next |
| 589 |
iteration within the token cycle, unless the tokens are subsequently |
| 590 |
replaced at the head of the input stream (see \macname{tcpush} below). |
| 591 |
|
| 592 |
Beware that when \TeX{} absorbs an argument, leading white space is lost. |
| 593 |
Further, if the argument was enclosed in cat-1,2 braces, the braces are |
| 594 |
stripped and the whole group is absorbed as the argument. |
| 595 |
In some cases, if the argument needs manipulation, this brace-stripping |
| 596 |
behavior may be desired. |
| 597 |
However, if the retention of the leading white space and the braces are |
| 598 |
desired, one can use manual techniques (possibly involving macros |
| 599 |
such as \macname{tcpeek}, \macname{tcpopwhitespace} and |
| 600 |
\macname{tcpushgroup}) or one can instead use \macname{tcpopliteral} |
| 601 |
(see below) in lieu of \macname{tcpop}. |
| 602 |
|
| 603 |
If \macname{tcpop} is used nonetheless, to aid in such matters when |
| 604 |
cat-10 space is at the head of the input stream, the flag |
| 605 |
\macname{spacepoppedtrue} will be set when a \macname{tcpop} is issued. |
| 606 |
However, even \macname{ifspacepopped} will be unable to differentiate explicit |
| 607 |
``white'' space that is lost during argument absorption versus implicit |
| 608 |
space (e.g., \macname{@sptoken} or active-implicit space) that is not |
| 609 |
discarded. |
| 610 |
Another option for dealing with leading white space is |
| 611 |
\macname{tcpopwhitespace} (see below). |
| 612 |
|
| 613 |
|
| 614 |
\subsubsection{\macname{tcpopliteral}} |
| 615 |
|
| 616 |
This macro is an alternative to \macname{tcpop}, if the retention of |
| 617 |
leading white space and possibly braces are required when popping |
| 618 |
an argument from the input stream. |
| 619 |
This can be particularly useful if the popped tokens must later be |
| 620 |
placed back into the input stream in their original state. |
| 621 |
The syntax, \macname{tcpopliteral|z}, pops an argument into \macname{z}, |
| 622 |
while retaining possible leading whitespace and brace groups. |
| 623 |
|
| 624 |
\subsubsection{\macname{tcpopappto}\ and \macname{tcpopliteralappto}} |
| 625 |
|
| 626 |
When issued as, for example \macname{tcpopappto|z}, the next token(s) from the |
| 627 |
input stream is absorbed as an argument and \textit{appended} to the |
| 628 |
replacement text of \macname{z}. |
| 629 |
The same group/space provisos affecting \macname{tcpop} apply here as well. |
| 630 |
The control sequence to be appended, here \macname{z}, may not be undefined |
| 631 |
when \macname{tcpopappto} is invoked. |
| 632 |
This macro is just a convenient joining of two macros: \macname{tcpop} |
| 633 |
and \macname{tcappto\#1from\#2} (see below). |
| 634 |
|
| 635 |
The same applies for the macro \macname{tcpopliteralappto}, which conveniently |
| 636 |
joins \macname{tcpopliteral} and \macname{tcappto\#1from\#2}. |
| 637 |
|
| 638 |
\subsubsection{\macname{tcpopuntil}} |
| 639 |
|
| 640 |
This command pops one or more tokens from the input stream in the manner of |
| 641 |
a delimited argument. |
| 642 |
Thus, when issued as, for example \macname{tcpopuntil~0|z}, tokens from |
| 643 |
the input stream are absorbed into \macname{z} until an \texttt{0} token is |
| 644 |
reached. |
| 645 |
Unlike delimited-argument absorption, however, the delimiter (\texttt{0} in |
| 646 |
this example) is also added to the specified control sequence, in this case |
| 647 |
\macname{z}. |
| 648 |
This construct is very useful for obtaining optional-argument tokens from the |
| 649 |
input stream. |
| 650 |
Consider the following code, with an input stream of \texttt{[1ex]\{2ex\}}: |
| 651 |
\begin{verbatim} |
| 652 |
\tcpeek\z |
| 653 |
\ifx[\z\tcpopuntil ]\q\else\def\q{}\fi |
| 654 |
\end{verbatim} |
| 655 |
Since an optional argument is next in the input stream, \macname{q} will |
| 656 |
obtain the replacement text \texttt{[1ex]}, and the input stream will now |
| 657 |
begin with \texttt{\{2ex\}}. |
| 658 |
If an optional argument had not been next in the input stream, then |
| 659 |
\macname{q} would be empty and the input stream would still begin |
| 660 |
with \texttt{\{2ex\}}. |
| 661 |
|
| 662 |
\subsubsection{\macname{tcpopwhitespace}} |
| 663 |
|
| 664 |
When issued as, for example \macname{tcpopwhitespace|z}, leading white space |
| 665 |
from the input stream will be absorbed and set in \macname{z}, in the |
| 666 |
manner of a \macname{def}. |
| 667 |
Unlike the flag \macname{ifspacepopped}, leading implicit space will not be |
| 668 |
indicated by this macro---only leading explicit white space. |
| 669 |
If no leading white space is present, \macname{z} will be empty following |
| 670 |
its invocation and no tokens will have been removed from the input stream. |
| 671 |
|
| 672 |
\subsubsection{\macname{tcpush}} |
| 673 |
|
| 674 |
To this point, several commands have been described for reading and/or |
| 675 |
extracting tokens from the input stream. |
| 676 |
There are also a corresponding commands for placing material at the head |
| 677 |
of the input stream, should that need arise. |
| 678 |
With an invocation of \macname{tcpush|z}, the replacement text of \macname{z} |
| 679 |
will be pushed onto the input stream. |
| 680 |
Multiple pushes will be handled in a last-in-first-out fashion. |
| 681 |
|
| 682 |
Beware that, because argument absorption in \TeX{} will strip the braces of |
| 683 |
an absorbed group, care must be taken. If the input stream leads with a |
| 684 |
grouped quantity, such as \texttt{\{abc\}}, then \macname{tcpop|z|tcpush|z} |
| 685 |
will end up with \texttt{abc} in the input stream, with the braces missing. |
| 686 |
\textit{The commands \macname{tcpop} and \macname{tcpush} are not strictly |
| 687 |
inverse operations} (see \macname{tcpopliteral} as an alternative). |
| 688 |
For this reason, the command \macname{tcpushgroup} is also provided |
| 689 |
(see below). |
| 690 |
|
| 691 |
The command \macname{tcpush} supports an optional argument that functions |
| 692 |
in the same manner as that of \macname{addcytoks}, to provide additional |
| 693 |
levels of expansion beyond the mere replacement text. |
| 694 |
Thus, \macname{tcpush[2]|z} will take the replacement text of \macname{z}, |
| 695 |
expand it twice, and push the result onto the input stream. |
| 696 |
In a similar way, \macname{tcpush[x]|z} will fully expand the replacement |
| 697 |
text of \macname{z} and then place those tokens onto the input stream. |
| 698 |
Whereas \macname{addytoks} places tokens into the output stream (the |
| 699 |
token list \macname{cytoks}), \macname{tcpush} places tokens at the |
| 700 |
head of the input stream. |
| 701 |
|
| 702 |
\subsubsection{\macname{tcpushgroup}} |
| 703 |
|
| 704 |
The command \macname{tcpushgroup} functions in a manner similar to |
| 705 |
\macname{tcpush}, except that the replacement text of the argument |
| 706 |
is placed onto the input stream within a braced (cat-1,2) group. |
| 707 |
So if the replacement text of \macname{z} is \texttt{abc}, then the |
| 708 |
invocation \macname{tcpushgroup|z} will place \texttt{\{abc\}} onto |
| 709 |
the input stream. |
| 710 |
As with \macname{tcpush}, and with the identical syntax, the command |
| 711 |
\macname{tcpushgroup} supports an optional argument that directs |
| 712 |
addition levels of expansion beyond the mere replacement text. |
| 713 |
|
| 714 |
\subsubsection{\macname{tcappto\#1from\#2}} |
| 715 |
|
| 716 |
This macro does not touch the input stream, per se. |
| 717 |
However, as tokens are popped from the input stream and placed into macros, |
| 718 |
this command conveniently allows for their aggregation |
| 719 |
Thus, if \macname{q} contains \texttt{abc} and the newly popped \macname{z} |
| 720 |
contains \texttt{d}, then \macname{tcappto |q from |z} will append the |
| 721 |
contents of \macname{z} to \macname{q}, so that, upon conclusion, |
| 722 |
\macname{q} will contain the tokens \texttt{abcd}. |
| 723 |
Unexpandable tokens can also be appended directly using this macro, using |
| 724 |
the syntax of \macname{tcappto |q from\{123\}}. |
| 725 |
Expandable tokens can also be added directly with the use of this macro, |
| 726 |
by using a leading \macname{noexpand} or \macname{empty}, as in |
| 727 |
\macname{tcappto |q from\{|noexpand|today\}}. |
| 728 |
|
| 729 |
|
| 730 |
\subsection{Truncating the input stream} |
| 731 |
|
| 732 |
The basic process of \TokCycle{} is one in which an input stream (or argument) |
| 733 |
of tokens is examined and processed based on directives |
| 734 |
set up by the user. |
| 735 |
Typically, the input stream is processed token-by-token to exhaustion, which |
| 736 |
occurs when a defined terminating token is reached. |
| 737 |
In section~\ref{s:escape}, we will see how a sequence of tokens can be exempted |
| 738 |
from any processing that would otherwise occur in the directive, |
| 739 |
and be passed instead directly to the output stream. |
| 740 |
|
| 741 |
However, in this section, we will examine how \TokCycle{} can be directed |
| 742 |
to \textit{dynamically} truncate, that is, discard the remaining |
| 743 |
input stream based on the tokens found therein. |
| 744 |
Truncation commands may be issued within the \Character, \Macro, |
| 745 |
and \Space{} directives. |
| 746 |
More will be said about the \Group{} directive in section~\ref{s:truncingrp}. |
| 747 |
|
| 748 |
Truncation can be made to apply either to the remainder of the current |
| 749 |
\TokCycle-nesting level (corresponding to an explicit catcode-1,2 |
| 750 |
group in the input stream)\footnote{ |
| 751 |
Note that implicit groups ({\ttfamily\macname{bgroup}...\macname{egroup}}) |
| 752 |
do not create a new \TokCycle{} nesting level, nor do |
| 753 |
{\ttfamily\macname{begingroup}...\macname{endgroup}}. |
| 754 |
Only explicit catcode-1,2 groups, in the nature of {\ttfamily\{...\}}, |
| 755 |
create a nested \TokCycle{} level. |
| 756 |
The current \TokCycle{} nesting level may be obtained from the \TeX{} count |
| 757 |
\macname{tcdepth}, which starts at zero upon \TokCycle{} entry. |
| 758 |
This count is incremented and decremented as groups are entered and |
| 759 |
exited in the input stream. |
| 760 |
It may be examined within the \CGMS{} directives in order to |
| 761 |
guide decisions. |
| 762 |
} |
| 763 |
or for the remainder of the total \TokCycle{} |
| 764 |
input stream. |
| 765 |
|
| 766 |
\subsubsection{\macname{truncategroup}\ and \macname{truncategroupiftokis}} |
| 767 |
\label{s:truncgrp} |
| 768 |
|
| 769 |
As to truncating the input stream for the remainder of the \TokCycle{} |
| 770 |
nesting level, the command is simply \macname{truncategroup}. |
| 771 |
So, for example, the directive |
| 772 |
|
| 773 |
\begin{verbbox}[\vbdelim] |
| 774 |
\Spacedirective{\truncategroup} |
| 775 |
\end{verbbox} |
| 776 |
\thevbox |
| 777 |
would direct \TokCycle{}, if it ever finds a (catcode-10) space token |
| 778 |
in the input stream, to discard the remainder of the tokens within |
| 779 |
the group in which that space was initially found. |
| 780 |
|
| 781 |
More often, however, one would desire to issue the truncation |
| 782 |
\textit{conditionally}. |
| 783 |
One could use one of the conditional commands of section~\ref{s:misc}, |
| 784 |
for example, |
| 785 |
|
| 786 |
\begin{verbbox}[\vbdelim] |
| 787 |
\tctestifcon{<condition>}{\truncategroup}{<code if condition not met>} |
| 788 |
\end{verbbox} |
| 789 |
\thevbox |
| 790 |
Alternatively, if the \textit{condition} is the occurrence of a particular |
| 791 |
single token in the input stream, one may use an abbreviated syntax: |
| 792 |
|
| 793 |
\begin{verbbox}[\vbdelim] |
| 794 |
\truncategroupiftokis{<tok>}{<code if condition not met>} |
| 795 |
\end{verbbox} |
| 796 |
\thevbox |
| 797 |
Thus, an example to echo macro tokens to the output stream, unless |
| 798 |
a \macname{relax} token is found, in which case terminate the group, |
| 799 |
would be |
| 800 |
|
| 801 |
\begin{verbbox}[\vbdelim] |
| 802 |
\Macrodirective{\truncategroupiftokis{\relax}{\addcytoks{#1}}} |
| 803 |
\end{verbbox} |
| 804 |
\thevbox |
| 805 |
Obviously, checks for particular character tokens would be placed in the |
| 806 |
\Character{} directive, rather than the \Macro{} directive. |
| 807 |
|
| 808 |
\subsubsection{\macname{truncatecycle}\ and \macname{truncatecycleiftokis}} |
| 809 |
|
| 810 |
There are two commands that are in every way analogous to the |
| 811 |
group-\hspace{0pt}terminating macros of section~\ref{s:truncgrp}. |
| 812 |
These commands are \macname{truncatecycle} and |
| 813 |
\macname{truncatecycleiftokis}. |
| 814 |
But in this case, all tokens are discarded to the end of the input |
| 815 |
stream, not just the current group (i.e., \TokCycle-nesting level). |
| 816 |
One point to note, however, is that open group levels will be |
| 817 |
closed by the \macname{truncatecycle} command, so that the tokens, |
| 818 |
both those being executed as well as those retained in |
| 819 |
\macname{cytoks}, will be group-balanced. |
| 820 |
|
| 821 |
\subsubsection{Truncating from within the \Group{} Directive} |
| 822 |
\label{s:truncingrp} |
| 823 |
|
| 824 |
The truncate commands described in prior sections may \textit{not} |
| 825 |
be executed from the \Group{} directive, as has already been |
| 826 |
mentioned. |
| 827 |
What does termination even mean in the \macname{Groupdirective} |
| 828 |
context? |
| 829 |
The \Group{} directive is executed whenever an explicit catcode-1,2 |
| 830 |
group is encountered in the input stream. |
| 831 |
The argument, \argone, to that directive will be the complete |
| 832 |
contents of the group (sans the catcode-1,2 delimiters). |
| 833 |
|
| 834 |
So, to truncate the group from the outset, not doing anything with |
| 835 |
\argone{} will accomplish the desired result. |
| 836 |
For example, to truncate all group-level-3 tokens, you could say, |
| 837 |
using the conditional commands of section~\ref{s:misc}, |
| 838 |
|
| 839 |
\begin{verbbox}[\vbdelim] |
| 840 |
\Groupdirective{\tctestifnum{\tcdepth=3}{}{\processtoks{#1}}} |
| 841 |
\end{verbbox} |
| 842 |
\thevbox |
| 843 |
There is no need to use an explicit \macname{truncategroup} macro |
| 844 |
to accomplish it. |
| 845 |
|
| 846 |
On the other hand, to truncate the complete token cycle if, for example, |
| 847 |
group-level-3 tokens are encountered, you have to set some sort of |
| 848 |
flag that is immediately picked up by one of the other directives. |
| 849 |
Here is an example of how that can be done: |
| 850 |
|
| 851 |
\begin{verbbox}[\vbdelim] |
| 852 |
\Groupdirective{\tctestifnum{\tcdepth=3}{\processtoks{\truncatenow}}% |
| 853 |
{\processtoks{#1}}} |
| 854 |
\Macrodirective{\truncatecycleiftokis{\truncatenow}{\addcytoks{#1}}} |
| 855 |
\end{verbbox} |
| 856 |
\thevbox |
| 857 |
Note that, as long as \macname{stripgroupingfalse} is active, the above |
| 858 |
examples will result in empty, rather than absent, level-3 groups, |
| 859 |
since the group open occurs before the truncation is processed. |
| 860 |
If the presence of even an empty group is to be avoided in such a case, |
| 861 |
one may employ the \macname{groupedcytoks} techniques described in |
| 862 |
section~\ref{s:grpcy}. |
| 863 |
|
| 864 |
\subsection{Escaping content from \TokCycle{} processing\label{s:escape}} |
| 865 |
|
| 866 |
There are times you may wish to prevent tokens in the \TokCycle{} |
| 867 |
input stream from being operated on by \TokCycle. |
| 868 |
Rather, you just want |
| 869 |
the content passed through unchanged to the output; that is, |
| 870 |
with the intent to have multi-token content bypass the |
| 871 |
\TokCycle{} directives altogether. |
| 872 |
|
| 873 |
\begin{sloppypar} |
| 874 |
The method developed by the package is to enclose the escaped |
| 875 |
content in the input stream between a set of \TokCycle{} escape |
| 876 |
characters, initially set to a vertical rule character found |
| 877 |
on the keyboard: ``$|\mkern1mu$''. |
| 878 |
The main proviso is that the escaped content cannot straddle a |
| 879 |
group boundary (the complete group may, however, be escaped). |
| 880 |
The escape character can be changed with |
| 881 |
\begin{verbbox}[\vbdelim] |
| 882 |
\settcEscapechar{<escape-token>}\end{verbbox} |
| 883 |
\theverbbox |
| 884 |
$\!\!\!$. |
| 885 |
\end{sloppypar} |
| 886 |
|
| 887 |
As of version 1.5 of the package, the specification of the escape character |
| 888 |
has been generalized. |
| 889 |
In addition to ASCII tokens such as ``$|\mkern1mu$'', unused macro tokens may be |
| 890 |
specified, such as \begin{verbbox}[\vbdelim] |
| 891 |
\settcEscapechar{\z}\end{verbbox} |
| 892 |
\theverbbox $\!\!\!$. |
| 893 |
|
| 894 |
|
| 895 |
\subsection{Flagged tokens} |
| 896 |
|
| 897 |
Certain token types are trapped and flagged via true/false |
| 898 |
\textit{if}-conditions. |
| 899 |
These \textit{if}-conditions can be examined within the appropriate |
| 900 |
directive (generally the \Character{} directive), to direct the |
| 901 |
course of action within the directive. |
| 902 |
|
| 903 |
\subsubsection{Active characters} |
| 904 |
|
| 905 |
\paragraph{\macname{ifactivetok}:} |
| 906 |
|
| 907 |
Active (cat-13) tokens that occur in the input stream result in the |
| 908 |
flag \macname{ifactivetok} being set \macname{activetoktrue}. |
| 909 |
Note that the expansion of the token's active \macname{def} |
| 910 |
occurs \textit{after} \TokCycle{} processing. |
| 911 |
With active \macname{let}'s, there is no text substitution; however, |
| 912 |
the assignment is already active at the time of \TokCycle{} processing. |
| 913 |
% |
| 914 |
The only exception to this rule is with pre-expanded input, |
| 915 |
\macname{expandedtokcycle[xpress]}. |
| 916 |
If an active token's substitution is governed by a \macname{def}, the |
| 917 |
text substitution will have occurred before reaching the token cycle. |
| 918 |
|
| 919 |
The disposition of active tokens will depend on their actual definition. They could |
| 920 |
be directed to the Character, Macro, or Space directive as a result. |
| 921 |
As an example, whereas non-active UNICODE characters would show up in the |
| 922 |
Character directive when using lualatex or xelatex, |
| 923 |
multi-byte UNICODE characters may be discerned (in pdflatex) by |
| 924 |
looking for and testing active tokens in the Macro directive. |
| 925 |
An example of this is shown in the companion document depicting examples of use. |
| 926 |
|
| 927 |
\paragraph{\macname{ifactivetokunexpandable}:} |
| 928 |
|
| 929 |
This flag is similar to \macname{ifactivetok}, in that a token must |
| 930 |
be active for this to be set true. |
| 931 |
However, in addition, it is only |
| 932 |
true if the active token is \macname{let} to a character or a |
| 933 |
primitive, neither of which can be expanded. |
| 934 |
Active characters assigned via \macname{def} or else \macname{let} to a |
| 935 |
macro will \textit{not} qualify as \macname{activetokunexpandabletrue}. |
| 936 |
|
| 937 |
\paragraph{\macname{ifactivechar}:} |
| 938 |
|
| 939 |
This flag, rather than testing the token, tests the character code of |
| 940 |
the token, to see if it is set active. |
| 941 |
Generally, the token and its character code will be syncronized |
| 942 |
in their \textit{activeness}. |
| 943 |
However, if a token is tokenized when active, but the corresponding character |
| 944 |
code is made non-active in the meantime, prior to the token reaching |
| 945 |
\TokCycle{} processing, this flag will be set \macname{activecharfalse}. |
| 946 |
A similar discrepency will arise if a token is not active when tokenized, |
| 947 |
but the character code is made active in the interim, prior to |
| 948 |
\TokCycle{} processing. |
| 949 |
|
| 950 |
\subsubsection{Implicit tokens: \macname{ifimplicittok}} |
| 951 |
|
| 952 |
Implicit tokens, those assigned via \macname{let} to characters\footnote{% |
| 953 |
Some clarification may be needed. |
| 954 |
Control sequences and active characters that are \macname{let} |
| 955 |
to something other than a cat-0 control sequence will be flagged |
| 956 |
as implicit. |
| 957 |
If implicit, a token will be processed |
| 958 |
through the \Character{} directive (exceptions noted). |
| 959 |
On the other hand, if a control sequence or active character is \macname{let} |
| 960 |
to a cat-0 control sequence, it will be directed to the \Macro{} |
| 961 |
directive for processing, without the implicit flag. |
| 962 |
}% |
| 963 |
, are flagged with |
| 964 |
a \texttt{true} setting for \macname{ifimplicittok}. |
| 965 |
Generally, implicit tokens will be directed to the \Character{} |
| 966 |
directive for processing. |
| 967 |
There are, however, two exceptions: |
| 968 |
i)~implicit grouping tokens (e.g., \macname{bgroup} and \macname{egroup}) |
| 969 |
will not appear in any directive unless the |
| 970 |
\macname{stripimplicitgroupingcase} has been set to $-$1; and |
| 971 |
ii)~implicit space tokens (e.g., \macname{@sptoken}) will be processed |
| 972 |
by the \Space{} directive. |
| 973 |
|
| 974 |
\subsubsection{Active-implicit tokens, including spaces\label{s:acimsp}} |
| 975 |
|
| 976 |
One may occasionally run across a token that is both active and implicit. |
| 977 |
For example, in the following code, \texttt{Q} is made both active and implicit: |
| 978 |
|
| 979 |
\begin{verbbox}[\vbdelim] |
| 980 |
\catcode`Q=\active |
| 981 |
\let Qx |
| 982 |
\end{verbbox} |
| 983 |
\thevbox |
| 984 |
In general, both \macname{ifactivetok} and \macname{ifimplicittok} |
| 985 |
tests can be performed together to determine such cases. |
| 986 |
|
| 987 |
This is true even in the case of active-implicit catcode-10 spaces, which |
| 988 |
are always processed through the \Space{} directive. |
| 989 |
As of \TokCycle{} v1.4, the actual active-implicit space, if |
| 990 |
encountered in the input stream, will be passed as \verb|#1| to |
| 991 |
the \Space{} directive, \textit{as long as the character code of the |
| 992 |
token is still \macname{active}.} |
| 993 |
If the character code of that token is no longer active, a generic |
| 994 |
implicit space token named \macname{tcsptoken} is instead passed |
| 995 |
to the the \Space{} directive as \verb|#1|. |
| 996 |
In either case, the catcode-12 version of the active character that |
| 997 |
was digested will be, for that moment, retained in a definition |
| 998 |
named \macname{theactivespace}. |
| 999 |
This can be useful if detokenization is required of the spaces. |
| 1000 |
Such an example is described in the \texttt{tokcycle-examples} |
| 1001 |
adjunct document. |
| 1002 |
|
| 1003 |
\subsubsection{Parameter (cat-6) tokens (e.g., \texttt{\#}): \macname{ifcatSIX}} |
| 1004 |
\label{s:cat6} |
| 1005 |
|
| 1006 |
Typically, category-code 6 tokens (like \texttt{\#}) are used to |
| 1007 |
designate a parameter (e.g., \argone--\texttt{\#9}). |
| 1008 |
Since they are unlikely to be used in that capacity inside a |
| 1009 |
tokcycle input stream, the package behavior is to convert them |
| 1010 |
into something cat-12 and set the \textit{if}-condition \macname{catSIXtrue}. |
| 1011 |
In this manner, \macname{ifcatSIX} can be used inside the |
| 1012 |
\Character{} directive to trap and convert cat-6 tokens into something of |
| 1013 |
the user's choosing. |
| 1014 |
|
| 1015 |
\begin{sloppypar} |
| 1016 |
As to the default nature of this conversion (if no special actions are |
| 1017 |
taken), explicit cat-6 characters are converted |
| 1018 |
into the identical character with category code of 12. |
| 1019 |
On the other hand, implicit cat-6 macros (e.g., \verb|\let\myhash#|) |
| 1020 |
are converted into a fixed-name macro, \macname{implicitsixtok}, |
| 1021 |
whose cat-12 substitution text is a \macname{string} of the original |
| 1022 |
implicit-macro name. |
| 1023 |
\end{sloppypar} |
| 1024 |
|
| 1025 |
\subsubsection{Parameters (\argone--\texttt{\#9}): |
| 1026 |
\macname{whennotprocessingparameter}} |
| 1027 |
\label{s:whennot} |
| 1028 |
|
| 1029 |
While, generally, one would not intend to use parameters |
| 1030 |
in the \TokCycle{} input stream, the package provides, not a flag, |
| 1031 |
but a macro to allow it. |
| 1032 |
The macro is to be used \textit{within} the \Character{} directive |
| 1033 |
with the syntax:\smallskip |
| 1034 |
|
| 1035 |
\begin{verbbox}[\vbdelim] |
| 1036 |
\whennotprocessingparameter#1{<non-parameter-processing-directive>}\end{verbbox} |
| 1037 |
\theverbbox\smallskip |
| 1038 |
|
| 1039 |
\noindent Here, the \argone{} doesn't refer to \argone{} as |
| 1040 |
it might appear in the input stream, but to the sole parameter of |
| 1041 |
the \Character{} directive, referring to the current token being |
| 1042 |
processed. |
| 1043 |
With this syntax, when the token under consideration is a |
| 1044 |
parameter (e.g., \argone--\texttt{\#9}), that parameter is |
| 1045 |
added to the \macname{cytoks} register. |
| 1046 |
If the token under consideration is not a parameter, the code |
| 1047 |
in the final argument is executed. |
| 1048 |
|
| 1049 |
\subsection{Misc: general \textit{if}-condition tools} |
| 1050 |
\label{s:misc} |
| 1051 |
|
| 1052 |
\TeX{} comes equipped with a variety of \macname{if...} condition |
| 1053 |
primitives. |
| 1054 |
When dealing with macros for which the order of expansion is important, |
| 1055 |
the \macname{else} and \macname{fi} can sometimes get in the way |
| 1056 |
of proper expansion and execution. |
| 1057 |
These four restructured \textit{if} macros came in handy writing the |
| 1058 |
package, and may be of use in creating your directives, |
| 1059 |
preventing \macname{else} and \macname{fi} from getting in the |
| 1060 |
way:\medskip |
| 1061 |
|
| 1062 |
\begin{verbbox}[\vbdelim] |
| 1063 |
\tctestifcon{<TeX-if-condition>}{<true-code>}{<false-code>} |
| 1064 |
\tctestifx{<\ifx-comparison-toks>}{<true-code>}{<false-code>} |
| 1065 |
\tctestifnum{<\ifnum-condition>}{<true-code>}{<false-code>} |
| 1066 |
\tctestifcatnx<tok1><tok2>{<true-code>}{<false-code>} |
| 1067 |
\end{verbbox} |
| 1068 |
\theverbbox |
| 1069 |
|
| 1070 |
\medskip\noindent |
| 1071 |
Note that unlike those macros requiring three arguments, the lone |
| 1072 |
exception is \macname{tctestifcatnx}, which requires four, comparing the |
| 1073 |
catcodes of the two input toks \textbf{without expansion} (\texttt{nx} |
| 1074 |
denotes \macname{noexpand}). |
| 1075 |
|
| 1076 |
\subsection{\TokCycle{} completion: \macname{aftertokcycle}\ |
| 1077 |
and \macname{tcendgroup}} |
| 1078 |
|
| 1079 |
The \TokCycle{} \textit{macros}, upon completion, do nothing. |
| 1080 |
Unlike \macname{tokencycle} \textit{environments}, they don't even |
| 1081 |
output \macname{the}\macname{cytoks} token register. |
| 1082 |
A command has been provided, \macname{aftertokcycle}, which takes |
| 1083 |
an argument to denote the actions to be executed following |
| 1084 |
completion of \textit{all} subsequent \TokCycle{} \textit{macro} |
| 1085 |
invocations within the scope of the current group. |
| 1086 |
It might be specified as simply as \verb|\aftertokcycle{\the\cytoks}|, |
| 1087 |
so as to have the output stream automatically typeset by |
| 1088 |
future invocations of the \macname{tokcycle} macro. |
| 1089 |
|
| 1090 |
The meaning of \macname{aftertokcycle} can be reset with |
| 1091 |
\macname{aftertokcycle\{\}}, but is also reset as part of |
| 1092 |
\macname{resettokcycle}. |
| 1093 |
Unlike macros, the \TokCycle{} environments are unaffected |
| 1094 |
by \macname{aftertokcycle}, as they actually set it locally |
| 1095 |
(within their grouped scope) to accomplish their own purposes. |
| 1096 |
|
| 1097 |
Speaking of the \TokCycle{} environments, there is a command employed by |
| 1098 |
the environments \macname{tokencycle} and \macname{tokencyclexpress} |
| 1099 |
that is likewise available to the user: \macname{tcendgroup}. |
| 1100 |
This is a version of \macname{endgroup} that additionally transmits the |
| 1101 |
current value of the \macname{cytoks} token list \textit{outside of |
| 1102 |
the group}. |
| 1103 |
So if a user wishes to confine certain \TokCycle{} activity inside a |
| 1104 |
group, but wishes to have access to the \macname{cytoks} result afterward, |
| 1105 |
he may simply wrap the activity in \verb|\begingroup...\tcendgroup|. |
| 1106 |
|
| 1107 |
\subsection{Accommodating catcode-1,2 changes: \macname{settcGrouping}% |
| 1108 |
\label{s:grouping}} |
| 1109 |
|
| 1110 |
In order to avoid making the \TokCycle{} parser overly complex, requiring |
| 1111 |
multiple passes of the input stream, the package defaults to using |
| 1112 |
catcode-1,2 braces \texttt{\{~\}} to bring about grouping in the |
| 1113 |
output stream, regardless of what the actual cat-1,2 tokens are in |
| 1114 |
the input stream. |
| 1115 |
As long as their sole purpose in the token |
| 1116 |
cycle is for grouping and scoping, this arrangement will produce |
| 1117 |
the expected output. |
| 1118 |
|
| 1119 |
However, if the actual character-code of these tokens is important |
| 1120 |
to the result (e.g., when detokenized), there is one other option. |
| 1121 |
The package allows the external specification of which cat-1,2 |
| 1122 |
tokens should be used in the \TokCycle{} output stream. |
| 1123 |
The syntax is \verb|\settcGrouping{{#1}}|, to use the standard braces |
| 1124 |
for this purpose (default). |
| 1125 |
If angle-brackets \verb|< >| were to be the \textit{new} |
| 1126 |
grouping tokens, then, after their catcodes were changed, and |
| 1127 |
\macname{bgroup} and \macname{egroup} were reassigned, one would |
| 1128 |
invoke \verb|\settcGrouping<<#1>>|. |
| 1129 |
These will then be the grouping tokens in the \TokCycle{} output |
| 1130 |
stream until set to something else. |
| 1131 |
|
| 1132 |
\section{Usage Examples} |
| 1133 |
|
| 1134 |
See the adjunct file, \texttt{tokcycle-examples.pdf}, for an |
| 1135 |
array of \TokCycle{} examples. |
| 1136 |
|
| 1137 |
\section{Summary of known package limitations} |
| 1138 |
|
| 1139 |
The goal of this package is not to build the perfect token-stream |
| 1140 |
parser. |
| 1141 |
It is, rather, to provide the means for users to build useful |
| 1142 |
token-processing tools for their \TeX/\LaTeX{} documents. |
| 1143 |
|
| 1144 |
What follows are the known limitations of the package. |
| 1145 |
which arise, in part, from the single-pass parsing algorithm |
| 1146 |
embedded in the package. |
| 1147 |
Surely, there are more cases associated with arcane catcode-changing |
| 1148 |
syntax that are not accounted for; |
| 1149 |
I encourage you to bring them to my attention. |
| 1150 |
If I can't fix them, I can at least disclaim and declaim them below: |
| 1151 |
|
| 1152 |
\begin{itemize} |
| 1153 |
|
| 1154 |
\item One must inform the package (via \macname{settcGrouping}) of |
| 1155 |
changes to the cat-1,2 tokens \textit{if} |
| 1156 |
there is a need to detokenize the output with the specified |
| 1157 |
bracing group; however, |
| 1158 |
grouping will still be handled properly (i.e., cat-1,2 tokens will |
| 1159 |
be detected), even if the package is not notified. |
| 1160 |
See section~\ref{s:grouping}. |
| 1161 |
|
| 1162 |
\item Should one need to keep track of the \textit{names} of implicit |
| 1163 |
cat-6 tokens (e.g., \verb|\svhash| following \verb|\let\svhash#|), |
| 1164 |
care must be exercised. |
| 1165 |
When encountered in the input stream, a cat-12 \macname{string} of |
| 1166 |
the implicit-cat-6 token \textit{name} is stored in the \macname{implicitsixtok} |
| 1167 |
macro. |
| 1168 |
It is the \macname{implicitsixtok} token which is passed to the |
| 1169 |
\Character{} directive as \verb|#1|, in lieu of the actual cat-6 token. |
| 1170 |
If there is at most a single implicit-cat-6 named token in the input stream, |
| 1171 |
then \macname{implicitsixtok} will reflect its name. |
| 1172 |
However, if there is more than one, \macname{implicitsixtok} will be redefined |
| 1173 |
on the fly with each occurrence. |
| 1174 |
|
| 1175 |
Therefore, should one intend to add more than one such token \textit{name} |
| 1176 |
to the token list by way of \macname{addcytoks}, \textit{it must be expanded |
| 1177 |
first} (e.g., \macname{addcytoks[1]\{\argone\}}), in order to capture the |
| 1178 |
\textit{name} of that particular cat-6 implicit token, rather than the name |
| 1179 |
of the last one in the input stream. |
| 1180 |
There is no similar problem for explicit cat-6 tokens. |
| 1181 |
In any event, all cat-6 tokens are trapped and flag as \texttt{true} |
| 1182 |
the \macname{ifcatSIX} condition, which is how the user should detect |
| 1183 |
their presence. |
| 1184 |
|
| 1185 |
\item A similar warning is to be made for active-implicit spaces as was |
| 1186 |
made for implicit-cat-6 tokens. |
| 1187 |
In the case of active-implicit spaces, the name (a cat-12 string) of the |
| 1188 |
active character is momentarily stored in the \macname{theactivespace} macro. |
| 1189 |
If one wishes to output the active \textit{name} to the \macname{cytoks} token |
| 1190 |
list, one must make sure that \macname{theactivespace} is expanded once before |
| 1191 |
being added to the token list. |
| 1192 |
|
| 1193 |
One difference here, however, is that, unlike a cat-6 token, an |
| 1194 |
active-implicit space (and not a string of it) is actually passed |
| 1195 |
to the \Space{} directive as \texttt{\#1}. |
| 1196 |
In all but one special case, the active-implicit \texttt{\#1} will match that |
| 1197 |
which occurred in the input stream (see section~\ref{s:acimsp}). |
| 1198 |
|
| 1199 |
\end{itemize} |
| 1200 |
|
| 1201 |
\section*{Acknowledgments} |
| 1202 |
|
| 1203 |
In addition to Christian Tellechea, a contributor to this |
| 1204 |
package, |
| 1205 |
the author would like to thank Dr.\@ David Carlisle for his |
| 1206 |
assistance in understanding some of the nuances of token |
| 1207 |
registers. |
| 1208 |
Likewise, his explanation about how a space token is defined |
| 1209 |
in \TeX{} (see \verb|https://tex.stackexchange.com|\allowbreak |
| 1210 |
\verb|/questions/64197/|\allowbreak\verb|pgfparser-module-and-blank-spaces/64200#64200|) |
| 1211 |
proved to be useful here. |
| 1212 |
The \texttt{tex.stackexchange} site provides a wonderful |
| 1213 |
opportunity to interact with the leading developers and |
| 1214 |
practitioners of \TeX{} and \LaTeX{}. |
| 1215 |
|
| 1216 |
%\vspace{1.6in} |
| 1217 |
\section*{Source Code} |
| 1218 |
|
| 1219 |
%\vspace{-0.1in} |
| 1220 |
\Large\texttt{tokcycle.sty} |
| 1221 |
\verbfilenobox[\footnotesize]{tokcycle.sty}% |
| 1222 |
\vspace{0.5in} |
| 1223 |
|
| 1224 |
\noindent\texttt{tokcycle.tex} |
| 1225 |
\verbfilenobox[\footnotesize]{tokcycle.tex} |
| 1226 |
|
| 1227 |
\end{document} |
| 1228 |
|