| 1 |
\documentclass[10pt]{article} |
| 2 |
\usepackage[margin=1.3in]{geometry} |
| 3 |
\usepackage[T2A,T1]{fontenc} |
| 4 |
\usepackage{tokcycle,xcolor,manfnt,lmodern,mathtools} |
| 5 |
%%%%TRIAL PACKAGE CHANGES (MAKE SURE EXAMPLE FILE COMPILES CORRECTLY) |
| 6 |
\makeatletter |
| 7 |
\makeatother |
| 8 |
%%%%END TRIAL |
| 9 |
\colorlet{bred}{red!90!blue} |
| 10 |
\usepackage{tikz} |
| 11 |
% MACROS: REDEFINE TO ALLOW \cytoks TO ESCAPE SCOPE OF exampleA |
| 12 |
\aftertokcycle{\global\cytoks\expandafter{\the\cytoks}} |
| 13 |
% ENVIRONMENTS: REDEFINE TO ALLOW \cytoks TO ESCAPE SCOPE OF exampleA |
| 14 |
\makeatletter\renewcommand\tcenvscope{\global\cytoks}\makeatother |
| 15 |
|
| 16 |
\def\ddbend{\setbox0=\hbox{\dbend\dbend}\leavevmode\llap{% |
| 17 |
\raisebox{10pt}{\box0~}}} |
| 18 |
|
| 19 |
\usepackage[skins,listings]{tcolorbox} |
| 20 |
|
| 21 |
\newtcblisting{exampleA}[2][]{% |
| 22 |
colframe=red!70!yellow!50!black, |
| 23 |
colback=red!75!yellow!5!white, |
| 24 |
coltitle=red!50!yellow!3!white, |
| 25 |
bicolor,colbacklower=white, |
| 26 |
top=0mm,bottom=0mm,left=0mm,right=0mm, |
| 27 |
arc=1mm,boxrule=1pt, |
| 28 |
fonttitle=\sffamily\bfseries, |
| 29 |
sidebyside, |
| 30 |
title=#2,#1} |
| 31 |
|
| 32 |
\newtcblisting{exampleB}[2][]{% |
| 33 |
colframe=red!50!yellow!50!black, |
| 34 |
colback=red!50!yellow!5!white,%white, |
| 35 |
coltitle=red!50!yellow!3!white, |
| 36 |
bicolor,colbacklower=white,%red!50!yellow!5!white, |
| 37 |
fonttitle=\sffamily\bfseries, |
| 38 |
sidebyside,text and listing, |
| 39 |
top=0mm,bottom=0mm,left=0mm,right=0mm, |
| 40 |
arc=1mm,boxrule=1pt, |
| 41 |
title=#2,#1} |
| 42 |
|
| 43 |
\newtcblisting{exampleC}[2][]{% |
| 44 |
colframe=cyan!70!yellow!50!black, |
| 45 |
colback=cyan!30!yellow!5!white,%white, |
| 46 |
coltitle=cyan!50!yellow!3!white, |
| 47 |
fonttitle=\sffamily\bfseries, |
| 48 |
listing only, |
| 49 |
top=0mm,bottom=0mm,left=0mm,right=0mm, |
| 50 |
arc=1mm,boxrule=1pt, |
| 51 |
title=#2,#1} |
| 52 |
|
| 53 |
\newtcblisting{exampleD}[2][]{% |
| 54 |
colframe=red!70!yellow!50!black, |
| 55 |
colback=red!20!yellow!5!white, |
| 56 |
coltitle=red!50!yellow!3!white, |
| 57 |
fonttitle=\sffamily\bfseries, |
| 58 |
text only, |
| 59 |
top=0mm,bottom=0mm,left=0mm,right=0mm, |
| 60 |
arc=1mm,boxrule=1pt, |
| 61 |
title=#2,#1} |
| 62 |
|
| 63 |
\def\altdecytoks{\par\medskip\begingroup\noindent\macname{cytoks} |
| 64 |
\ \textit{alt}detokenization:\\\raggedright\footnotesize\ttfamily |
| 65 |
\expandafter\altdetokenize\expandafter{\the\cytoks}\normalsize\par |
| 66 |
\endgroup\medskip} |
| 67 |
|
| 68 |
\newcommand\TokCycle{\textsf{tokcycle}} |
| 69 |
|
| 70 |
\fboxsep=1.5pt |
| 71 |
\DeclareUnicodeCharacter{1D60}{$\varphi$}%{\fbox{U+1D60}} |
| 72 |
\DeclareUnicodeCharacter{1D131}{$\sharp$}%{\fbox{U+1D131}} |
| 73 |
|
| 74 |
\parindent0pt |
| 75 |
\parskip0pt |
| 76 |
\begin{document} |
| 77 |
{\centering\LARGE \TokCycle{} Package Examples\par\smallskip |
| 78 |
\large\today\par} |
| 79 |
|
| 80 |
\tableofcontents |
| 81 |
|
| 82 |
%%%%%%%%%%%%%% SUPPORT MACRO |
| 83 |
|
| 84 |
\newcommand\macname[1]{\texttt{\char92#1}} |
| 85 |
|
| 86 |
\newcommand\underdot[1]{\ooalign{#1\cr\hfil{\raisebox{-5pt}{.}}\hfil}} |
| 87 |
|
| 88 |
\newcounter{colorindex} |
| 89 |
|
| 90 |
\newcommand\restorecolor{\setcounter{colorindex}{100}} |
| 91 |
|
| 92 |
\newcommand\reducecolor[1]{% |
| 93 |
\color{red!\thecolorindex!cyan}% |
| 94 |
\addtocounter{colorindex}{-#1}% |
| 95 |
\ifnum\thecolorindex<1\relax\setcounter{colorindex}{1}\fi |
| 96 |
} |
| 97 |
|
| 98 |
\newif\ifmacro |
| 99 |
\newcommand\altdetokenize[1]{\begingroup\stripgroupingtrue\macrofalse |
| 100 |
\stripimplicitgroupingcase{-1}% |
| 101 |
\tokcycle |
| 102 |
{\ifmacro\def\tmp{##1}\ifcat\tmp A\else\unskip\allowbreak\fi\macrofalse\fi |
| 103 |
\detokenize{##1}\ifx##1\bgroup\unskip\fi\ifx##1\egroup\unskip\fi} |
| 104 |
{\ifmacro\unskip\macrofalse\fi\{\processtoks{##1}\ifmacro\unskip\fi\}\allowbreak} |
| 105 |
{\tctestifx{\\##1}{\\}{\ifmacro\unskip\allowbreak\fi |
| 106 |
\allowbreak\detokenize{##1}\macrotrue}} |
| 107 |
{ \hspace{0pt plus 3em minus .3ex}} |
| 108 |
{#1}% |
| 109 |
\unskip |
| 110 |
\endgroup} |
| 111 |
|
| 112 |
\newcommand\plusl[1]{\char\numexpr`#1+1\relax} |
| 113 |
|
| 114 |
\newcommand\vowelcap[1]{% |
| 115 |
\ifx a#1A\else |
| 116 |
\ifx e#1E\else |
| 117 |
\ifx i#1I\else |
| 118 |
\ifx o#1O\else |
| 119 |
\ifx u#1U\else |
| 120 |
#1\fi\fi\fi\fi\fi |
| 121 |
} |
| 122 |
|
| 123 |
\newcommand\spaceouttext[2]{% |
| 124 |
\tokcycle |
| 125 |
{\addcytoks{##1\nobreak\hspace{#1}}}% |
| 126 |
{\processtoks{##1}} |
| 127 |
{\addcytoks{##1}}% |
| 128 |
{\addcytoks{##1\hspace{#1}}} |
| 129 |
{#2}% |
| 130 |
\the\cytoks\unskip} |
| 131 |
|
| 132 |
\newcommand\growdim[2]{% |
| 133 |
\tokcycle{\addcytoks{##1}} |
| 134 |
{\addcytoks{#1\dimexpr##1}} |
| 135 |
{\addcytoks{##1}} |
| 136 |
{\addcytoks{##1}}{% |
| 137 |
#2}% |
| 138 |
\the\cytoks} |
| 139 |
|
| 140 |
\newcommand\nextcap[1]{% |
| 141 |
\edef\tmp{#1}% |
| 142 |
\tctestifx{-#1}{\def\capnext{T}}{}% <- TEST FOR HYPHEN |
| 143 |
\tctestifcon{\if T\capnext}% |
| 144 |
{\tctestifcon{\ifcat\tmp A}% <- TEST FOR NEXT CAT-11 |
| 145 |
{\addcytoks{\uppercase{#1}}\def\capnext{F}}% |
| 146 |
{\addcytoks{#1}}}% |
| 147 |
{\addcytoks{#1}}% |
| 148 |
} |
| 149 |
|
| 150 |
\newcommand\TitleCase[1]{% |
| 151 |
\def\capnext{T}% <- INITIAL ENTRY |
| 152 |
\tokcycle |
| 153 |
{\nextcap{##1}} |
| 154 |
{\def\capnext{T}\processtoks{##1}}% <- GROUP ENTRY |
| 155 |
{\addcytoks{##1}} |
| 156 |
{\addcytoks{##1}\def\capnext{T}}% <-CAT-10 SPACE |
| 157 |
{#1}% |
| 158 |
\the\cytoks |
| 159 |
} |
| 160 |
|
| 161 |
\tokcycleenvironment\spaceBgone |
| 162 |
{\addcytoks{##1}} |
| 163 |
{\processtoks{##1}} |
| 164 |
{\addcytoks{##1}} |
| 165 |
{\addcytoks{\hspace{.2pt plus .2pt minus .8pt}}}% |
| 166 |
|
| 167 |
|
| 168 |
\tokcycleenvironment\remaptext |
| 169 |
{\addcytoks[x]{\tcremap{##1}}} |
| 170 |
{\processtoks{##1}} |
| 171 |
{\addcytoks{##1}} |
| 172 |
{\addcytoks{##1}} |
| 173 |
\newcommand*\tcmapto[2]{\expandafter\def\csname tcmapto#1\endcsname{#2}} |
| 174 |
\newcommand*\tcremap[1]{\ifcsname tcmapto#1\endcsname |
| 175 |
\csname tcmapto#1\expandafter\endcsname\else\expandafter#1\fi} |
| 176 |
\tcmapto am \tcmapto bf \tcmapto cz \tcmapto de \tcmapto ey |
| 177 |
\tcmapto fl \tcmapto gx \tcmapto hb \tcmapto ic \tcmapto jn |
| 178 |
\tcmapto ki \tcmapto lr \tcmapto mh \tcmapto nt \tcmapto ok |
| 179 |
\tcmapto ps \tcmapto qa \tcmapto ro \tcmapto sq \tcmapto tw |
| 180 |
\tcmapto uj \tcmapto vp \tcmapto wd \tcmapto xg \tcmapto yu |
| 181 |
\tcmapto zv |
| 182 |
|
| 183 |
\tokcycleenvironment\greek% NEW TOKCYCLE ENVIRONMENT: |
| 184 |
{\addcytoks[4]{\tcremap{##1}}}% HOW TO TREAT CHARACTERS |
| 185 |
{\processtoks{##1}}% HOW TO TREAT GROUP CONTENTS |
| 186 |
{\addcytoks{##1}}% HOW TO TREAT MACROS |
| 187 |
{\addcytoks{##1}}% HOW TO TREAT SPACES |
| 188 |
|
| 189 |
\newcommand\findinstring[2]{\begingroup% |
| 190 |
\stripgroupingtrue |
| 191 |
\setcounter{runcount}{0}% |
| 192 |
\tokcycle |
| 193 |
{\nextctltok{##1}} |
| 194 |
{\nextctltok{\opengroup}\processtoks{##1}\nextctltok{\closegroup}} |
| 195 |
{\nextctltok{##1}} |
| 196 |
{\nextctltok{\tcspace}} |
| 197 |
{#1}% |
| 198 |
\edef\numlet{\theruncount}% |
| 199 |
\expandafter\def\expandafter\searchword\expandafter{\the\cytoks}% |
| 200 |
% |
| 201 |
\aftertokcycle{\matchfound}% |
| 202 |
\setcounter{runcount}{0}% |
| 203 |
\def\matchfound{F}% |
| 204 |
\tokcycle |
| 205 |
{\nextcmptok{##1}} |
| 206 |
{\nextcmptok{\opengroup}\processtoks{##1}\nextcmptok{\closegroup}} |
| 207 |
{\nextcmptok{##1}} |
| 208 |
{\nextcmptok{\tcspace}} |
| 209 |
{#2}% |
| 210 |
\endgroup} |
| 211 |
\newcounter{runcount} |
| 212 |
\makeatletter |
| 213 |
\newcommand\rotcytoks[1]{\cytoks\expandafter\expandafter\expandafter{% |
| 214 |
\expandafter\tc@gobble\the\cytoks#1}} |
| 215 |
\makeatother |
| 216 |
\newcommand\testmatch[1]{\ifx#1\searchword\gdef\matchfound{T}\fi}% |
| 217 |
\newcommand\rotoradd[2]{\stepcounter{runcount}% |
| 218 |
\ifnum\theruncount>\numlet\relax#1\else#2\fi |
| 219 |
\expandafter\def\expandafter\tmp\expandafter{\the\cytoks}} |
| 220 |
\newcommand\nextcmptok[1]{\rotoradd{\rotcytoks{#1}}{\addcytoks{#1}}\testmatch{\tmp}} |
| 221 |
\newcommand\nextctltok[1]{\stepcounter{runcount}\addcytoks{#1}} |
| 222 |
|
| 223 |
\newcounter{sigd} |
| 224 |
\newcounter{trailingz} |
| 225 |
\newif\iffounddot |
| 226 |
\newif\ifzerosig |
| 227 |
\newcommand\sigdigits[1]{% |
| 228 |
\setcounter{sigd}{0}% SIGNIFICANT DIGIT COUNT |
| 229 |
\setcounter{trailingz}{0}% TRAILING ZERO COUNT |
| 230 |
\founddotfalse% MADE T WHEN DECIMAL HAS BEEN 1ST LOCATED |
| 231 |
\zerosigfalse% MADE T WHEN ZERO BECOMES SIGNIFICANT |
| 232 |
\tokcycle% CYCLE THROOUGH EACH TOKEN |
| 233 |
{\tctestifx{.##1}% |
| 234 |
{\iffounddot\throwerr\fi\founddottrue}% IF . |
| 235 |
{\tctestifnum{`##1>`/}% |
| 236 |
{\tctestifnum{`##1<`:}% |
| 237 |
{\tctestifnum{##1>0}% |
| 238 |
{\stepcounter{sigd}\zerosigtrue\setcounter{trailingz}{0}}% IF 1-9 |
| 239 |
{\ifzerosig\stepcounter{sigd}\fi\stepcounter{trailingz}}% IF 0 |
| 240 |
}% |
| 241 |
{\throwerr}% IF ASCII > `9 |
| 242 |
}% |
| 243 |
{\throwerr}% IF ASCII < `0 |
| 244 |
}% |
| 245 |
}% APPLY ABOVE LOGIC FOR CHAR TOKENS |
| 246 |
{\throwerr}% IF BRACES |
| 247 |
{\throwerr}% IF CONTROL SEQUENCE |
| 248 |
{}% IGNORE SPACES |
| 249 |
{#1}% THE ARGUMENT |
| 250 |
\iffounddot\else\addtocounter{sigd}{-\thetrailingz}\fi% TRAILING ZEROS |
| 251 |
\expandafter\ifx\expandafter\relax\detokenize\expandafter{\the\cytoks}% |
| 252 |
\relax\else\setcounter{sigd}{-1}\fi% CHECK FOR INVALID INPUT |
| 253 |
\thesigd |
| 254 |
} |
| 255 |
\newcommand\throwerr{\addcytoks{##1}}% ADD INVALID TOKS TO \cytoks |
| 256 |
|
| 257 |
\newcounter{Tcount} |
| 258 |
\xtokcycleenvironment\capTive% ENVIRONMENT NAME |
| 259 |
{\tctestifx{t##1}{\stepcounter{Tcount}\addcytoks{T}}{\addcytoks{##1}}}% CHAR. DIRECTIVE |
| 260 |
{\processtoks{##1}}% GROUP DIRECTIVE |
| 261 |
{\addcytoks{##1}}% MACRO DIRECTIVE |
| 262 |
{\addcytoks{##1}}% SPACE DIRECTIVE |
| 263 |
{\setcounter{Tcount}{0}\color{red}\tcafterenv{\ Here}}% INITIALIZATION CODE |
| 264 |
{\theTcount{} t's capitalized: \addcytoks{!!}}% CLOSE-OUT CODE |
| 265 |
|
| 266 |
\newcounter{wordcount} |
| 267 |
\newcounter{lettercount} |
| 268 |
\newif\ifinword |
| 269 |
\newif\ifrunningcount |
| 270 |
\newcommand\dumpword{% |
| 271 |
\addcytoks[1]{\accumword}% |
| 272 |
\tctestifcon\ifinword{\stepcounter{wordcount}% |
| 273 |
\ifrunningcount\addcytoks[x]{$^{\thewordcount,\thelettercount}$}\fi}{}% |
| 274 |
\inwordfalse\def\accumword{}} |
| 275 |
\makeatletter |
| 276 |
\newcommand\addletter[1]{% |
| 277 |
\tctestifcatnx A#1{\stepcounter{lettercount}\inwordtrue}{\dumpword}% |
| 278 |
\tc@defx\accumword{\accumword#1}} |
| 279 |
\makeatother |
| 280 |
|
| 281 |
\xtokcycleenvironment\countem |
| 282 |
{\addletter{##1}} |
| 283 |
{\processtoks{##1}\dumpword} |
| 284 |
{\dumpword\addcytoks{##1}} |
| 285 |
{\dumpword\addcytoks{##1}} |
| 286 |
{\def\accumword{}\setcounter{wordcount}{0}\setcounter{lettercount}{0}} |
| 287 |
{\dumpword\ifrunningcount\else\tcafterenv{\par(Wordcount=\thewordcount, |
| 288 |
Lettercount=\thelettercount)}\fi} |
| 289 |
|
| 290 |
\newcounter{pointcount} |
| 291 |
\newcommand\addtopoints[1]{(\textit{#1 point}% |
| 292 |
\ifnum#1=1\relax\else\textit{s}\fi)} |
| 293 |
\NewEnviron{pointtracker}[2][points]{% |
| 294 |
\par\bigskip\resettokcycle |
| 295 |
\setcounter{pointcount}{0}% |
| 296 |
\Macrodirective{\addcytoks{##1}\tctestifx{\addtopoints##1}{\sumpoints}{}}% |
| 297 |
\def\tmp{\tokencyclexpress{\large\bfseries#2: \thepointcount\ #1}}% |
| 298 |
\expandafter\tmp\BODY\endtokencyclexpress |
| 299 |
} |
| 300 |
\def\sumpoints{\tcpop\Q\addtocounter{pointcount}{\Q}\tcpushgroup{\Q}} |
| 301 |
|
| 302 |
\newcommand\scalerule[1]{% |
| 303 |
\tcpeek\zA |
| 304 |
\ifx[\zA\tcpopuntil]\opt\fi% IF PRESENT, POP OPTIONAL ARG |
| 305 |
\tcpop\ArgA% POP ARG1 |
| 306 |
\tcpop\ArgB% POP ARG2 |
| 307 |
\tcpushgroup[x]{#1\dimexpr\ArgB}% PUSH BACK IN REVERSE (LIFO) FASHION |
| 308 |
\tcpushgroup[x]{#1\dimexpr\ArgA} |
| 309 |
\ifx[\zA\tcpush[x]{\expandafter\scaleopt\opt{#1}}\fi |
| 310 |
} |
| 311 |
\def\scaleopt[#1]#2{[#2\dimexpr#1]} |
| 312 |
|
| 313 |
\newcommand\protectruleargs[1]{\ifx\rule#1% |
| 314 |
\tcpeek\zA |
| 315 |
\ifx[\zA\tcpopuntil]\opt |
| 316 |
\addcytoks[1]{\opt}\fi% IF OPTIONAL ARG PRESENT, POP AND SAVE TO \cytoks |
| 317 |
\tcpopliteral\ArgA% MUST POP ARG 1 WITH BRACES |
| 318 |
\addcytoks[1]{\ArgA}% |
| 319 |
\tcpopliteral\ArgB% MUST POP ARG 2 WITH BRACES |
| 320 |
\addcytoks[1]{\ArgB}% |
| 321 |
\fi |
| 322 |
} |
| 323 |
|
| 324 |
\newif\iffoundstring |
| 325 |
\newcommand\findstring[4]{% {CURRENT TOK}{MATCH STRING}{SUCCESS OP}{FAIL CMDS} |
| 326 |
\foundstringfalse% DEFAULT CONDITION |
| 327 |
\def\poptoks{}% INITIALIZATION. IF MATCH FAILS, MUST TO RESTORE POPPED TOKENS |
| 328 |
\expandafter\matchnextchar\ifx#1#2\relax\relax\fi\empty\relax |
| 329 |
\iffoundstring\addcytoks{#3{#2}}\else\tcpush{\poptoks}#4\fi |
| 330 |
} |
| 331 |
\def\matchnextchar#1#2\relax{\tctestifx{#1\empty}{}{% NO MATCH FROM 1ST TOKEN |
| 332 |
\tctestifx{\relax#1}{% |
| 333 |
\foundstringtrue% NOTHING LEFT TO COMPARE; MATCH FOUND |
| 334 |
}{% |
| 335 |
\tcpeek\Q% WITHOUT POPPING... |
| 336 |
\tctestifx{\Q\bgroup}{}{% PEEK FOR GROUPS OR... |
| 337 |
\tctestifx{\Q\tcsptoken}{}{% SPACES THAT INDICATE MATCH FAILURE |
| 338 |
\tcpopappto\poptoks% POP TOKEN AND APPEND TO \poptoks |
| 339 |
\expandafter\tctestifx\expandafter{\Q#1}{% |
| 340 |
\matchnextchar#2\relax% STILL MATCHING; KEEP GOING |
| 341 |
}{}% |
| 342 |
}% |
| 343 |
}% |
| 344 |
}% |
| 345 |
}} |
| 346 |
|
| 347 |
\newcommand\foo{$\bullet$} |
| 348 |
\newcommand\foocheck[1]{\tcpeek\z\ifx\foo\z\else |
| 349 |
\ifx\par\z\else\addcytoks{#1}\fi\fi} |
| 350 |
\tokcycleenvironment\dofoo |
| 351 |
{\addcytoks{##1}} |
| 352 |
{\processtoks{##1}} |
| 353 |
{\ifx\par##1\foocheck{##1}\else\addcytoks{##1}\fi} |
| 354 |
{\foocheck{##1}} |
| 355 |
|
| 356 |
\newcommand\coleqchek[1]{\tcpeek\z |
| 357 |
\ifx=\z\tcpop\z\addcytoks{\coloneqq}\else\addcytoks{#1}\fi} |
| 358 |
\tokcycleenvironment\subcoloneqq |
| 359 |
{\tctestifx{:##1}{\coleqchek{##1}}{\addcytoks{##1}}} |
| 360 |
{\processtoks{##1}} |
| 361 |
{\tctestifcon{\if\detokenize{:}\detokenize{##1}} |
| 362 |
{\coleqchek{##1}}{\addcytoks{##1}}} |
| 363 |
{\addcytoks{##1}} |
| 364 |
|
| 365 |
|
| 366 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 367 |
\section{Examples, examples, and more examples} |
| 368 |
|
| 369 |
Often, the best way to learn a new tool is to see examples of |
| 370 |
it being used. |
| 371 |
Here, a number of examples are gathered that span the spectrum |
| 372 |
of \TokCycle{} usage. |
| 373 |
|
| 374 |
\subsection{Application basics} |
| 375 |
|
| 376 |
\subsubsection{Using the CGMS directives} |
| 377 |
|
| 378 |
Apply different directives to Characters (under-dot), Groups |
| 379 |
(visible braces), Macros (boxed, detokenized), and |
| 380 |
Spaces (visible space). |
| 381 |
|
| 382 |
\begin{exampleC}[]{The \macname{underdot} macro} |
| 383 |
\newcommand\underdot[1]{\ooalign{#1\cr\hfil{\raisebox{-5pt}{.}}\hfil}} |
| 384 |
\end{exampleC} |
| 385 |
|
| 386 |
|
| 387 |
\begingroup |
| 388 |
\begin{exampleA}[lefthand width=7.8cm]{Apply different directives to |
| 389 |
characters{,} groups{,} macros{,} and spaces} |
| 390 |
\tokcycle{\addcytoks{\underdot{#1}}} |
| 391 |
{\addcytoks{\{}\processtoks{#1} |
| 392 |
\addcytoks{\}}} |
| 393 |
{\addcytoks{\fbox{\detokenize{#1}}}} |
| 394 |
{\addcytoks{\textvisiblespace}} |
| 395 |
{This \textit{is \textbf{a}} test.} |
| 396 |
\the\cytoks |
| 397 |
\end{exampleA} |
| 398 |
|
| 399 |
\endgroup |
| 400 |
|
| 401 |
\subsubsection{Escaping text} |
| 402 |
|
| 403 |
Text between two successive escape characters is bypassed |
| 404 |
by \TokCycle{} and instead echoed to the output token list |
| 405 |
(\macname{cytoks}). |
| 406 |
The default escape character is $|$. |
| 407 |
One can change the token-cycle escape character by way of the |
| 408 |
\macname{settcEscapechar} macro. |
| 409 |
As of version 1.5, the secape character need no longer be restricted to |
| 410 |
ASCII characters, but can now include an otherwise unutilized macro name. |
| 411 |
Below, the character codes are incremented by one; however, the |
| 412 |
arguments of \macname{rule} as well as other text are protected against that. |
| 413 |
|
| 414 |
\begingroup |
| 415 |
|
| 416 |
\begin{exampleC}[]{The unexpandable \macname{plusl} macro to increase |
| 417 |
the ASCII value of tokens by 1} |
| 418 |
\newcommand\plusl[1]{\char\numexpr`#1+1\relax} |
| 419 |
\end{exampleC} |
| 420 |
|
| 421 |
\begin{exampleA}[lefthand width=5cm]{Escaping text and macro parameters |
| 422 |
in the input stream} |
| 423 |
\tokcycle |
| 424 |
{\addcytoks{\plusl{#1}}} |
| 425 |
{\processtoks{#1}} |
| 426 |
{\addcytoks{#1}} |
| 427 |
{\addcytoks{#1}} |
| 428 |
{This \fbox{code is a test |
| 429 |
|(I can also escape text)|} |
| 430 |
of |\rule{1em}{.5em}| |
| 431 |
{\bfseries mine}.} |
| 432 |
\the\cytoks |
| 433 |
\par\settcEscapechar{\Esc} |
| 434 |
\tokcyclexpress{% |
| 435 |
Now we \Esc test a new escape token \Esc here.} |
| 436 |
\the\cytoks |
| 437 |
\end{exampleA} |
| 438 |
|
| 439 |
\endgroup |
| 440 |
|
| 441 |
\subsubsection{Unexpandable, unexpanded, and expanded Character |
| 442 |
directives} |
| 443 |
|
| 444 |
\begingroup |
| 445 |
This section concerns the issue of whether the characters of the input |
| 446 |
stream are transformed before or after being placed in the |
| 447 |
output token list (\macname{cytoks}). |
| 448 |
|
| 449 |
\medskip |
| 450 |
\begin{minipage}[t]{\textwidth} |
| 451 |
Transform characters (+1 ASCII) via unexpandable macro:\medskip |
| 452 |
|
| 453 |
\begin{exampleA}[lefthand width=8.3cm] |
| 454 |
{Unexpandable Character directive} |
| 455 |
\tokcycle |
| 456 |
{\addcytoks{\plusl{#1}}} |
| 457 |
{\processtoks{#1}} |
| 458 |
{\addcytoks{#1}} |
| 459 |
{\addcytoks{#1}}{% |
| 460 |
This \textit{code \textup{is} a test} of mine.} |
| 461 |
\the\cytoks |
| 462 |
\end{exampleA} |
| 463 |
|
| 464 |
\altdecytoks |
| 465 |
|
| 466 |
Note how the above detokenization shows the actual transformation |
| 467 |
instructions involving \macname{plusl}. |
| 468 |
This is done because \macname{plusl} is not expandable. |
| 469 |
\end{minipage} |
| 470 |
|
| 471 |
\par\noindent\hrulefill\medskip |
| 472 |
\endgroup |
| 473 |
|
| 474 |
\noindent\begin{minipage}[t]{\textwidth} |
| 475 |
Capitalize vowels (but don't expand the character directive) |
| 476 |
|
| 477 |
\begingroup |
| 478 |
|
| 479 |
\begin{exampleC}[]{The expandable\macname{vowelcap} macro} |
| 480 |
\newcommand\vowelcap[1]{% |
| 481 |
\ifx a#1A\else |
| 482 |
\ifx e#1E\else |
| 483 |
\ifx i#1I\else |
| 484 |
\ifx o#1O\else |
| 485 |
\ifx u#1U\else |
| 486 |
#1\fi\fi\fi\fi\fi |
| 487 |
} |
| 488 |
\end{exampleC} |
| 489 |
|
| 490 |
\begin{exampleA}[lefthand width=8.3cm] |
| 491 |
{Not expanded Character directive} |
| 492 |
\tokcycle |
| 493 |
{\addcytoks{\vowelcap{#1}}} |
| 494 |
{\processtoks{#1}} |
| 495 |
{\addcytoks{#1}} |
| 496 |
{\addcytoks{#1}}{% |
| 497 |
This \textit{code \textup{is} a test} of mine.} |
| 498 |
\the\cytoks |
| 499 |
\end{exampleA} |
| 500 |
|
| 501 |
\altdecytoks |
| 502 |
|
| 503 |
The above detokenization also shows the transformation macro, |
| 504 |
\macname{vowelcap}, as part of \macname{cytoks}. |
| 505 |
But because \macname{vowelcap} is expandable, we did not need to |
| 506 |
leave it at this stage, as the next example shows. |
| 507 |
|
| 508 |
\par\noindent\hrulefill\medskip |
| 509 |
\endgroup |
| 510 |
\end{minipage} |
| 511 |
|
| 512 |
\begin{minipage}[t]{\textwidth} |
| 513 |
\begingroup |
| 514 |
Capitalize vowels (expanding the character directive) |
| 515 |
|
| 516 |
\medskip |
| 517 |
By using the \texttt{[x]} option of \macname{addcytoks}, the |
| 518 |
\macname{vowelcap} is fully expanded prior to being placed in |
| 519 |
the \macname{cytoks} token list. |
| 520 |
|
| 521 |
|
| 522 |
\begin{exampleA}[lefthand width=8.3cm] |
| 523 |
{Expanded Character directive} |
| 524 |
\tokcycle |
| 525 |
{\addcytoks[x]{\vowelcap{#1}}} |
| 526 |
{\processtoks{#1}} |
| 527 |
{\addcytoks{#1}} |
| 528 |
{\addcytoks{#1}}{% |
| 529 |
This \textit{code \textup{is} a test} of mine.} |
| 530 |
\the\cytoks |
| 531 |
\end{exampleA} |
| 532 |
|
| 533 |
\altdecytoks |
| 534 |
|
| 535 |
Here, we see no vestiges of \macname{vowelcap} in \macname{cytoks}, |
| 536 |
but only the transformed result. |
| 537 |
\endgroup |
| 538 |
\end{minipage} |
| 539 |
|
| 540 |
\enlargethispage{12pt} |
| 541 |
\subsubsection{Unexpanded vs. pre-expanded input stream} |
| 542 |
|
| 543 |
\begingroup |
| 544 |
\Characterdirective{\addcytoks[x]{\vowelcap{#1}}} |
| 545 |
|
| 546 |
\noindent\begin{minipage}[t]{\textwidth} |
| 547 |
\begin{exampleA}[lefthand width=5.5cm]{Normal token cycle (input stream |
| 548 |
not pre-expanded)} |
| 549 |
\tokcycle |
| 550 |
{\addcytoks[x]{\vowelcap{#1}}} |
| 551 |
{\processtoks{#1}} |
| 552 |
{\addcytoks{#1}} |
| 553 |
{\addcytoks{#1}}% |
| 554 |
{This \fbox{code |
| 555 |
is a test \today} of |
| 556 |
{\bfseries mine}.} |
| 557 |
\the\cytoks |
| 558 |
\end{exampleA} |
| 559 |
|
| 560 |
\altdecytoks |
| 561 |
|
| 562 |
In the above case, vowel-capping is not applied to the date, because |
| 563 |
\macname{today} remains unexpanded in the \macname{cytoks} token list. |
| 564 |
|
| 565 |
\noindent\hrulefill\strut |
| 566 |
\end{minipage} |
| 567 |
|
| 568 |
Note that, when pre-expanding the input stream, one must |
| 569 |
\macname{noexpand} the macros that are \textit{not} |
| 570 |
to be pre-expanded. |
| 571 |
|
| 572 |
\begin{exampleA}[lefthand width=5.5cm]{Pre-\macname{expanded} token cycle |
| 573 |
input stream} |
| 574 |
\expandedtokcyclexpress |
| 575 |
{This \noexpand\fbox{code |
| 576 |
is a test \today} of |
| 577 |
{\noexpand\bfseries mine}.} |
| 578 |
\the\cytoks |
| 579 |
\end{exampleA} |
| 580 |
|
| 581 |
\altdecytoks |
| 582 |
|
| 583 |
Here, we see the date subjected to vowel-capping, because the \TokCycle{} |
| 584 |
input was expanded prior to the token cycle. |
| 585 |
Thus \macname{today} no longer appears \textit{explicitly} in the |
| 586 |
\macname{cytoks} token list. |
| 587 |
Its pre-expansion into character tokens is now subject to the |
| 588 |
\macname{vowelcap}. |
| 589 |
|
| 590 |
\endgroup |
| 591 |
|
| 592 |
\subsection{Grouping} |
| 593 |
|
| 594 |
Differentiating explicit groups, e.g., \{...\}, from |
| 595 |
implicit groups, e.g. \macname{bgroup}...\macname{egroup}, |
| 596 |
is done automatically by \TokCycle. |
| 597 |
The user has options on how \TokCycle{} should treat these tokens. |
| 598 |
The desired options are to be set prior to the \TokCycle{} invocation. |
| 599 |
|
| 600 |
|
| 601 |
\subsubsection{Treatment options for implicit groups} |
| 602 |
|
| 603 |
\begingroup |
| 604 |
|
| 605 |
The macro |
| 606 |
\macname{stripimplicitgroupingcase} can take three possible |
| 607 |
integer arguments: 0 (default) to automatically place unaltered |
| 608 |
implicit group tokens in the \macname{cytoks} output; 1 to strip |
| 609 |
implicit group tokens from the output; or $-$1 to instead pass the |
| 610 |
implicit group tokens to the Character directive (as implicit |
| 611 |
tokens) for separate processing (typically, when detokenization |
| 612 |
is desired). |
| 613 |
|
| 614 |
\begin{exampleA}[lefthand width=6cm]{Using |
| 615 |
\macname{stripimplicitgroupingcase} to affect treatment of |
| 616 |
implicit grouping tokens} |
| 617 |
\resettokcycle |
| 618 |
\Characterdirective{\addcytoks[x]{% |
| 619 |
\vowelcap{#1}}} |
| 620 |
\def\z{Announcement: |
| 621 |
{\bfseries\bgroup\itshape |
| 622 |
Today \egroup it is} \today, |
| 623 |
a Wednesday} |
| 624 |
\expandafter\tokencyclexpress\z |
| 625 |
\endtokencyclexpress\medskip |
| 626 |
|
| 627 |
\detokenize\expandafter{\the\cytoks} |
| 628 |
\bigskip |
| 629 |
|
| 630 |
\stripimplicitgroupingcase{1} |
| 631 |
\expandafter\tokencyclexpress\z |
| 632 |
\endtokencyclexpress\medskip |
| 633 |
|
| 634 |
\detokenize\expandafter{\the\cytoks} |
| 635 |
\end{exampleA} |
| 636 |
|
| 637 |
%\tokcyclexpress{Announcement: {\bfseries\bgroup\itshape |
| 638 |
% Today \egroup it is} \today, a Wednesday.}\the\cytoks |
| 639 |
|
| 640 |
%\detokenize\expandafter{\the\cytoks} |
| 641 |
|
| 642 |
\endgroup |
| 643 |
|
| 644 |
\subsubsection{Treatment options for explicit groups} |
| 645 |
|
| 646 |
\begingroup |
| 647 |
|
| 648 |
For explicit group tokens, e.g., \{ \}, there are only two |
| 649 |
options to be had. |
| 650 |
These are embodied in the if-condition \macname{ifstripgrouping} |
| 651 |
(default \macname{stripgroupingfalse}). |
| 652 |
Regardless of which condition is set, the tokens within the explicit |
| 653 |
group are still passed to the Group directive for processing. |
| 654 |
|
| 655 |
\medskip |
| 656 |
Permutations of the following code are used in the subsequent examples. |
| 657 |
Group stripping, brought about by \macname{stripgroupingtrue}, |
| 658 |
involves removing the grouping braces from around the group. |
| 659 |
The choice of \macname{processtoks} vs.\@ \macname{addcytoks} |
| 660 |
affects whether the tokens inside the group are recommitted |
| 661 |
to \TokCycle{} for processing, or are merely sent to the |
| 662 |
\macname{cytoks} token list in their original unprocessed form. |
| 663 |
|
| 664 |
\medskip |
| 665 |
Note that, in these examples, underdots and visible spaces |
| 666 |
will only appear on characters and spaces that have been directed |
| 667 |
to the Character and Space directives, respectively. |
| 668 |
Without \macname{processtoks}, that will not occur to tokens |
| 669 |
\textit{inside} of groups. |
| 670 |
|
| 671 |
\begin{exampleB}[listing only,lefthand width = \dimexpr\textwidth-19pt] |
| 672 |
{Code permutations on group stripping and inner-group token processing} |
| 673 |
\stripgroupingfalse OR \stripgroupingtrue |
| 674 |
\tokcycle{\addcytoks{\underdot{#1}}} |
| 675 |
{\processtoks{#1}} OR {\addcytoks{#1}}} |
| 676 |
{\addcytoks{#1}} |
| 677 |
{\addcytoks{\textvisiblespace}} |
| 678 |
{This \fbox{is a \fbox{token}} test.} |
| 679 |
\the\cytoks |
| 680 |
\end{exampleB} |
| 681 |
|
| 682 |
|
| 683 |
\begin{exampleB}[text only, colback=white]% |
| 684 |
{\macname{stripgroupingfalse \string\processtoks} } |
| 685 |
\stripgroupingfalse% RETAIN GROUPING (PACKAGE DEFAULT) |
| 686 |
\tokcycle{\addcytoks{\underdot{#1}}} |
| 687 |
{\processtoks{#1}}% GROUP TOKENS ARE PROCESSED |
| 688 |
{\addcytoks{#1}} |
| 689 |
{\addcytoks{\textvisiblespace}}{% |
| 690 |
This \fbox{is a \fbox{token}} test.} |
| 691 |
\the\cytoks |
| 692 |
\end{exampleB} |
| 693 |
|
| 694 |
\begin{exampleB}[text only, colback=white]% |
| 695 |
{\macname{stripgroupingfalse \string\addcytoks} } |
| 696 |
\tokcycle{\addcytoks{\underdot{#1}}} |
| 697 |
{\addcytoks{#1}}% GROUP TOKENS OUTPUT DIRECTLY |
| 698 |
{\addcytoks{#1}} |
| 699 |
{\addcytoks{\textvisiblespace}}{% |
| 700 |
This \fbox{is a \fbox{token}} test.} |
| 701 |
\the\cytoks |
| 702 |
\end{exampleB} |
| 703 |
|
| 704 |
\begin{exampleB}[text only, colback=white]% |
| 705 |
{\macname{stripgroupingtrue \string\processtoks} } |
| 706 |
\stripgroupingtrue% DANGEROUS AS IT REMOVES GROUPING FROM ARGUMENTS |
| 707 |
\tokcycle{\addcytoks{{\underdot{#1}}}}% EXTRA GROUPING TO COMPENSATE |
| 708 |
{\processtoks{#1}}% GROUP TOKENS PROCESSED |
| 709 |
{\addcytoks{#1}} |
| 710 |
{\addcytoks{\textvisiblespace}}{% |
| 711 |
This \fbox{is a \fbox{token}} test.} |
| 712 |
\the\cytoks |
| 713 |
\end{exampleB} |
| 714 |
|
| 715 |
\begin{exampleB}[text only, colback=white]% |
| 716 |
{\macname{stripgroupingtrue \string\addcytoks} } |
| 717 |
\stripgroupingtrue% DANGEROUS AS IT REMOVES GROUPING FROM ARGUMENTS |
| 718 |
\tokcycle{\addcytoks{\underdot{#1}}} |
| 719 |
{\addcytoks{#1}}% GROUP TOKENS OUTPUT DIRECTLY |
| 720 |
{\addcytoks{#1}} |
| 721 |
{\addcytoks{\textvisiblespace}}{% |
| 722 |
This \fbox{is a \fbox{token}} test.} |
| 723 |
\the\cytoks |
| 724 |
\end{exampleB} |
| 725 |
|
| 726 |
Note that the content of groups can be altogether eliminated if |
| 727 |
\textit{neither} \macname{processtoks\{\#1\}} nor |
| 728 |
\macname{addcytoks\{\#1\}} are used in the Group directive. |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
\endgroup |
| 733 |
|
| 734 |
\subsubsection{Group nesting} |
| 735 |
|
| 736 |
\begingroup |
| 737 |
|
| 738 |
\begin{exampleC}[]{The \macname{reducecolor} and \macname{restorecolor} |
| 739 |
macros} |
| 740 |
\newcounter{colorindex} |
| 741 |
\newcommand\restorecolor{\setcounter{colorindex}{100}} |
| 742 |
\newcommand\reducecolor[1]{% |
| 743 |
\color{red!\thecolorindex!cyan}% |
| 744 |
\addtocounter{colorindex}{-#1}% |
| 745 |
\ifnum\thecolorindex<1\relax\setcounter{colorindex}{1}\fi} |
| 746 |
\end{exampleC} |
| 747 |
|
| 748 |
\begin{exampleA}[lefthand width=6cm]{Group nesting is no impediment to |
| 749 |
tokcycle} |
| 750 |
\restorecolor |
| 751 |
\tokcycle |
| 752 |
{\addcytoks{(#1)}} |
| 753 |
{\addcytoks{\reducecolor{11}}% |
| 754 |
\addcytoks{[}\processtoks{#1}% |
| 755 |
\addcytoks{]}} |
| 756 |
{\addcytoks{#1}} |
| 757 |
{}{% |
| 758 |
{1{{3{{5{{7{{9{1{}0}}8}}6}}4}}2}}} |
| 759 |
\the\cytoks |
| 760 |
\end{exampleA} |
| 761 |
Note, above, that \TokCycle{} does not rely on an argument-based nesting |
| 762 |
algorithm that would otherwise be limited to 9 embedded groups. |
| 763 |
\endgroup |
| 764 |
|
| 765 |
\subsubsection{The use of \macname{groupedcytoks} for more sophisticated |
| 766 |
group processing} |
| 767 |
|
| 768 |
\begingroup |
| 769 |
|
| 770 |
\begin{sloppypar} |
| 771 |
By default, \TokCycle{} processes groups such that whatever additions to the |
| 772 |
\macname{cytoks} token list you accomplish in the Group directive will be |
| 773 |
encased in an explicit grouped scope. |
| 774 |
This is the token-cycle behavior with the default \macname{stripgroupingfalse} |
| 775 |
declaration. |
| 776 |
However, for more sophisticated tasks associated with the processing of |
| 777 |
group content, one may override the default (by way of |
| 778 |
\macname{stripgroupingtrue}) and manually dictate the groupings associated |
| 779 |
with the Group directive, with the use of \macname{groupedcytoks\{...\}}. |
| 780 |
\end{sloppypar} |
| 781 |
|
| 782 |
\medskip In the following example, tasks A and D are performed \textit{outside} |
| 783 |
of the explicit grouping otherwise introduced with \macname{groupedcytoks}, |
| 784 |
whereas tasks B and C fall inside of that explicit group. |
| 785 |
The fact that, in the output, \texttt{d} is not italic confirms the |
| 786 |
fact that task D occurs after the conclusion of the explicit \macname{cytoks} |
| 787 |
group that encompasses the italic `bxc'. |
| 788 |
The fact that none of \texttt{a}, \texttt{b}, \texttt{c}, nor \texttt{d} are |
| 789 |
encased in an \macname{fbox} indicates that they were not processed through |
| 790 |
the Character directive, but arose from direct additions to \macname{cytoks}, |
| 791 |
by way of their tasks. |
| 792 |
|
| 793 |
\begin{exampleA}[lefthand width=10cm]{Use of the \macname{groupedcytoks} |
| 794 |
macro} |
| 795 |
\stripgroupingtrue |
| 796 |
\Characterdirective{\addcytoks{\fbox{#1}}} |
| 797 |
\Groupdirective{\taskA% |
| 798 |
\groupedcytoks{\taskB\processtoks{#1}\taskC}% |
| 799 |
\taskD} |
| 800 |
\def\taskA{\addcytoks{\bgroup\LARGE a}} |
| 801 |
\def\taskB{\addcytoks{\itshape b}} |
| 802 |
\def\taskC{\addcytoks{c}} |
| 803 |
\def\taskD{\addcytoks{d\egroup}} |
| 804 |
|
| 805 |
\tokcyclexpress{x\textbf{x}x} |
| 806 |
\the\cytoks |
| 807 |
\end{exampleA} |
| 808 |
|
| 809 |
The final disposition of the \macname{cytoks} token list shows the effects of |
| 810 |
\macname{stripgroupingtrue} and \macname{groupedcytoks} more explicitly: |
| 811 |
|
| 812 |
\altdecytoks |
| 813 |
|
| 814 |
Without \macname{stripgroupingtrue}, all tasks, A,B,C, and D, would have |
| 815 |
been contained within the explicit \texttt{\{...\}} group. |
| 816 |
|
| 817 |
\endgroup |
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
\subsection{Direct use of \TokCycle} |
| 822 |
|
| 823 |
The \TokCycle{} macros and pseudo-environments |
| 824 |
(in regular or \texttt{xpress} form) may be invoked |
| 825 |
directly from the document, without |
| 826 |
being first encapsulated within a separate macro or environment. |
| 827 |
|
| 828 |
\subsubsection{Modifying counters as part of the Character |
| 829 |
directive} |
| 830 |
|
| 831 |
\begingroup |
| 832 |
|
| 833 |
\begin{exampleA}[lefthand width=8.5cm]{Using a period token (.) to |
| 834 |
reset a changing color every sentence} |
| 835 |
\restorecolor |
| 836 |
\tokencycle |
| 837 |
{\addcytoks{\bgroup\reducecolor{3}#1\egroup}% |
| 838 |
\ifx.#1\addcytoks{\restorecolor}\fi} |
| 839 |
{\processtoks{#1}} |
| 840 |
{\addcytoks{#1}} |
| 841 |
{\addcytoks{#1}}% |
| 842 |
This right \textit{here is a sentence in italic}. |
| 843 |
And \textbf{here we have another sentence in bold}. |
| 844 |
|
| 845 |
{\scshape Now in a new paragraph, the sentence |
| 846 |
is long.} Now, it is short. |
| 847 |
\endtokencycle |
| 848 |
\end{exampleA} |
| 849 |
|
| 850 |
\endgroup |
| 851 |
|
| 852 |
\subsection{Macro encapsulation of \TokCycle} |
| 853 |
|
| 854 |
\subsubsection{Spacing out text} |
| 855 |
|
| 856 |
\begingroup |
| 857 |
|
| 858 |
\begin{exampleC}[]{The \macname{spaceouttext} macro} |
| 859 |
\newcommand\spaceouttext[2]{% |
| 860 |
\tokcycle |
| 861 |
{\addcytoks{##1\nobreak\hspace{#1}}}% |
| 862 |
{\processtoks{##1}} |
| 863 |
{\addcytoks{##1}}% |
| 864 |
{\addcytoks{##1\hspace{#1}}} |
| 865 |
{#2}% |
| 866 |
\the\cytoks\unskip} |
| 867 |
\end{exampleC} |
| 868 |
|
| 869 |
\begin{exampleA}[lefthand width=6cm]{\macname{spaceouttext} demo} |
| 870 |
\spaceouttext{3pt plus 3pt}{This |
| 871 |
\textit{text \textbf{is} |
| 872 |
very} spaced out}. Back |
| 873 |
to regular text. |
| 874 |
|
| 875 |
\spaceouttext{1.5pt}{This |
| 876 |
\textit{text \textbf{is} |
| 877 |
somewhat} spaced out}. |
| 878 |
Back to regular text. |
| 879 |
\end{exampleA} |
| 880 |
|
| 881 |
Note that embedded macros such as \macname{textit} and \macname{textbf} |
| 882 |
do not adversely affect the capability of the \macname{spaceouttext} |
| 883 |
macro. |
| 884 |
\endgroup |
| 885 |
|
| 886 |
|
| 887 |
\subsubsection{Alternate presentation of detokenized content} |
| 888 |
|
| 889 |
\begingroup |
| 890 |
This macro attempts to give a more natural presentation of |
| 891 |
\macname{detokenize}'d material. |
| 892 |
It is \textbf{not} to be confused as a replacement for |
| 893 |
\macname{detokenize}. |
| 894 |
In certain applications, it may offer a more pleasingly formatted |
| 895 |
typesetting of detokenized material. |
| 896 |
|
| 897 |
\medskip It is an unusual application of \TokCycle{} in that it does not |
| 898 |
actually use the \macname{cytoks} token list to collect |
| 899 |
its output. |
| 900 |
This is only possible because all macros in the input stream |
| 901 |
are detokenized, rather than executed. |
| 902 |
|
| 903 |
\medskip |
| 904 |
|
| 905 |
\begin{exampleC}[]{The \macname{altdetokenize }\ macro} |
| 906 |
\newif\ifmacro |
| 907 |
\newcommand\altdetokenize[1]{\begingroup\stripgroupingtrue\macrofalse |
| 908 |
\stripimplicitgroupingcase{-1}% |
| 909 |
\tokcycle |
| 910 |
{\ifmacro\def\tmp{##1}\ifcat\tmp A\else\unskip\allowbreak\fi\macrofalse\fi |
| 911 |
\detokenize{##1}\ifx##1\bgroup\unskip\fi\ifx##1\egroup\unskip\fi} |
| 912 |
{\ifmacro\unskip\macrofalse\fi\{\processtoks{##1}\ifmacro\unskip\fi\}\allowbreak} |
| 913 |
{\tctestifx{\\##1}{\\}{\ifmacro\unskip\allowbreak\fi |
| 914 |
\allowbreak\detokenize{##1}\macrotrue}} |
| 915 |
{ \hspace{0pt plus 3em minus .3ex}} |
| 916 |
{#1}% |
| 917 |
\unskip |
| 918 |
\endgroup} |
| 919 |
\end{exampleC} |
| 920 |
|
| 921 |
\let\mac\relax |
| 922 |
\begin{exampleA}[lefthand width=6.3cm] |
| 923 |
{\macname{altdetokenize} demo} |
| 924 |
\string\altdetokenize: \\ |
| 925 |
\texttt{\altdetokenize{a\mac a \mac2 {\mac}\mac{a\mac\mac}\mac}}! |
| 926 |
|
| 927 |
versus \string\detokenize: \\ |
| 928 |
\texttt{\detokenize{a\mac a \mac2 {\mac}\mac{a\mac\mac}\mac}}! |
| 929 |
\end{exampleA} |
| 930 |
\endgroup |
| 931 |
|
| 932 |
|
| 933 |
\subsubsection{Capitalize all words, including compound and |
| 934 |
parenthetical words\label{s:cap}} |
| 935 |
|
| 936 |
\begingroup |
| 937 |
|
| 938 |
\begin{exampleC}[]{The \macname{Titlecase} and \texttt{\string\nextcap} macros} |
| 939 |
\newcommand\TitleCase[1]{% |
| 940 |
\def\capnext{T}% <- INITIAL ENTRY |
| 941 |
\tokcycle |
| 942 |
{\nextcap{##1}} |
| 943 |
{\def\capnext{T}\processtoks{##1}}% <- GROUP ENTRY |
| 944 |
{\addcytoks{##1}} |
| 945 |
{\addcytoks{##1}\def\capnext{T}}% <-CAT-10 SPACE |
| 946 |
{#1}% |
| 947 |
\the\cytoks |
| 948 |
} |
| 949 |
\newcommand\nextcap[1]{% |
| 950 |
\edef\tmp{#1}% |
| 951 |
\tctestifx{-#1}{\def\capnext{T}}{}% <- TEST FOR HYPHEN |
| 952 |
\tctestifcon{\if T\capnext}% |
| 953 |
{\tctestifcon{\ifcat\tmp A}% <- TEST FOR NEXT CAT-11 |
| 954 |
{\addcytoks{\uppercase{#1}}\def\capnext{F}}% |
| 955 |
{\addcytoks{#1}}}% |
| 956 |
{\addcytoks{#1}}% |
| 957 |
} |
| 958 |
\end{exampleC} |
| 959 |
|
| 960 |
\begin{exampleA}[]{A demo of \macname{Titlecase} showing raw (escaped) |
| 961 |
input and processed output} |
| 962 |
\TitleCase{% |
| 963 |
|here, {\bfseries\today{}, is [my]} |
| 964 |
really-big-test |
| 965 |
(\textit{capitalizing} words).| |
| 966 |
|
| 967 |
here, {\bfseries\today{}, is [my]} |
| 968 |
really-big-test |
| 969 |
(\textit{capitalizing} words).} |
| 970 |
\end{exampleA} |
| 971 |
|
| 972 |
As it is currently set up, a future-capitalization of the next catcode-11 token will be triggered when any of the following is encountered in the input stream: |
| 973 |
\begin{itemize} |
| 974 |
\item The initial entry into the routine |
| 975 |
|
| 976 |
\item a catcode-10 space token |
| 977 |
|
| 978 |
\item a hyphen \texttt{-} |
| 979 |
|
| 980 |
\item upon entry into a group \texttt{\{...\}} |
| 981 |
\end{itemize} |
| 982 |
|
| 983 |
Obviously, additional tests could be added. |
| 984 |
|
| 985 |
\endgroup |
| 986 |
|
| 987 |
\subsubsection{Scaling rule dimensions} |
| 988 |
\begingroup |
| 989 |
|
| 990 |
\ddbend This example only applies if one can guarantee that |
| 991 |
the input stream will contain only text and rules... |
| 992 |
|
| 993 |
\begin{exampleC}[]{The \macname{growdim} macro} |
| 994 |
\newcommand\growdim[2]{% |
| 995 |
\tokcycle{\addcytoks{##1}} |
| 996 |
{\addcytoks{#1\dimexpr##1}} |
| 997 |
{\addcytoks{##1}} |
| 998 |
{\addcytoks{##1}}{% |
| 999 |
#2}% |
| 1000 |
\the\cytoks} |
| 1001 |
\end{exampleC} |
| 1002 |
|
| 1003 |
\begin{exampleA}[]{Using \TokCycle{} to change \macname{rule} dimensions} |
| 1004 |
\growdim{2}{This rule is exactly 4pt: |
| 1005 |
\rule|{4pt}{4pt}| , whereas this |
| 1006 |
rule is 2x bigger than 4pt: |
| 1007 |
\rule{4pt}{4pt} .}\par |
| 1008 |
\growdim{4}{This rule is exactly 5pt: |
| 1009 |
\rule|{5pt}{5pt}| , whereas this |
| 1010 |
rule is 4x bigger than 5pt: |
| 1011 |
\rule{5pt}{5pt} .} |
| 1012 |
\end{exampleA} |
| 1013 |
|
| 1014 |
\endgroup |
| 1015 |
|
| 1016 |
\subsubsection{String search, including non-regex material\label{s:searchA}} |
| 1017 |
|
| 1018 |
\begingroup |
| 1019 |
|
| 1020 |
\begin{exampleC}[]{The \macname{findinstring} macro for string searches} |
| 1021 |
\newcommand\findinstring[2]{\begingroup% |
| 1022 |
\stripgroupingtrue |
| 1023 |
\setcounter{runcount}{0}% |
| 1024 |
\tokcycle |
| 1025 |
{\nextctltok{##1}} |
| 1026 |
{\nextctltok{\opengroup}\processtoks{##1}\nextctltok{\closegroup}} |
| 1027 |
{\nextctltok{##1}} |
| 1028 |
{\nextctltok{\tcspace}} |
| 1029 |
{#1}% |
| 1030 |
\edef\numlet{\theruncount}% |
| 1031 |
\expandafter\def\expandafter\searchword\expandafter{\the\cytoks}% |
| 1032 |
% |
| 1033 |
\aftertokcycle{\matchfound}% |
| 1034 |
\setcounter{runcount}{0}% |
| 1035 |
\def\matchfound{F}% |
| 1036 |
\tokcycle |
| 1037 |
{\nextcmptok{##1}} |
| 1038 |
{\nextcmptok{\opengroup}\processtoks{##1}\nextcmptok{\closegroup}} |
| 1039 |
{\nextcmptok{##1}} |
| 1040 |
{\nextcmptok{\tcspace}} |
| 1041 |
{#2}% |
| 1042 |
\endgroup} |
| 1043 |
\newcounter{runcount} |
| 1044 |
\makeatletter |
| 1045 |
\newcommand\rotcytoks[1]{\cytoks\expandafter\expandafter\expandafter{% |
| 1046 |
\expandafter\tc@gobble\the\cytoks#1}} |
| 1047 |
\makeatother |
| 1048 |
\newcommand\testmatch[1]{\ifx#1\searchword\gdef\matchfound{T}\fi}% |
| 1049 |
\newcommand\rotoradd[2]{\stepcounter{runcount}% |
| 1050 |
\ifnum\theruncount>\numlet\relax#1\else#2\fi |
| 1051 |
\expandafter\def\expandafter\tmp\expandafter{\the\cytoks}} |
| 1052 |
\newcommand\nextcmptok[1]{\rotoradd{\rotcytoks{#1}}{\addcytoks{#1}}\testmatch{\tmp}} |
| 1053 |
\newcommand\nextctltok[1]{\stepcounter{runcount}\addcytoks{#1}} |
| 1054 |
\end{exampleC} |
| 1055 |
|
| 1056 |
\begin{exampleA}[lefthand width=11.5cm]{Demo of the |
| 1057 |
\macname{findinstring} macro} |
| 1058 |
1. \findinstring{this}{A test of the times} |
| 1059 |
\findinstring{the} {A test of the times}\par |
| 1060 |
2. \findinstring{This is}{Here, This is a test} |
| 1061 |
\findinstring{Thisis} {Here, This is a test}\par |
| 1062 |
3. \findinstring{the} {This is the\bfseries{} test} |
| 1063 |
\findinstring{he\bfseries}{This is the\bfseries{} test}\par |
| 1064 |
4. \findinstring{a{bc}} {gf{vf{a{b c}g}gh}hn} |
| 1065 |
\findinstring{a{b c}}{gf{vf{a{b c}g}gh}hn}\par |
| 1066 |
5. \findinstring{a\notmymac{b c}}{gf{vf{a\mymac{b c}g}gh}hn} |
| 1067 |
\findinstring{a\mymac{b c}} {gf{vf{a\mymac{b c}g}gh}hn}\par |
| 1068 |
6. \findinstring{\textit{Italic}}{this is an \textit{italic} test} |
| 1069 |
\findinstring{\textit{italic}}{this is an \textit{italic} test} |
| 1070 |
\end{exampleA} |
| 1071 |
|
| 1072 |
\endgroup |
| 1073 |
|
| 1074 |
\subsubsection{Counting the significant digits in a number} |
| 1075 |
|
| 1076 |
\begingroup |
| 1077 |
|
| 1078 |
The problem of counting the significant digits in a number is an interesting |
| 1079 |
one, in that leading zeros are not significant, trailing zeros are sometimes |
| 1080 |
significant, but zeros in the middle of the number are significant. |
| 1081 |
In a problem like this, the \macname{cytoks} token list is not used to |
| 1082 |
carry a transformation of the input tokens, but only to carry a record |
| 1083 |
of errant tokens from the input (as seen from the helper macro |
| 1084 |
\macname{throwerr}). |
| 1085 |
If \macname{the\textbackslash cytoks} is anything other than empty when the |
| 1086 |
macro completes, the macro will set the significant-digit counter |
| 1087 |
\texttt{sigd} to a value of -1 to signify an error condition. |
| 1088 |
|
| 1089 |
\medskip |
| 1090 |
The macro expects its input to comprise digits in the 0--9 range, an [optional] |
| 1091 |
single decimal point, and ignorable spaces. Any other token will throw |
| 1092 |
an error. |
| 1093 |
|
| 1094 |
\medskip |
| 1095 |
The main work of the \macname{sigdigits} macro is to use the counters |
| 1096 |
\texttt{sigd} to count significant digits and \texttt{trailingz} to |
| 1097 |
tabulate the number of trailing-zeros with which to decrement |
| 1098 |
\texttt{sigd}. |
| 1099 |
|
| 1100 |
\begin{exampleC}[]{The \macname{sigdigits} macro for counting significant digits} |
| 1101 |
\newcounter{sigd} |
| 1102 |
\newcounter{trailingz} |
| 1103 |
\newif\iffounddot |
| 1104 |
\newif\ifzerosig |
| 1105 |
\newcommand\sigdigits[1]{% |
| 1106 |
\setcounter{sigd}{0}% SIGNIFICANT DIGIT COUNT |
| 1107 |
\setcounter{trailingz}{0}% TRAILING ZERO COUNT |
| 1108 |
\founddotfalse% MADE T WHEN DECIMAL HAS BEEN 1ST LOCATED |
| 1109 |
\zerosigfalse% MADE T WHEN ZERO BECOMES SIGNIFICANT |
| 1110 |
\tokcycle% CYCLE THROOUGH EACH TOKEN |
| 1111 |
{\tctestifx{.##1}% |
| 1112 |
{\iffounddot\throwerr\fi\founddottrue}% IF . |
| 1113 |
{\tctestifnum{`##1>`/}% |
| 1114 |
{\tctestifnum{`##1<`:}% |
| 1115 |
{\tctestifnum{##1>0}% |
| 1116 |
{\stepcounter{sigd}\zerosigtrue\setcounter{trailingz}{0}}% IF 1-9 |
| 1117 |
{\ifzerosig\stepcounter{sigd}\fi\stepcounter{trailingz}}% IF 0 |
| 1118 |
}% |
| 1119 |
{\throwerr}% IF ASCII > `9 |
| 1120 |
}% |
| 1121 |
{\throwerr}% IF ASCII < `0 |
| 1122 |
}% |
| 1123 |
}% APPLY ABOVE LOGIC FOR CHAR TOKENS |
| 1124 |
{\throwerr}% IF BRACES |
| 1125 |
{\throwerr}% IF CONTROL SEQUENCE |
| 1126 |
{}% IGNORE SPACES |
| 1127 |
{#1}% THE ARGUMENT |
| 1128 |
\iffounddot\else\addtocounter{sigd}{-\thetrailingz}\fi% TRAILING ZEROS |
| 1129 |
\expandafter\ifx\expandafter\relax\detokenize\expandafter{\the\cytoks}% |
| 1130 |
\relax\else\setcounter{sigd}{-1}\fi% CHECK FOR INVALID INPUT |
| 1131 |
\thesigd |
| 1132 |
} |
| 1133 |
\newcommand\throwerr{\addcytoks{##1}}% ADD INVALID TOKS TO \cytoks |
| 1134 |
\end{exampleC} |
| 1135 |
|
| 1136 |
\begin{exampleA}[lefthand width=8.0cm]{Example use of the \macname{sigdigits} |
| 1137 |
macro} |
| 1138 |
1) \sigdigits{12.3}\\ |
| 1139 |
2) \sigdigits{0.123}\\ |
| 1140 |
3) \sigdigits{1.2.3}\\ |
| 1141 |
4) \sigdigits{1.3\today}\\ |
| 1142 |
5) \sigdigits{321.345 678}\\ |
| 1143 |
6) \sigdigits{000321.305}\\ |
| 1144 |
7) \sigdigits{.000 300 345}\\ |
| 1145 |
8) \sigdigits{0003x1.345}\\ |
| 1146 |
9) \sigdigits{123000}\\ |
| 1147 |
A) \sigdigits{123.000}\\ |
| 1148 |
B) \sigdigits{0003;345} |
| 1149 |
\end{exampleA} |
| 1150 |
|
| 1151 |
\endgroup |
| 1152 |
|
| 1153 |
|
| 1154 |
\subsection{Creating your own \TokCycle-based environments} |
| 1155 |
|
| 1156 |
The \macname{tokcycleenvironment} macro allows users to define |
| 1157 |
their own \TokCycle{} environments. |
| 1158 |
The more recently introduced \macname{xtokcycleenvironment} macro extends |
| 1159 |
that capability to allow initialization and postprocessing when defining |
| 1160 |
a named \TokCycle{} environment. |
| 1161 |
Here are some examples of both types. |
| 1162 |
|
| 1163 |
\subsubsection{``Removing'' spaces, but still breakable/hyphenatable} |
| 1164 |
|
| 1165 |
\begingroup |
| 1166 |
|
| 1167 |
\begin{exampleC}[]{The \macname{spaceBgone} environment} |
| 1168 |
\tokcycleenvironment\spaceBgone |
| 1169 |
{\addcytoks{##1}} |
| 1170 |
{\processtoks{##1}} |
| 1171 |
{\addcytoks{##1}} |
| 1172 |
{\addcytoks{\hspace{.2pt plus .2pt minus .8pt}}}% |
| 1173 |
\end{exampleC} |
| 1174 |
|
| 1175 |
\begin{exampleA}[lefthand width=6.0cm]{Example of using the \macname{spaceBgone} |
| 1176 |
environment} |
| 1177 |
\spaceBgone |
| 1178 |
Here we have a \textit{test} of |
| 1179 |
whether the spaces are removed. |
| 1180 |
We are choosing to use the |
| 1181 |
tokencycle environment. |
| 1182 |
|
| 1183 |
We are also testing the use of |
| 1184 |
paragraph breaks in the |
| 1185 |
environment. |
| 1186 |
\endspaceBgone |
| 1187 |
\end{exampleA} |
| 1188 |
|
| 1189 |
\endgroup |
| 1190 |
|
| 1191 |
\subsubsection{Remapping text} |
| 1192 |
|
| 1193 |
\begin{minipage}[t]{\textwidth} |
| 1194 |
\begingroup |
| 1195 |
|
| 1196 |
\begin{exampleC}[]{The \macname{remaptext} environment with |
| 1197 |
supporting macros} |
| 1198 |
\tokcycleenvironment\remaptext |
| 1199 |
{\addcytoks[x]{\tcremap{##1}}} |
| 1200 |
{\processtoks{##1}} |
| 1201 |
{\addcytoks{##1}} |
| 1202 |
{\addcytoks{##1}} |
| 1203 |
\newcommand*\tcmapto[2]{\expandafter\def\csname tcmapto#1\endcsname{#2}} |
| 1204 |
\newcommand*\tcremap[1]{\ifcsname tcmapto#1\endcsname |
| 1205 |
\csname tcmapto#1\expandafter\endcsname\else\expandafter#1\fi} |
| 1206 |
\tcmapto am \tcmapto bf \tcmapto cz \tcmapto de \tcmapto ey |
| 1207 |
\tcmapto fl \tcmapto gx \tcmapto hb \tcmapto ic \tcmapto jn |
| 1208 |
\tcmapto ki \tcmapto lr \tcmapto mh \tcmapto nt \tcmapto ok |
| 1209 |
\tcmapto ps \tcmapto qa \tcmapto ro \tcmapto sq \tcmapto tw |
| 1210 |
\tcmapto uj \tcmapto vp \tcmapto wd \tcmapto xg \tcmapto yu |
| 1211 |
\tcmapto zv |
| 1212 |
\end{exampleC} |
| 1213 |
|
| 1214 |
\begin{exampleA}[lefthand width=8.5cm]{Demo of \macname{remaptext}} |
| 1215 |
\remaptext |
| 1216 |
What can't we \textit{accomplish} if we try? |
| 1217 |
|
| 1218 |
Let us be of good spirit and put our minds to it! |
| 1219 |
\endremaptext |
| 1220 |
\end{exampleA} |
| 1221 |
|
| 1222 |
Because \macname{tcremap} is expandable, the original text is totally absent |
| 1223 |
from the processed output: |
| 1224 |
|
| 1225 |
\altdecytoks |
| 1226 |
|
| 1227 |
\endgroup |
| 1228 |
\end{minipage} |
| 1229 |
|
| 1230 |
\bigskip |
| 1231 |
\begingroup |
| 1232 |
|
| 1233 |
This approach can be used, for example, to temporarily remap Latin into Greek in |
| 1234 |
math mode. |
| 1235 |
In the example given below, only several of the Latin letters are remapped |
| 1236 |
for demonstration purposes, but others can be added. |
| 1237 |
Also, remember that one can use the \verb+|...|+ delimiters to escape back to |
| 1238 |
Latin within the environment. |
| 1239 |
|
| 1240 |
\begin{exampleC}[]{The \macname{greek} environment with |
| 1241 |
supporting macros} |
| 1242 |
\tokcycleenvironment\greek% NEW TOKCYCLE ENVIRONMENT: |
| 1243 |
{\addcytoks[4]{\tcremap{##1}}}% HOW TO TREAT CHARACTERS |
| 1244 |
{\processtoks{##1}}% HOW TO TREAT GROUP CONTENTS |
| 1245 |
{\addcytoks{##1}}% HOW TO TREAT MACROS |
| 1246 |
{\addcytoks{##1}}% HOW TO TREAT SPACES |
| 1247 |
\newcommand*\tcmapto[2]{\expandafter\def\csname tcmapto#1\endcsname{#2}} |
| 1248 |
\newcommand*\tcremap[1]{\ifcsname tcmapto#1\endcsname |
| 1249 |
\csname tcmapto#1\expandafter\endcsname\else\expandafter#1\fi} |
| 1250 |
\tcmapto a\alpha \tcmapto b\beta \tcmapto e\epsilon |
| 1251 |
\tcmapto g\gamma \tcmapto p\pi |
| 1252 |
\end{exampleC} |
| 1253 |
|
| 1254 |
\tcmapto a\alpha \tcmapto b\beta \tcmapto e\epsilon |
| 1255 |
\tcmapto g\gamma \tcmapto p\pi |
| 1256 |
|
| 1257 |
\begin{exampleA}[lefthand width=8.5cm]{Demo of \macname{greek}} |
| 1258 |
\[ |
| 1259 |
y = |
| 1260 |
\greek a^2b\frac{2e}{2 + p^|p| + g}\endgreek |
| 1261 |
+ x |
| 1262 |
\] |
| 1263 |
\end{exampleA} |
| 1264 |
|
| 1265 |
\endgroup |
| 1266 |
|
| 1267 |
\subsubsection{Creating an ``extended'' \TokCycle{} environment} |
| 1268 |
|
| 1269 |
\begingroup |
| 1270 |
The \macname{xtokcycleenvironment} macro extends the capability of |
| 1271 |
\macname{tokcycleenvironment} by adding two arguments that correspond, |
| 1272 |
respectively, to initialization code and ``close-out'' code that is run |
| 1273 |
after the token cycle is complete (but before it is typeset). |
| 1274 |
Let's say I wanted an environment that capitalizes instances of the letter |
| 1275 |
`\texttt{t}', but I want it to tell me how many |
| 1276 |
letter \texttt{t}'s are affected before typesetting the result. |
| 1277 |
Furthermore, I want it always to set the result in red and add two |
| 1278 |
exclamation points to the end of the environment output. |
| 1279 |
|
| 1280 |
\begin{exampleC}[]{The \macname{capTive} extended-environment definition} |
| 1281 |
\newcounter{Tcount} |
| 1282 |
\xtokcycleenvironment\capTive% ENVIRONMENT NAME |
| 1283 |
{\tctestifx{t##1}{\stepcounter{Tcount}\addcytoks{T}}{\addcytoks{##1}}}% CHAR. DIRECTIVE |
| 1284 |
{\processtoks{##1}}% GROUP DIRECTIVE |
| 1285 |
{\addcytoks{##1}}% MACRO DIRECTIVE |
| 1286 |
{\addcytoks{##1}}% SPACE DIRECTIVE |
| 1287 |
{\setcounter{Tcount}{0}\color{red}\tcafterenv{\ Here}}% INITIALIZATION CODE |
| 1288 |
{\theTcount{} t's capitalized: \addcytoks{!!}}% CLOSE-OUT CODE |
| 1289 |
\end{exampleC} |
| 1290 |
|
| 1291 |
\begin{exampleA}[lefthand width=7.9cm]{Demo of \macname{capTive} |
| 1292 |
environment} |
| 1293 |
Before \capTive the cytoks \footnotesize token list\endcapTive After |
| 1294 |
\end{exampleA} |
| 1295 |
|
| 1296 |
In the initialization code, the \texttt{Tcount} counter is reset and the color |
| 1297 |
of what follows is made red. |
| 1298 |
It also makes use of the specialized \macname{tcafterenv} macro that can |
| 1299 |
\textbf{only be used in \macname{xtokcycle\-environment} definitions}. |
| 1300 |
This macro indicates code to execute \textit{after} the \texttt{capTive} |
| 1301 |
environment scope is exited. |
| 1302 |
This invocation typesets the word `Here'. |
| 1303 |
We see that in the output, it occurs at normal size and in black, because |
| 1304 |
it is no longer subject to the color and fontsize that was left inside |
| 1305 |
the environment. |
| 1306 |
The \macname{tcafterenv} macro is unique in that it is an appending, rather |
| 1307 |
than a defining, macro. |
| 1308 |
Therefore, multiple invocations of it will append multiple code segments, |
| 1309 |
each of which will be executed upon environment exit. |
| 1310 |
It may be invoked in any of the directives, as well as the initialization and |
| 1311 |
close-out code segments. |
| 1312 |
|
| 1313 |
\medskip |
| 1314 |
For the close-out code, because it is executed after the token cycle is |
| 1315 |
complete, but before it is typeset, the updated value of \texttt{Tcount} |
| 1316 |
is available. |
| 1317 |
Anything typeset directly in the close-out code will occur before the |
| 1318 |
\macname{cytoks} token-list output is typeset. |
| 1319 |
Thus, the ``...t's capitalized.'' phrase is typeset before \macname{cytoks} |
| 1320 |
itself, even as \macname{theTcount} is accurate. |
| 1321 |
The \macname{addcytoks} occurring in the close-out code is placed at the |
| 1322 |
end of \macname{cytoks} and thus takes on the \macname{footnotesize} |
| 1323 |
declaration that is still in force. |
| 1324 |
\endgroup |
| 1325 |
|
| 1326 |
\begingroup |
| 1327 |
\medskip |
| 1328 |
Next, we consider a \TokCycle{} extended environment that counts words and |
| 1329 |
letters. |
| 1330 |
We try to build some features into the code so that, for example, spaces |
| 1331 |
and punctuation do not count as letters. |
| 1332 |
Further, we try to build the logic so that the following cases would |
| 1333 |
count as two words, not one: ``here...now'', ``Case(number)''. |
| 1334 |
|
| 1335 |
\medskip |
| 1336 |
We provide two modes of output: one that provides an ending summary, and |
| 1337 |
the other that keeps a running count. |
| 1338 |
As with other \TokCycle{} environments and macros, portions can be |
| 1339 |
exempted from the count (for example macro arguments) by using the |
| 1340 |
escape delimiters provided. |
| 1341 |
An even more elaborate version of this environment can be found at |
| 1342 |
\texttt{https://tex.stackexchange.com/questions/\allowbreak |
| 1343 |
44618/dynamically-count-and-return-number-of-words-in-a-section/587567\#587567},% |
| 1344 |
\hfil in\-cluding a version that runs |
| 1345 |
in Plain \TeX. |
| 1346 |
|
| 1347 |
\begin{exampleC}[]{The \macname{countem} extended-environment definition |
| 1348 |
and supporting macros} |
| 1349 |
\newcounter{wordcount} |
| 1350 |
\newcounter{lettercount} |
| 1351 |
\newif\ifinword |
| 1352 |
\newif\ifrunningcount |
| 1353 |
\newcommand\dumpword{% |
| 1354 |
\addcytoks[1]{\accumword}% |
| 1355 |
\tctestifcon\ifinword{\stepcounter{wordcount}% |
| 1356 |
\ifrunningcount\addcytoks[x]{$^{\thewordcount,\thelettercount}$}\fi}{}% |
| 1357 |
\inwordfalse\def\accumword{}} |
| 1358 |
\makeatletter |
| 1359 |
\newcommand\addletter[1]{% |
| 1360 |
\tctestifcatnx A#1{\stepcounter{lettercount}\inwordtrue}{\dumpword}% |
| 1361 |
\tc@defx\accumword{\accumword#1}} |
| 1362 |
\makeatother |
| 1363 |
|
| 1364 |
\xtokcycleenvironment\countem |
| 1365 |
{\addletter{##1}} |
| 1366 |
{\processtoks{##1}\dumpword} |
| 1367 |
{\dumpword\addcytoks{##1}} |
| 1368 |
{\dumpword\addcytoks{##1}} |
| 1369 |
{\def\accumword{}\setcounter{wordcount}{0}\setcounter{lettercount}{0}} |
| 1370 |
{\dumpword\ifrunningcount\else\tcafterenv{\par(Wordcount=\thewordcount, |
| 1371 |
Lettercount=\thelettercount)}\fi} |
| 1372 |
\end{exampleC} |
| 1373 |
|
| 1374 |
\begin{exampleA}[lefthand width=5.6cm]{Demo of the two modes of |
| 1375 |
\macname{countem}} |
| 1376 |
\countem |
| 1377 |
This is a test. |
| 1378 |
|
| 1379 |
But...punctuation does not count. |
| 1380 |
\endcountem |
| 1381 |
|
| 1382 |
\bigskip\runningcounttrue |
| 1383 |
\countem |
| 1384 |
This is a test. |
| 1385 |
|
| 1386 |
But...punctuation does not count. |
| 1387 |
\endcountem |
| 1388 |
\end{exampleA} |
| 1389 |
|
| 1390 |
\endgroup |
| 1391 |
|
| 1392 |
\subsection{Truncation of input} |
| 1393 |
|
| 1394 |
\begingroup |
| 1395 |
|
| 1396 |
\TokCycle{} has several facilities for truncating input, either |
| 1397 |
to the end of the current group or to the end of the input stream. |
| 1398 |
The basic commands are \macname{truncategroup} and \macname{truncatecycle}, |
| 1399 |
which can be part of the Character, Macro, or Space directives (see the |
| 1400 |
package documentation for truncation from the Group directive). |
| 1401 |
|
| 1402 |
\medskip |
| 1403 |
These commands may be issued directly or they may be placed as |
| 1404 |
one fork of a conditional test. |
| 1405 |
If the conditional test is the occurrence of a particular token in the |
| 1406 |
input stream, special conditional shortcuts have been formulated: |
| 1407 |
\macname{truncategroupiftokis} and \macname{truncatecycleiftokis}. |
| 1408 |
Both of these commands take two arguments: the first is the comparison |
| 1409 |
token against which to compare the tokens of the input stream, and the |
| 1410 |
second is the directive to follow for tokens that fail the |
| 1411 |
comparison test. |
| 1412 |
If the input stream token matches the comparitor, the relevant |
| 1413 |
truncation command is executed. |
| 1414 |
|
| 1415 |
\medskip |
| 1416 |
The description below shows the truncations to a given input text, |
| 1417 |
resulting from several different forms of truncating directives. |
| 1418 |
In the first case, the token cycle is to be truncated if a space |
| 1419 |
is found in the input stream. |
| 1420 |
Comparing to the input text, we see that the space occurs after the |
| 1421 |
word ``until'', and that is indeed where the \TokCycle{} output |
| 1422 |
ceases. |
| 1423 |
|
| 1424 |
\medskip |
| 1425 |
In the second case, the truncation is limited to the current group, |
| 1426 |
rather than the remainder of the input stream. |
| 1427 |
Again, the space following ``until'' is found, and the words |
| 1428 |
``certain token'' are excised from the input stream until the |
| 1429 |
end-of-group is located. |
| 1430 |
The cycle continues successfully with ``-is'' until the next space |
| 1431 |
is found before ``located''. |
| 1432 |
Since this space is found in the top grouping level of the input, |
| 1433 |
the remainder of the input stream is thereafter truncated. |
| 1434 |
|
| 1435 |
\medskip |
| 1436 |
In the third case, the cycle is to be truncated in the event the |
| 1437 |
letter `a' is found in the input stream. |
| 1438 |
Examining the input text, the first `a' is in the word ``certain'' and |
| 1439 |
so the input is truncated after the string ``cert'' is digested. |
| 1440 |
|
| 1441 |
\medskip |
| 1442 |
The final case, calls for group truncation if an `a' is found in the |
| 1443 |
input stream. |
| 1444 |
As before, the first `a' is found after ``cert''. |
| 1445 |
However, in the group case, only the remaining group content, consisting |
| 1446 |
of ``ain token'', is discarded. |
| 1447 |
At group end, the cycle successfully resumes until the `a' in |
| 1448 |
``located'' is reached. |
| 1449 |
Thus, ``-is loc'' is also added to the output stream. |
| 1450 |
|
| 1451 |
\def\thetext{Continue-\textit{until certain token}-is located.} |
| 1452 |
|
| 1453 |
\newcommand\rundemowith[1]{% |
| 1454 |
{\footnotesize\ttfamily\altdetokenize{#1}}: |
| 1455 |
\resettokcycle#1\expandafter\tokencyclexpress\thetext\endtokencyclexpress |
| 1456 |
} |
| 1457 |
|
| 1458 |
\begin{exampleB}[text only, righthand width=0.01cm, colback=white]% |
| 1459 |
{Example: Effects of various truncation directives} |
| 1460 |
{\footnotesize\ttfamily Input text}: \expandafter\altdetokenize\expandafter |
| 1461 |
{\thetext} |
| 1462 |
|
| 1463 |
\rundemowith{\Spacedirective{\truncatecycle}} |
| 1464 |
|
| 1465 |
\rundemowith{\Spacedirective{\truncategroup}} |
| 1466 |
|
| 1467 |
\rundemowith{\Characterdirective{\truncatecycleiftokis{a}{\addcytoks{#1}}}} |
| 1468 |
|
| 1469 |
\rundemowith{\Characterdirective{\truncategroupiftokis{a}{\addcytoks{#1}}}} |
| 1470 |
\end{exampleB} |
| 1471 |
|
| 1472 |
Note how the macro function, in this case \macname{textit}, is not |
| 1473 |
disrupted by the truncation. |
| 1474 |
Even as tokens are truncated from the input stream, group closures |
| 1475 |
are retained so that the final output is group-balanced. |
| 1476 |
For example, a detokenization of the final case reveals this to be |
| 1477 |
the case: |
| 1478 |
|
| 1479 |
\altdecytoks |
| 1480 |
|
| 1481 |
\endgroup |
| 1482 |
|
| 1483 |
\subsection{Look-ahead features} |
| 1484 |
|
| 1485 |
The whole concept of the \TokCycle{} package is to examine the input stream |
| 1486 |
one token at a time and use the directives to process the tokens. |
| 1487 |
Occasionally, however, there are tasks that involve several adjacent tokens. |
| 1488 |
In the example of section~\ref{s:cap}, for instance, in order to capitalize |
| 1489 |
words, each space token triggers an action on the character that follows. |
| 1490 |
To accomplish this without any look-ahead features, the Space directive sets |
| 1491 |
a flag, \verb|\def\capnext{T}|, in order to communicate with a the Character |
| 1492 |
directive about a subsequent token. |
| 1493 |
That is a perfectly valid approach to communicate conditional action. |
| 1494 |
|
| 1495 |
\medskip |
| 1496 |
However, \TokCycle{} also provides look-ahead features, to avoid the necessity |
| 1497 |
of communicating with future tokens in the cycle by way of flag settings. |
| 1498 |
These tools allow a directive to directly ``peek'' ahead at the input stream |
| 1499 |
(without disturbing it) and, if desired, push and pop tokens to and from |
| 1500 |
the input stream. |
| 1501 |
The package manual should be consulted for a full list of macros provided to |
| 1502 |
facilitate look-ahead capability. |
| 1503 |
But here are some examples to look at. |
| 1504 |
|
| 1505 |
\begingroup |
| 1506 |
|
| 1507 |
\subsubsection{Count in advance that which follows} |
| 1508 |
|
| 1509 |
Specify the point values for questions and let \TokCycle{} perform the cumulative |
| 1510 |
tallies in advance, using a single-pass compilation. |
| 1511 |
|
| 1512 |
\begin{exampleC}[]{The \texttt{pointtracker} environment to count point values |
| 1513 |
in advance} |
| 1514 |
\newcounter{pointcount} |
| 1515 |
\newcommand\addtopoints[1]{(\textit{#1 point}% |
| 1516 |
\ifnum#1=1\relax\else\textit{s}\fi)} |
| 1517 |
\NewEnviron{pointtracker}[2][points]{% |
| 1518 |
\par\bigskip\resettokcycle |
| 1519 |
\setcounter{pointcount}{0}% |
| 1520 |
\Macrodirective{\addcytoks{##1}\tctestifx{\addtopoints##1}{\sumpoints}{}}% |
| 1521 |
\def\tmp{\tokencyclexpress{\large\bfseries#2: \thepointcount\ #1}}% |
| 1522 |
\expandafter\tmp\BODY\endtokencyclexpress |
| 1523 |
} |
| 1524 |
\def\sumpoints{\tcpop\Q\addtocounter{pointcount}{\Q}\tcpushgroup{\Q}} |
| 1525 |
\end{exampleC} |
| 1526 |
|
| 1527 |
\begin{exampleA}[lefthand width=6.2cm]{Demo of the pointtracker environment} |
| 1528 |
\begin{pointtracker}[total points]{\Large Science Test} |
| 1529 |
\begin{pointtracker}{Advanced Geology} |
| 1530 |
\begin{itemize} |
| 1531 |
\item Count the fingers on your left hand. Are there more than 9? |
| 1532 |
\addtopoints{1} |
| 1533 |
\item Draw a rectangle with five edges. \addtopoints{3} |
| 1534 |
\end{itemize} |
| 1535 |
\end{pointtracker} |
| 1536 |
\begin{pointtracker}{Meteorology} |
| 1537 |
\begin{itemize} |
| 1538 |
\item How many borgs does it take to brew a Danish beer? |
| 1539 |
\addtopoints{2} |
| 1540 |
\item What is the meaning of life? \addtopoints{4} |
| 1541 |
\end{itemize} |
| 1542 |
\end{pointtracker} |
| 1543 |
\end{pointtracker} |
| 1544 |
\end{exampleA} |
| 1545 |
|
| 1546 |
Note that this example has several neat features. |
| 1547 |
The points for Geology, Meteorology, and the Test total are not specified, |
| 1548 |
but calculated. |
| 1549 |
They are calculated based on content that appears later in the environment. |
| 1550 |
Traditionally, multiple compilations would be required with external data |
| 1551 |
written to the aux file, but that does not occur here---there is only |
| 1552 |
a single compilation. |
| 1553 |
Finally, becaue we are nesting the \TokCycle{} part of \texttt{pointtracker} |
| 1554 |
inside of a \macname{NewEnviron}, we see that the \texttt{pointtracker} |
| 1555 |
environment can be nested, without adverse effect. |
| 1556 |
|
| 1557 |
\endgroup |
| 1558 |
|
| 1559 |
\subsubsection{Modify the arguments of every instance of a particular macro} |
| 1560 |
|
| 1561 |
\begingroup |
| 1562 |
|
| 1563 |
This example, through the use of the \macname{scalerule} macro, shows how |
| 1564 |
the \macname{rule} arguments, including the optional argument, can be |
| 1565 |
popped off the input stream, adjusted, and placed back in the stream. |
| 1566 |
|
| 1567 |
\begin{exampleC}[]{The \macname{scalerule} macro to scale all |
| 1568 |
\macname{rule} arguments} |
| 1569 |
\newcommand\scalerule[1]{% |
| 1570 |
\tcpeek\zA |
| 1571 |
\ifx[\zA\tcpopuntil]\opt\fi% IF PRESENT, POP OPTIONAL ARG |
| 1572 |
\tcpop\ArgA% POP ARG1 |
| 1573 |
\tcpop\ArgB% POP ARG2 |
| 1574 |
\tcpushgroup[x]{#1\dimexpr\ArgB}% PUSH BACK IN REVERSE (LIFO) FASHION |
| 1575 |
\tcpushgroup[x]{#1\dimexpr\ArgA} |
| 1576 |
\ifx[\zA\tcpush[x]{\expandafter\scaleopt\opt{#1}}\fi |
| 1577 |
} |
| 1578 |
\def\scaleopt[#1]#2{[#2\dimexpr#1]} |
| 1579 |
\end{exampleC} |
| 1580 |
|
| 1581 |
\begin{exampleA}[lefthand width=9.2cm]{Scale every \macname{rule} argument} |
| 1582 |
\resettokcycle |
| 1583 |
\settcEscapechar{!}% SO AS NOT TO INTERFERE WITH `|' TOKS IN tabular |
| 1584 |
\Macrodirective{\addcytoks{#1}\ifx\rule#1\scalerule{2}\fi} |
| 1585 |
\tokencyclexpress |
| 1586 |
The rules appearing in the following input stream are literally |
| 1587 |
!\rule[-2pt]{1.5ex}{1.5ex}! and !\rule{2ex}{3pt}!: |
| 1588 |
|
| 1589 |
My environment can include all kinds of rules |
| 1590 |
\rule[-2pt]{1.5ex}{1.5ex} and other macros, such as \today{} and |
| 1591 |
\begin{tabular}{c|c} |
| 1592 |
23 & 454\\\hline |
| 1593 |
x & y |
| 1594 |
\end{tabular} |
| 1595 |
but each rule \rule{2ex}{3pt} is grown by a factor of two. |
| 1596 |
\endtokencyclexpress |
| 1597 |
\end{exampleA} |
| 1598 |
\settcEscapechar{|} |
| 1599 |
|
| 1600 |
\endgroup |
| 1601 |
|
| 1602 |
|
| 1603 |
\subsubsection{Protect arguments of specified macro from Character |
| 1604 |
directive modification} |
| 1605 |
|
| 1606 |
\begingroup |
| 1607 |
Tokcycle provides the escape character $|$ to delimit tokens to be protected |
| 1608 |
from processing by \TokCycle{} directives. |
| 1609 |
But that requires the input stream to be adjusted to include these delimiters. |
| 1610 |
Here's an alternative, not requiring input stream adjustment. |
| 1611 |
The Macro directive protects all \macname{rule} arguments against contamination |
| 1612 |
by the Character directive. |
| 1613 |
|
| 1614 |
\medskip |
| 1615 |
When a \macname{rule} is encountered, \macname{protectruleargs} is called |
| 1616 |
to pop the rule arguments from the input stream and place them directly |
| 1617 |
in the \macname{cytoks} token list, out of reach from the Character directive. |
| 1618 |
|
| 1619 |
|
| 1620 |
\begin{exampleC}[]{The \macname{protectruleargs} macro to prevent Character directive |
| 1621 |
contamination of arguments} |
| 1622 |
\newcommand\protectruleargs[1]{\ifx\rule#1% |
| 1623 |
\tcpeek\zA |
| 1624 |
\ifx[\zA\tcpopuntil]\opt |
| 1625 |
\addcytoks[1]{\opt}\fi% IF OPTIONAL ARG PRESENT, POP AND SAVE TO \cytoks |
| 1626 |
\tcpopliteral\ArgA% MUST POP ARG 1 WITH BRACES |
| 1627 |
\addcytoks[1]{\ArgA}% |
| 1628 |
\tcpopliteral\ArgB% MUST POP ARG 2 WITH BRACES |
| 1629 |
\addcytoks[1]{\ArgB}% |
| 1630 |
\fi |
| 1631 |
} |
| 1632 |
\end{exampleC} |
| 1633 |
|
| 1634 |
Here, every character is typeset with a slash suffix. |
| 1635 |
If this action were to be imposed to \macname{rule} argument characters, |
| 1636 |
it would destroy their integrity. |
| 1637 |
Fortunately, when the Macro directive detects a \macname{rule}, it can |
| 1638 |
take evasive action. |
| 1639 |
|
| 1640 |
\begin{exampleA}[lefthand width=9.1cm]{Protect every \macname{rule} argument |
| 1641 |
from Character directive contamination} |
| 1642 |
\resettokcycle |
| 1643 |
\Characterdirective{\addcytoks{#1\textup{/}\allowbreak}} |
| 1644 |
\Macrodirective{\addcytoks{#1}\protectruleargs{#1}} |
| 1645 |
\tokencyclexpress |
| 1646 |
My \textit{environment} modifies each character token, but rule |
| 1647 |
\rule[-4pt]{1.5ex}{1.5ex} arguments \rule{2ex}{3pt} are protected. |
| 1648 |
\endtokencyclexpress |
| 1649 |
\end{exampleA} |
| 1650 |
\endgroup |
| 1651 |
|
| 1652 |
|
| 1653 |
\begingroup |
| 1654 |
|
| 1655 |
\subsubsection{Find a string of character tokens} |
| 1656 |
|
| 1657 |
Earlier in this manuscript, in section~\ref{s:searchA}, a token cycle |
| 1658 |
was developed to search a token stream for a string match. |
| 1659 |
Here, we do something similar, except we use the new ``look ahead'' features |
| 1660 |
of \TokCycle{} to achieve it. |
| 1661 |
In this case, however, the search string cannot cross space or group |
| 1662 |
boundaries. |
| 1663 |
The \macname{matchnextchar} macro demonstrates a use of \macname{tcpopappto}, |
| 1664 |
which is one of the newly introduced \TokCycle{} commands; instead of |
| 1665 |
popping an argument from the input stream \textit{into} a macro, it pops it |
| 1666 |
and \textit{appends} it to a macro. |
| 1667 |
|
| 1668 |
\begin{exampleC}[]{The \macname{findstring} macro to perform a look{-}ahead string |
| 1669 |
search} |
| 1670 |
\newcommand\findstring[4]{% {CURRENT TOK}{MATCH STRING}{SUCCESS OP}{FAIL CMDS} |
| 1671 |
\foundstringfalse% DEFAULT CONDITION |
| 1672 |
\def\poptoks{}% INITIALIZATION. IF MATCH FAILS, MUST TO RESTORE POPPED TOKENS |
| 1673 |
\expandafter\matchnextchar\ifx#1#2\relax\relax\fi\empty\relax |
| 1674 |
\iffoundstring\addcytoks{#3{#2}}\else\tcpush{\poptoks}#4\fi |
| 1675 |
} |
| 1676 |
\def\matchnextchar#1#2\relax{\tctestifx{#1\empty}{}{% NO MATCH FROM 1ST TOKEN |
| 1677 |
\tctestifx{\relax#1}{% |
| 1678 |
\foundstringtrue% NOTHING LEFT TO COMPARE; MATCH FOUND |
| 1679 |
}{% |
| 1680 |
\tcpeek\Q% WITHOUT POPPING... |
| 1681 |
\tctestifx{\Q\bgroup}{}{% PEEK FOR GROUPS OR... |
| 1682 |
\tctestifx{\Q\tcsptoken}{}{% SPACES THAT INDICATE MATCH FAILURE |
| 1683 |
\tcpopappto\poptoks% POP TOKEN AND APPEND TO \poptoks |
| 1684 |
\expandafter\tctestifx\expandafter{\Q#1}{% |
| 1685 |
\matchnextchar#2\relax% STILL MATCHING; KEEP GOING |
| 1686 |
}{}% |
| 1687 |
}% |
| 1688 |
}% |
| 1689 |
}% |
| 1690 |
}} |
| 1691 |
\end{exampleC} |
| 1692 |
|
| 1693 |
\begin{exampleA}[lefthand width=8cm]{Search for the string ``red''} |
| 1694 |
\resettokcycle |
| 1695 |
\Characterdirective{\findstring{#1}{red}% |
| 1696 |
{\textcolor{purple}}{\addcytoks{#1}}} |
| 1697 |
\tokencyclexpress |
| 1698 |
Can I \textit{find red in the text}, even in the |
| 1699 |
MIDredDLE of words and groups? |
| 1700 |
\endtokencyclexpress |
| 1701 |
\end{exampleA} |
| 1702 |
|
| 1703 |
\endgroup |
| 1704 |
|
| 1705 |
|
| 1706 |
\subsubsection{Eliminate \textit{prior} spaces and pars before a particular |
| 1707 |
macro} |
| 1708 |
|
| 1709 |
The goal here is to eliminate spaces and paragraphs that occur |
| 1710 |
\textit{prior} to the occurrence of the macro \macname{foo}, which |
| 1711 |
sets a \macname{bullet}. |
| 1712 |
Looking backwards is not part of the \TeX{} approach. |
| 1713 |
Nonetheless, the problem can, with a token cycle, be turned on its head, |
| 1714 |
such that all occurrences of spaces and \macname{par} can be made |
| 1715 |
to look for a future instance of \macname{foo}. |
| 1716 |
|
| 1717 |
\medskip |
| 1718 |
Here, if a space or \macname{par} is detected, \macname{foocheck} is called. |
| 1719 |
It peeks at the token that follows the space or \macname{par}. |
| 1720 |
If the peeked argument is a \macname{par} or \macname{foo} itself, it discards |
| 1721 |
the current space or \macname{par}. |
| 1722 |
|
| 1723 |
\begingroup |
| 1724 |
|
| 1725 |
\begin{exampleC}[]{The \macname{dofoo} environment to remove prior spaces and |
| 1726 |
paragraphs before \macname{foo}} |
| 1727 |
\newcommand\foo{$\bullet$} |
| 1728 |
\newcommand\foocheck[1]{\tcpeek\z\ifx\foo\z\else |
| 1729 |
\ifx\par\z\else\addcytoks{#1}\fi\fi} |
| 1730 |
\tokcycleenvironment\dofoo |
| 1731 |
{\addcytoks{##1}} |
| 1732 |
{\processtoks{##1}} |
| 1733 |
{\ifx\par##1\foocheck{##1}\else\addcytoks{##1}\fi} |
| 1734 |
{\foocheck{##1}} |
| 1735 |
\end{exampleC} |
| 1736 |
|
| 1737 |
\begin{exampleA}[lefthand width=7cm]{Remove spaces and paragraphs before |
| 1738 |
\macname{foo}} |
| 1739 |
\dofoo |
| 1740 |
Lorem ipsum dolor sit amet. (No foo) |
| 1741 |
|
| 1742 |
Lorem ipsum dolor sit amet. \foo |
| 1743 |
|
| 1744 |
Lorem ipsum dolor sit amet.\foo |
| 1745 |
|
| 1746 |
Lorem ipsum dolor sit amet. |
| 1747 |
\foo |
| 1748 |
|
| 1749 |
Lorem ipsum dolor sit amet. |
| 1750 |
|
| 1751 |
\foo |
| 1752 |
\enddofoo |
| 1753 |
\end{exampleA} |
| 1754 |
\endgroup |
| 1755 |
|
| 1756 |
|
| 1757 |
\subsubsection{Replace := with \macname{coloneqq} (\texttt{mathtools} |
| 1758 |
package required)} |
| 1759 |
|
| 1760 |
In most cases, creating a macro to signify a composite symbol makes the most |
| 1761 |
sense. |
| 1762 |
However, sometimes you don't realize that until you've typed in a lot of code |
| 1763 |
that fails to use such a macro. |
| 1764 |
In such a situation, \TokCycle{} can help. |
| 1765 |
|
| 1766 |
\medskip |
| 1767 |
In the \texttt{mathtools} package, there is a symbol, \macname{coloneqq}, |
| 1768 |
a colon followed by an equal sign, in which the colon has been |
| 1769 |
vertically adjusted to be symmetric with respect to the \texttt{=} sign. |
| 1770 |
The adjustment is slight, but noticeable. |
| 1771 |
A token cycle can be set up to look for instances of the \texttt{:} character, |
| 1772 |
peek ahead to see if it is followed by an \texttt{=} character and, if so, |
| 1773 |
replace the pair with \macname{coloneqq}. |
| 1774 |
However, the process is further complicated, if one is a user of |
| 1775 |
\macname{usepackage[french]\{babel\}}, where the \texttt{:} character is is made |
| 1776 |
an active character. |
| 1777 |
|
| 1778 |
\medskip |
| 1779 |
Therefore, in this case, not only does \texttt{:} need to be located by |
| 1780 |
the Character directive (in the normal case), but it also need be searched |
| 1781 |
for by the Macro directive, in the event that |
| 1782 |
\macname{usepackage[french]\{babel\}} is being employed. |
| 1783 |
In the example below, there are two such colons that satisfy the criterion for |
| 1784 |
replacement. |
| 1785 |
|
| 1786 |
\begingroup |
| 1787 |
|
| 1788 |
\begin{exampleC}[]{The \macname{coleqchek} macro and the \macname{subcoloneqq} |
| 1789 |
environment} |
| 1790 |
\newcommand\coleqchek[1]{\tcpeek\z |
| 1791 |
\ifx=\z\tcpop\z\addcytoks{\coloneqq}\else\addcytoks{#1}\fi} |
| 1792 |
\tokcycleenvironment\subcoloneqq |
| 1793 |
{\tctestifx{:##1}{\coleqchek{##1}}{\addcytoks{##1}}}% <- IF : A CHARACTER |
| 1794 |
{\processtoks{##1}} |
| 1795 |
{\tctestifcon{\if\detokenize{:}\detokenize{##1}}% <- IF : AN ACTIVE MACRO |
| 1796 |
{\coleqchek{##1}}{\addcytoks{##1}}} |
| 1797 |
{\addcytoks{##1}} |
| 1798 |
\end{exampleC} |
| 1799 |
|
| 1800 |
\begin{exampleA}[lefthand width=6cm]{replace {:=} with \macname{coloneqq}} |
| 1801 |
\subcoloneqq |
| 1802 |
\LARGE Compare: $::=$ |
| 1803 |
|
| 1804 |
$a := b$ vs |
| 1805 |
$a \ordinarycolon= b$ vs |
| 1806 |
$a:b$. |
| 1807 |
\endsubcoloneqq |
| 1808 |
\end{exampleA} |
| 1809 |
|
| 1810 |
\endgroup |
| 1811 |
|
| 1812 |
\let\svtilde~ |
| 1813 |
\let~\\ |
| 1814 |
\section{Advanced topics: implicit and active tokens, catcode~changes} |
| 1815 |
\let~\svtilde |
| 1816 |
|
| 1817 |
\subsection{Trap active characters (catcode 13)} |
| 1818 |
|
| 1819 |
%\begin{minipage}[t]{\textwidth} |
| 1820 |
\begingroup |
| 1821 |
Active characters in the \TokCycle{} input stream are processed in |
| 1822 |
their original form. |
| 1823 |
Their active substitutions arising from \macname{def}s |
| 1824 |
only occur \textit{afterwards}, when |
| 1825 |
the \TokCycle{} output is typeset. |
| 1826 |
They may be identified with the \macname{ifactivetok} test. |
| 1827 |
If \macname{let} to a character, they may be identified in the Character |
| 1828 |
directive; |
| 1829 |
If \macname{let} to a control sequence or defined via \macname{def}, |
| 1830 |
they may be identified in the Macro directive. |
| 1831 |
For information on how to process active spaces, please refer to |
| 1832 |
section~\ref{s:spc}. |
| 1833 |
\let\svt T |
| 1834 |
\Characterdirective{\tctestifcon\ifactivetok |
| 1835 |
{\addcytoks{\fbox{#1-chr}}}{\addcytoks{#1}}} |
| 1836 |
\Macrodirective{\tctestifcon\ifactivetok |
| 1837 |
{\addcytoks{\fbox{#1-mac}}}{\addcytoks{#1}}} |
| 1838 |
|
| 1839 |
\begin{exampleA}[lefthand width=7.8cm]{Processing active characters} |
| 1840 |
\resettokcycle |
| 1841 |
\tokencyclexpress |
| 1842 |
1. This is a test!!\endtokencyclexpress |
| 1843 |
|
| 1844 |
2. \catcode`!=\active |
| 1845 |
\def !{?}% |
| 1846 |
\tokencyclexpress |
| 1847 |
This is a test!!\endtokencyclexpress |
| 1848 |
|
| 1849 |
3. |
| 1850 |
\Characterdirective{\tctestifcon\ifactivetok |
| 1851 |
{\addcytoks{\fbox{#1-chr}}}{\addcytoks{#1}}}% |
| 1852 |
\Macrodirective{\tctestifcon\ifactivetok |
| 1853 |
{\addcytoks{\fbox{#1-mac}}}{\addcytoks{#1}}}% |
| 1854 |
\tokencyclexpress |
| 1855 |
This is a test!!\endtokencyclexpress |
| 1856 |
|
| 1857 |
4. \catcode`T=\active |
| 1858 |
\let T+% |
| 1859 |
\tokencyclexpress |
| 1860 |
This is a test!!\endtokencyclexpress |
| 1861 |
|
| 1862 |
5. \detokenize\expandafter{\the\cytoks} |
| 1863 |
\end{exampleA} |
| 1864 |
|
| 1865 |
%%% |
| 1866 |
|
| 1867 |
\ddbend If the input stream is pre-\textit{expanded}, any active |
| 1868 |
substitutions that are expandable (i.e., those involving \macname{def} |
| 1869 |
as well as those \macname{let} to something expandable) |
| 1870 |
are made before reaching \TokCycle{} processing. |
| 1871 |
\svt hey are, thus, no longer detected as active, unless |
| 1872 |
\macname{noexpand} is applied before the pre-expansion. |
| 1873 |
In this example, the `\texttt{!}' that is \textit{not} subject to |
| 1874 |
a \macname{noexpand} is converted to a `\texttt{?}' prior to |
| 1875 |
reaching \TokCycle{} processing (and thus, not detected as |
| 1876 |
\macname{active}). |
| 1877 |
However, the `\texttt{T}' is not pre-converted to a `\texttt{+}', |
| 1878 |
because it is implicit, and not a definition: |
| 1879 |
|
| 1880 |
\catcode`!=\active |
| 1881 |
\def !{?} |
| 1882 |
\catcode`T=\active |
| 1883 |
\let T+ |
| 1884 |
\begin{exampleA}[lefthand width=7.0cm]{Expanded input stream acts |
| 1885 |
upon active \macname{def}ed characters unless \macname{noexpand} is applied} |
| 1886 |
\expandedtokcyclexpress{This is a test!\noexpand!} |
| 1887 |
\the\cytoks\par |
| 1888 |
\detokenize\expandafter{\the\cytoks} |
| 1889 |
\end{exampleA} |
| 1890 |
|
| 1891 |
\begingroup |
| 1892 |
\catcode`T=11 |
| 1893 |
\catcode`!=12 |
| 1894 |
However, pre-tokenization does not suffer this behavior. |
| 1895 |
The first `\texttt{!}' is tokenized as active during |
| 1896 |
its absorption into \macname{tmp}, as shown in the next example: |
| 1897 |
\endgroup |
| 1898 |
\begin{exampleA}[lefthand width=7.8cm]{Pre-tokenized input stream |
| 1899 |
does not affect active characters} |
| 1900 |
\def\tmp{This is a test!!} |
| 1901 |
\expandafter\tokcyclexpress\expandafter{\tmp} |
| 1902 |
\the\cytoks\par |
| 1903 |
\detokenize\expandafter{\the\cytoks} |
| 1904 |
\end{exampleA} |
| 1905 |
|
| 1906 |
%%% |
| 1907 |
\endgroup |
| 1908 |
%\end{minipage} |
| 1909 |
|
| 1910 |
\begingroup |
| 1911 |
\ddbend One aspect of \TeX{} to remember is that catcodes |
| 1912 |
are assigned at tokenization; however, for active characters, |
| 1913 |
the substitution assignment is evaluated only upon execution. |
| 1914 |
So, if a cat-13 token is placed into a \macname{def}, it will |
| 1915 |
remain active even if the catcode of that character code is |
| 1916 |
later changed. |
| 1917 |
But if the cat-13 active definition is changed prior to the |
| 1918 |
execution of the \macname{def}'ed token, the revised |
| 1919 |
token assignment will apply. |
| 1920 |
|
| 1921 |
\medskip |
| 1922 |
The following example demonstrates this concept, while showing, |
| 1923 |
without changing the input in any way, that \TokCycle{} |
| 1924 |
can properly digest active and implicit grouping (cat-1,2) characters: |
| 1925 |
|
| 1926 |
\begin{exampleA}[lefthand width=8cm]{Active and implicit grouping |
| 1927 |
tokens digestible by \TokCycle} |
| 1928 |
\catcode`Y=13 |
| 1929 |
\catcode`Z=13 |
| 1930 |
\let Y{ |
| 1931 |
\let Z} |
| 1932 |
\let\Y{ |
| 1933 |
\let\Z} |
| 1934 |
\def\tmp{\textit YabcZ de\Y\itshape f\Zg}% |
| 1935 |
|
| 1936 |
\def Y{\bgroup[NEW]}% APPLIES AT EXECUTION |
| 1937 |
\catcode`Y=11% DOES NOT AFFECT Y IN \tmp |
| 1938 |
|
| 1939 |
\expandafter\tokcyclexpress\expandafter{\tmp} |
| 1940 |
\the\cytoks |
| 1941 |
|
| 1942 |
\detokenize\expandafter{\the\cytoks} |
| 1943 |
\end{exampleA} |
| 1944 |
\endgroup |
| 1945 |
|
| 1946 |
\subsection{Digest/discern Unicode characters in pdflatex\label{s:unicode}} |
| 1947 |
|
| 1948 |
When using the lualatex or xelatex engines, the presence of Unicode characters |
| 1949 |
poses no problem. |
| 1950 |
In the context of a token cycle, each such character is digested and passed as |
| 1951 |
an individual token to the Character directive. |
| 1952 |
|
| 1953 |
\medskip |
| 1954 |
In pdflatex, however, which digests input one byte at a time, Unicode characters |
| 1955 |
are represented in UTF-8 encoding as multi-byte sequences of two to four bytes |
| 1956 |
in length. |
| 1957 |
These tokens are made active and the leading bits of the first byte can be used |
| 1958 |
to decipher the length of the Unicode character encoding. |
| 1959 |
Since tokens in pdflatex are composed of single bytes, a token cycle in the |
| 1960 |
context of pdflatex only gets to digest one byte at a time of a given Unicode |
| 1961 |
character. |
| 1962 |
The key to digesting and decoding Unicode characters therefore, is to look |
| 1963 |
for active tokens coming into the Macro directive. |
| 1964 |
If such a byte is found, its slot should be determined. |
| 1965 |
Slots 192--223 indicate a 2-byte Unicode character and so one additional byte |
| 1966 |
needs to be absorbed to compose the full Unicode character. |
| 1967 |
If the first byte resides in slots 224--239, a 3-byte encoding is called for, |
| 1968 |
so that two additional bytes need to be absorbed to reconstitute the full |
| 1969 |
character. |
| 1970 |
Finally, if the slot of the active byte exceeds 239, a 4-byte encoding is called |
| 1971 |
for and three additional bytes must be absorbed to reformulate the Unicode |
| 1972 |
character. |
| 1973 |
The following code shows exactly how this is done using an example. |
| 1974 |
|
| 1975 |
\begin{exampleC}[]{Setting up a token cycle to digest/discern Unicode characters |
| 1976 |
in pdflatex} |
| 1977 |
% PREAMBLE SETUP FOR NEEDED UNICODE TOKENS |
| 1978 |
\DeclareUnicodeCharacter{1D60}{$\varphi$} |
| 1979 |
\DeclareUnicodeCharacter{1D131}{$\sharp$} |
| 1980 |
% |
| 1981 |
\newif\ifUnicode |
| 1982 |
\resettokcycle |
| 1983 |
\Macrodirective{% |
| 1984 |
\ifactivetok\digestactivechar{#1}% |
| 1985 |
\ifUnicode\addcytoks[1]{\expandafter\makered\expandafter{\tcactivechar}}% |
| 1986 |
\else\addcytoks{#1}\fi |
| 1987 |
\else |
| 1988 |
\addcytoks{#1}% |
| 1989 |
\fi} |
| 1990 |
\newcommand\tcdigestbyte{\tcpopappto\tcactivechar} |
| 1991 |
\newcommand\digestactivechar[1]{\Unicodefalse |
| 1992 |
\def\tcactivechar{#1}% |
| 1993 |
\ifnum`#1>239 \Unicodetrue\tcdigestbyte\tcdigestbyte\tcdigestbyte\else |
| 1994 |
\ifnum`#1>223 \Unicodetrue\tcdigestbyte\tcdigestbyte\else |
| 1995 |
\ifnum`#1>191 \Unicodetrue\tcdigestbyte\fi\fi\fi} |
| 1996 |
\newcommand\makered{\textcolor{red}} |
| 1997 |
\end{exampleC} |
| 1998 |
|
| 1999 |
\begingroup |
| 2000 |
\begin{exampleD}[]{Executing a pdflatex token cycle able to digest/discern |
| 2001 |
Unicode characters} |
| 2002 |
\small |
| 2003 |
\newif\ifUnicode |
| 2004 |
\resettokcycle |
| 2005 |
\Macrodirective{% |
| 2006 |
\ifactivetok\digestactivechar{#1}% |
| 2007 |
\ifUnicode\addcytoks[1]{\expandafter\makered\expandafter{\tcactivechar}}% |
| 2008 |
\else\addcytoks{#1}\fi |
| 2009 |
\else |
| 2010 |
\addcytoks{#1}% |
| 2011 |
\fi} |
| 2012 |
\newcommand\tcdigestbyte{\tcpopappto\tcactivechar} |
| 2013 |
\newcommand\digestactivechar[1]{\Unicodefalse |
| 2014 |
\def\tcactivechar{#1}% |
| 2015 |
\ifnum`#1>239 \Unicodetrue\tcdigestbyte\tcdigestbyte\tcdigestbyte\else |
| 2016 |
\ifnum`#1>223 \Unicodetrue\tcdigestbyte\tcdigestbyte\else |
| 2017 |
\ifnum`#1>191 \Unicodetrue\tcdigestbyte\fi\fi\fi} |
| 2018 |
\newcommand\makered{\textcolor{red}} |
| 2019 |
\begingroup |
| 2020 |
\Macrodirective{% |
| 2021 |
\ifactivetok\digestactivechar{#1}% |
| 2022 |
\ifUnicode\addcytoks{\begingroup\fontencoding{T2A}\selectfont}% |
| 2023 |
\addcytoks[1]{\expandafter{\tcactivechar}\endgroup} |
| 2024 |
\else\ifx~#1\addcytoks{\string#1}\else\addcytoks{#1\mbox{}}\fi\fi |
| 2025 |
\else |
| 2026 |
\addcytoks{\string#1} |
| 2027 |
\fi} |
| 2028 |
\obeylines\obeyspaces |
| 2029 |
\ttfamily |
| 2030 |
\string\tokencyclexpress \tokencyclexpress On \today, we are running input through a token cycle |
| 2031 |
that highlights Unicode characters in red: |
| 2032 |
|
| 2033 |
Testing an active tilde: (~) and a Euro sign: €. |
| 2034 |
|
| 2035 |
Accented Latin Characters: |
| 2036 |
á, é, ó, ú, ü, ñ, ç, à, è, ò, ù, â, ê, ô, û, ã, |
| 2037 |
õ, ä, ö, ü, ß, œ. |
| 2038 |
|
| 2039 |
Cyrillic Characters: |
| 2040 |
А, Б, В, Г, Д, Е, Ё, Ж, З, И, Й, К, Л, М, Н, О, П, |
| 2041 |
Р, С, Т, У, Ф, Х, Ц, Ч, Ш, Щ, Ъ, Ы, Ь, Э, Ю, Я. |
| 2042 |
|
| 2043 |
Пример текста на кириллице: Здравствуйте, Latin, как дела? |
| 2044 |
|
| 2045 |
Higher level (3, 4-byte) Unicode (unsupported by font): |
| 2046 |
ᵠ, and 𝄱 |
| 2047 |
\endtokencyclexpress\string\endtokencyclexpress\endgroup |
| 2048 |
|
| 2049 |
\vspace{-7pt}\hrulefill |
| 2050 |
\fontencoding{T2A}\selectfont |
| 2051 |
%\includegraphics{UNICODE DECODE} |
| 2052 |
|
| 2053 |
\tokencyclexpress On \today, we are running input through a token cycle |
| 2054 |
that highlights Unicode characters in red: |
| 2055 |
|
| 2056 |
Testing an active tilde: (~) and a Euro sign: €. |
| 2057 |
|
| 2058 |
Accented Latin Characters: |
| 2059 |
á, é, ó, ú, ü, ñ, ç, à, è, ò, ù, â, ê, ô, û, ã, |
| 2060 |
õ, ä, ö, ü, ß, œ. |
| 2061 |
|
| 2062 |
Cyrillic Characters: |
| 2063 |
А, Б, В, Г, Д, Е, Ё, Ж, З, И, Й, К, Л, М, Н, О, П, |
| 2064 |
Р, С, Т, У, Ф, Х, Ц, Ч, Ш, Щ, Ъ, Ы, Ь, Э, Ю, Я. |
| 2065 |
|
| 2066 |
Пример текста на кириллице: Здравствуйте, Latin, как дела? |
| 2067 |
|
| 2068 |
Higher level (3, 4-byte) Unicode (unsupported by font): |
| 2069 |
ᵠ, and 𝄱 |
| 2070 |
\endtokencyclexpress |
| 2071 |
\end{exampleD} |
| 2072 |
|
| 2073 |
\endgroup |
| 2074 |
|
| 2075 |
\subsection{Trap catcode 6 (explicit \& implicit) tokens} |
| 2076 |
|
| 2077 |
\begingroup |
| 2078 |
|
| 2079 |
\let\myhash# |
| 2080 |
|
| 2081 |
\medskip |
| 2082 |
|
| 2083 |
Typically, cat-6 tokens (like \texttt{\#}) are used to designate the |
| 2084 |
following digit (1-9) as a parameter. |
| 2085 |
Since they are unlikely to be used in that capacity inside a |
| 2086 |
\TokCycle{} input stream, the package behavior is to convert them |
| 2087 |
into something cat-12 and set the if-condition \macname{catSIXtrue}. |
| 2088 |
In this manner, \macname{ifcatSIX} can be used inside the |
| 2089 |
Character directive to convert cat-6 tokens into something of |
| 2090 |
the user's choosing. |
| 2091 |
|
| 2092 |
\medskip |
| 2093 |
As to this cat-12 conversion, explicit cat-6 characters are converted |
| 2094 |
into the same character with cat-12. |
| 2095 |
On the other hand, implicit cat-6 control sequences (e.g., \verb|\let\myhash#|) |
| 2096 |
are converted into a fixed-name macro, \macname{implicitsixtok}, |
| 2097 |
whose cat-12 substitution text is a \macname{string} of the original |
| 2098 |
implicit-macro name. |
| 2099 |
|
| 2100 |
\begingroup |
| 2101 |
\resettokcycle |
| 2102 |
\Characterdirective{\ifcatSIX |
| 2103 |
\addcytoks{\fbox{#1}} |
| 2104 |
\else\addcytoks{#1}\fi} |
| 2105 |
|
| 2106 |
%\tokencyclexpress This# isQ \textit{a Q# test\myhash}!\endtokencyclexpress |
| 2107 |
|
| 2108 |
\begin{exampleA}[lefthand width=6.5cm]{Treatment of cat-6 tokens} |
| 2109 |
\resettokcycle |
| 2110 |
\Characterdirective{\ifcatSIX |
| 2111 |
\addcytoks{\fbox{#1}} |
| 2112 |
\else\addcytoks{#1}\fi} |
| 2113 |
\let\myhash# |
| 2114 |
\tokcyclexpress{This# isQ |
| 2115 |
\textit{a Q# test\myhash}!} |
| 2116 |
\the\cytoks\bigskip\par |
| 2117 |
\detokenize\expandafter{\the\cytoks} |
| 2118 |
\end{exampleA} |
| 2119 |
|
| 2120 |
%\tokcyclexpress{This# isQ \textit{a Q# t\,e\, s\,t\myhash}!}\the\cytoks{} |
| 2121 |
%with 1-character macros |
| 2122 |
|
| 2123 |
%%% |
| 2124 |
%\expandedtokcyclexpress{This# isQ \noexpand\textit{a Q# test\myhash}!}\the\cytoks |
| 2125 |
%{} PRE-EXPANDED |
| 2126 |
%%% |
| 2127 |
|
| 2128 |
|
| 2129 |
%\tokencyclexpress This# isQ \textit{a Q# test}!\endtokencyclexpress |
| 2130 |
|
| 2131 |
%\Characterdirective{\ifcatSIX\addcytoks{\fbox{#1}}\else\addcytoks{#1}\fi} |
| 2132 |
|
| 2133 |
\begin{exampleA}[lefthand width=6.5cm]{Multiple explicit cat-6 tokens are not |
| 2134 |
a problem} |
| 2135 |
\catcode`Q=6 |
| 2136 |
\tokcyclexpress{This# isQ |
| 2137 |
\textit{a Q# test\myhash}!} |
| 2138 |
\the\cytoks |
| 2139 |
\end{exampleA} |
| 2140 |
|
| 2141 |
\catcode`Q=11 |
| 2142 |
\hrulefill |
| 2143 |
\endgroup |
| 2144 |
|
| 2145 |
\ddbend For what is, perhaps, a rare situation, one can even process input |
| 2146 |
streams that contain cat-6 macro parameters. |
| 2147 |
A package macro, \macname{whennotprocessingparameter\#1\{<}% |
| 2148 |
\textit{directive when not a parameter}\verb|>}|, can be |
| 2149 |
used inside of the Character directive to intercept parameters. |
| 2150 |
In this example, a macro is defined and then executed, subject to |
| 2151 |
token replacements brought about by the expandable Character directive. |
| 2152 |
|
| 2153 |
\begin{exampleA}[lefthand width=7cm]{Preserving parameters (e.g. |
| 2154 |
\texttt{\#1, \#2}) in the \TokCycle{} input stream} |
| 2155 |
\Characterdirective{% |
| 2156 |
\whennotprocessingparameter#1{% |
| 2157 |
\addcytoks[x]{\vowelcap{#1}}}} |
| 2158 |
\tokcyclexpress{% |
| 2159 |
\def\zQ#1#2{[one:#1](two:#2)} |
| 2160 |
This is a \zQ big test. |
| 2161 |
|
| 2162 |
\renewcommand\zQ[2]{\ifx t#1[#1]\fi(#2)} |
| 2163 |
This is a \zQ test.} |
| 2164 |
\the\cytoks |
| 2165 |
\end{exampleA} |
| 2166 |
|
| 2167 |
\altdecytoks |
| 2168 |
|
| 2169 |
\endgroup |
| 2170 |
|
| 2171 |
\subsection{Trap implicit tokens in general} |
| 2172 |
|
| 2173 |
\begingroup |
| 2174 |
|
| 2175 |
Implicit control sequences (assigned via \macname{let} to characters) |
| 2176 |
were already mentioned in the context of cat-6. |
| 2177 |
However, implicit control sequences can be of any valid catcode (except |
| 2178 |
for cat-0, which we instead call macros or primitives). |
| 2179 |
The condition \macname{ifimplicittok} is used to flag such |
| 2180 |
tokens for special processing, as well as active tokens that |
| 2181 |
are \macname{let} to anything unexpandable. |
| 2182 |
|
| 2183 |
In the next example, implicit, cat-6 and implicit-cat-6 tokens may |
| 2184 |
all be differentiated, shown here with a multiplicity of |
| 2185 |
\macname{fbox}es. |
| 2186 |
|
| 2187 |
\fboxsep=1.5pt |
| 2188 |
|
| 2189 |
\begin{exampleA}[lefthand width=8cm]{Implicit\ensuremath{{}={}}% |
| 2190 |
single box\char44 \ cat-6\ensuremath{{}={}}double box\char44 \ % |
| 2191 |
implicit-cat-6\ensuremath{{}={}}triple box} |
| 2192 |
\catcode`Q=\active \let QN |
| 2193 |
\let\littlet=t |
| 2194 |
\let\littlel=l |
| 2195 |
\let\svhash# |
| 2196 |
\Characterdirective{\ifimplicittok |
| 2197 |
\ifcatSIX\addcytoks{\fbox{\fbox{\fbox{#1}}}}% |
| 2198 |
\else\addcytoks{\fbox{#1}}\fi\else\ifcatSIX |
| 2199 |
\addcytoks{\fbox{\fbox{#1}}}\else |
| 2200 |
\addcytoks{#1}\fi\fi} |
| 2201 |
|
| 2202 |
\tokencyclexpress We wi\littlel\littlel# |
| 2203 |
\textit{ make a \littlet est #} \littlet |
| 2204 |
|
| 2205 |
This \textit{is a \textbf{big}} \littlet est. |
| 2206 |
|
| 2207 |
Qext pa#agraph ending with implicit cat six |
| 2208 |
\svhash.\endtokencyclexpress |
| 2209 |
\end{exampleA} |
| 2210 |
|
| 2211 |
In the following example, we use both control sequences and active characters |
| 2212 |
in \macname{def} and \macname{let} capacities, to demonstrate how |
| 2213 |
\TokCycle{} digests things. |
| 2214 |
Implicit tokens (tokens \macname{let} to characters) are shown in a box, |
| 2215 |
with both the token name and the implicit value (note that tokens |
| 2216 |
\macname{let} to macros and primitives are not considered implicit). |
| 2217 |
Active tokens processed through the character directive are followed with |
| 2218 |
a \dag, whereas those processed through the macro directive are followed |
| 2219 |
with a \ddag. |
| 2220 |
|
| 2221 |
|
| 2222 |
\begin{exampleA}[lefthand width=7.5cm]{Non-active vs{.} active \macname{def} |
| 2223 |
\& \macname{let}} |
| 2224 |
\Characterdirective{\ifimplicittok |
| 2225 |
\addcytoks{\fbox{\detokenize{#1}:#1}}% |
| 2226 |
\else\addcytoks{#1}\fi\ifactivetok |
| 2227 |
\addcytoks{\rlap{\dag}}\fi\addcytoks{~\,}} |
| 2228 |
\Macrodirective{\ifimplicittok |
| 2229 |
\addcytoks{\fbox{\detokenize{#1}}}% |
| 2230 |
\else\addcytoks{#1}\fi\ifactivetok |
| 2231 |
\addcytoks{\rlap{\ddag}}\fi |
| 2232 |
\ifx\par#1\else\addcytoks{~\,}\fi} |
| 2233 |
|
| 2234 |
\def\A{a} |
| 2235 |
\let\B i |
| 2236 |
\let\C\today |
| 2237 |
\let\D\relax |
| 2238 |
\def\E{\relax} |
| 2239 |
\catcode`V=13 \def V{a} |
| 2240 |
\catcode`W=13 \let Ww |
| 2241 |
\catcode`X=13 \let X\today |
| 2242 |
\catcode`Y=13 \let Y\relax |
| 2243 |
\catcode`Z=13 \def Z{\relax} |
| 2244 |
\tokcyclexpress{\A\B\C\D\E ab\par VWXYZab} |
| 2245 |
\the\cytoks |
| 2246 |
\end{exampleA} |
| 2247 |
|
| 2248 |
|
| 2249 |
\ddbend If the input stream is subject to pre-expansion, one |
| 2250 |
will require \macname{noexpand} for macros where no pre-expansion |
| 2251 |
is desired.\medskip |
| 2252 |
|
| 2253 |
\ddbend If the input stream is provided pre-tokenized via |
| 2254 |
\macname{def}, \TeX{} convention requires cat-6 tokens to appear |
| 2255 |
in the input stream as duplicate, e.g. \texttt{\#\#}. |
| 2256 |
|
| 2257 |
|
| 2258 |
\subsection{Using the \TokCycle{} traps to understand a token's nature} |
| 2259 |
|
| 2260 |
With the use of all the \TokCycle{} traps, one can do a great deal to |
| 2261 |
understand the nature of the more arcane tokens found in an input stream. |
| 2262 |
We already saw in section \ref{s:unicode} how the \macname{ifactivetok} |
| 2263 |
trap can be used to decipher multi-byte Unicode tokens in pdflatex. |
| 2264 |
In the example below, we define various tokens outside of \TokCycle{}, and then |
| 2265 |
pass them, successively, to a token cycle to see what can be discerned about |
| 2266 |
their nature and if the result matches what we know externally about how each |
| 2267 |
token was created. |
| 2268 |
|
| 2269 |
\medskip |
| 2270 |
First, we define the \TokCycle{} macro \macname{identAI} that is used to test |
| 2271 |
the token which is passed as the argument. |
| 2272 |
Traps employed include \macname{ifactivetok}, \macname{ifimplicittok}, |
| 2273 |
\macname{ifactivetokunexpandable}, and \macname{ifcatSIX}. |
| 2274 |
|
| 2275 |
\begin{exampleC}[]{Setting up a token cycle to discern the characteristics of |
| 2276 |
a token} |
| 2277 |
\newcommand\identAI[1]{\begingroup\color{red} |
| 2278 |
\tokcycle |
| 2279 |
{\findAI{Character}{##1}} |
| 2280 |
{\processtoks{##1}} |
| 2281 |
{\findAI{Macro}{##1}} |
| 2282 |
{\findAI{Space}{##1}} |
| 2283 |
{#1}\endgroup\par} |
| 2284 |
\newcommand\findAI[2]{% |
| 2285 |
\ifactivetok |
| 2286 |
\ifimplicittok\showtok{#1}{#2}{active and implicit}% |
| 2287 |
\else\showtok{#1}{#2}{active}\fi |
| 2288 |
\else |
| 2289 |
\ifimplicittok\showtok{#1}{#2}{implicit}\else |
| 2290 |
\showtok{#1}{#2}{conventional}\fi |
| 2291 |
\fi} |
| 2292 |
\newcommand\showtok[3]{ |
| 2293 |
\ifcatSIX |
| 2294 |
#1 \expandafter\string#2 is #3, of catcode-6% |
| 2295 |
\else |
| 2296 |
#1 \string#2 is #3, |
| 2297 |
\ifimplicittok let to ``#2''% |
| 2298 |
\else |
| 2299 |
\ifactivetokunexpandable but unexpandable% |
| 2300 |
\else replaces with ``\expandafter\string#2''\fi |
| 2301 |
\fi% |
| 2302 |
\fi.} |
| 2303 |
\def\lettok#1#2{\let#1= #2\empty} |
| 2304 |
\end{exampleC} |
| 2305 |
|
| 2306 |
Once the above macros are set up, we define the active tilde token (\texttt{\string~}) |
| 2307 |
in a variety of ways and pass it, successsively, to \macname{identAI}. |
| 2308 |
Then, we define the macro \macname{z} in the same variety of ways and perform the |
| 2309 |
tests on successive incarnations of \macname{z}. |
| 2310 |
Finally, we pass a catcode-6 \string# to \macname{identAI} just to see how it |
| 2311 |
reacts. |
| 2312 |
|
| 2313 |
\medskip |
| 2314 |
Note that the \macname{ifactivetokunexpandable} trap only applies to active tokens. |
| 2315 |
Therefore, for non-active tokens that are likewise unexpandable, such as when |
| 2316 |
\macname{z} is let to \macname{relax}, the \macname{identAI} macro instead |
| 2317 |
indicates that the analyzed token (\macname{z}) and its replacement code |
| 2318 |
are identical. |
| 2319 |
This, of course, is another way to say ``unexpandable''. |
| 2320 |
|
| 2321 |
\medskip |
| 2322 |
In the output below, the code is shown at the top, and in the output shown at the |
| 2323 |
bottom, the setup external to \macname{identAI} is shown in black, while the |
| 2324 |
output of the \macname{identAI} token cycle is shown in red. |
| 2325 |
The first word of the \macname{identAI} output, either ``Character'', ``Macro'', |
| 2326 |
or ``Space'', indicates the particular \TokCycle{} directive to which the |
| 2327 |
token was delivered for examination. |
| 2328 |
As we can see, the token cycle, when presented with a wide variety of token types, |
| 2329 |
including combinations of active, implicit, and unexpandable, is able to |
| 2330 |
discern the conditions under which the token was created. |
| 2331 |
|
| 2332 |
|
| 2333 |
\begin{exampleD}[]{Discerning the characteristics of unknown tokens (token cycle |
| 2334 |
output in red)} |
| 2335 |
\small |
| 2336 |
\newcommand\identAI[1]{\begingroup\color{red} |
| 2337 |
\tokcycle |
| 2338 |
{\findAI{Character}{##1}} |
| 2339 |
{\processtoks{##1}} |
| 2340 |
{\findAI{Macro}{##1}} |
| 2341 |
{\findAI{Space}{##1}} |
| 2342 |
{#1}\endgroup\par} |
| 2343 |
\newcommand\findAI[2]{% |
| 2344 |
\ifactivetok |
| 2345 |
\ifimplicittok\showtok{#1}{#2}{active and implicit}% |
| 2346 |
\else\showtok{#1}{#2}{active}\fi |
| 2347 |
\else |
| 2348 |
\ifimplicittok\showtok{#1}{#2}{implicit}\else |
| 2349 |
\showtok{#1}{#2}{conventional}\fi |
| 2350 |
\fi} |
| 2351 |
\newcommand\showtok[3]{ |
| 2352 |
\ifcatSIX |
| 2353 |
#1 \expandafter\string#2 is #3, of catcode-6% |
| 2354 |
\else |
| 2355 |
#1 \string#2 is #3, |
| 2356 |
\ifimplicittok let to ``#2''% |
| 2357 |
\else |
| 2358 |
\ifactivetokunexpandable but unexpandable% |
| 2359 |
\else replaces with ``\expandafter\string#2''\fi |
| 2360 |
\fi% |
| 2361 |
\fi.} |
| 2362 |
\def\lettok#1#2{\let#1= #2\empty} |
| 2363 |
\begin{verbatim} |
| 2364 |
\def\SayFoo{Foo!} |
| 2365 |
\lettok~{Q}\string~ let to Q: \identAI{~} |
| 2366 |
\def~{Q}\string~ def'ed to Q: \identAI{~} |
| 2367 |
\lettok~{\relax}\string~ let to \string\relax: \identAI{~} |
| 2368 |
\def~{\relax}\string~ def'ed to \string\relax: \identAI{~} |
| 2369 |
\lettok~{\SayFoo}\string~ let to \string\SayFoo: \identAI{~} |
| 2370 |
\def~{\SayFoo}\string~ def'ed to \string\SayFoo: \identAI{~} |
| 2371 |
\lettok~{ }\string~ let to space tok: \identAI{~} |
| 2372 |
\def~{ }\string~ def'ed to space tok: \identAI{~} |
| 2373 |
\lettok~{#}\string~\ let to \string#: \identAI{~} |
| 2374 |
\lettok\z{Q}\string\z\ let to Q: \identAI{\z} |
| 2375 |
\def\z{Q}\string\z\ def'ed to Q: \identAI{\z} |
| 2376 |
\lettok\z{\relax}\string\z\ let to \string\relax: \identAI{\z} |
| 2377 |
\def\z{\relax}\string\z\ def'ed to \string\relax: \identAI{\z} |
| 2378 |
\lettok\z{\SayFoo}\string\z\ let to \string\SayFoo: \identAI{\z} |
| 2379 |
\def\z{\SayFoo}\string\z\ def'ed to \string\SayFoo: \identAI{\z} |
| 2380 |
\lettok\z{ }\string\z\ let to space tok: \identAI{\z} |
| 2381 |
\def\z{ }\string\z\ def'ed to space tok: \identAI{\z} |
| 2382 |
\lettok\z{#}\string\z\ let to \string#: \identAI{\z} |
| 2383 |
\string# itself: \identAI{#} |
| 2384 |
\end{verbatim} |
| 2385 |
\vspace{-15pt}\hrulefill |
| 2386 |
|
| 2387 |
\def\SayFoo{Foo!} |
| 2388 |
\lettok~{Q}\string~ let to Q: \identAI{~} |
| 2389 |
\def~{Q}\string~ def'ed to Q: \identAI{~} |
| 2390 |
\lettok~{\relax}\string~ let to \string\relax: \identAI{~} |
| 2391 |
\def~{\relax}\string~ def'ed to \string\relax: \identAI{~} |
| 2392 |
\lettok~{\SayFoo}\string~ let to \string\SayFoo: \identAI{~} |
| 2393 |
\def~{\SayFoo}\string~ def'ed to \string\SayFoo: \identAI{~} |
| 2394 |
\lettok~{ }\string~ let to space tok: \identAI{~} |
| 2395 |
\def~{ }\string~ def'ed to space tok: \identAI{~} |
| 2396 |
\lettok~{#}\string~\ let to \string#: \identAI{~} |
| 2397 |
\lettok\z{Q}\string\z\ let to Q: \identAI{\z} |
| 2398 |
\def\z{Q}\string\z\ def'ed to Q: \identAI{\z} |
| 2399 |
\lettok\z{\relax}\string\z\ let to \string\relax: \identAI{\z} |
| 2400 |
\def\z{\relax}\string\z\ def'ed to \string\relax: \identAI{\z} |
| 2401 |
\lettok\z{\SayFoo}\string\z\ let to \string\SayFoo: \identAI{\z} |
| 2402 |
\def\z{\SayFoo}\string\z\ def'ed to \string\SayFoo: \identAI{\z} |
| 2403 |
\lettok\z{ }\string\z\ let to space tok: \identAI{\z} |
| 2404 |
\def\z{ }\string\z\ def'ed to space tok: \identAI{\z} |
| 2405 |
\lettok\z{#}\string\z\ let to \string#: \identAI{\z} |
| 2406 |
\string# itself: \identAI{#} |
| 2407 |
\end{exampleD} |
| 2408 |
|
| 2409 |
|
| 2410 |
\subsection{Changing grouping tokens (catcodes 1,2)} |
| 2411 |
|
| 2412 |
\begingroup |
| 2413 |
|
| 2414 |
Changing grouping tokens (catcodes 1,2) may require something more, |
| 2415 |
if the output stream is to be detokenized. |
| 2416 |
In the following examples, pay attention to the |
| 2417 |
detokenized grouping around the argument to \macname{fbox}. |
| 2418 |
|
| 2419 |
\medskip |
| 2420 |
As we will see, the issues raised here only affect the situation |
| 2421 |
when detokenization of the output stream is required. |
| 2422 |
\medskip |
| 2423 |
|
| 2424 |
\begin{exampleA}[]{\TokCycle{} defaults grouping tokens to braces:} |
| 2425 |
\tokencycle |
| 2426 |
{\addcytoks{(#1)}} |
| 2427 |
{\processtoks{#1}} |
| 2428 |
{\addcytoks{#1}} |
| 2429 |
{\addcytoks{ }} |
| 2430 |
This \fbox{is a} test. |
| 2431 |
\endtokencycle\medskip |
| 2432 |
|
| 2433 |
\detokenize\expandafter{\the\cytoks} |
| 2434 |
\end{exampleA} |
| 2435 |
|
| 2436 |
One can make brackets cat-1,2, redefining bgroup/egroup to [ ]. |
| 2437 |
However, while one can now use brackets in input stream, braces |
| 2438 |
will still appear in the detokenized \TokCycle{} output stream: |
| 2439 |
|
| 2440 |
\begin{exampleA}[lefthand width=6.5cm] |
| 2441 |
{\TokCycle{} will not automatically change its grouping tokens} |
| 2442 |
\catcode`\[=1 |
| 2443 |
\catcode`\]=2 |
| 2444 |
\let\bgroup[ |
| 2445 |
\let\egroup] |
| 2446 |
\tokencycle |
| 2447 |
{\addcytoks{(#1)}} |
| 2448 |
{\processtoks{#1}} |
| 2449 |
{\addcytoks{#1}} |
| 2450 |
{\addcytoks{ }} |
| 2451 |
This \fbox[is a] test. |
| 2452 |
\endtokencycle\medskip |
| 2453 |
|
| 2454 |
\detokenize\expandafter{\the\cytoks} |
| 2455 |
\end{exampleA} |
| 2456 |
|
| 2457 |
If it is necessary to reflect revised grouping tokens in the |
| 2458 |
output stream, the \macname{settcgrouping} macro is |
| 2459 |
to be used. |
| 2460 |
|
| 2461 |
\begin{exampleA}[lefthand width=6.5cm] |
| 2462 |
{Redefine \TokCycle{} grouping tokens as angle |
| 2463 |
brackets using \macname{settcGrouping}} |
| 2464 |
\catcode`\<=1 |
| 2465 |
\catcode`\>=2 |
| 2466 |
\catcode`\{=12 |
| 2467 |
\catcode`\}=12 |
| 2468 |
\let\bgroup< |
| 2469 |
\let\egroup> |
| 2470 |
\settcGrouping<<#1>> |
| 2471 |
\tokencycle |
| 2472 |
<\addcytoks<(#1)>> |
| 2473 |
<\processtoks<#1>> |
| 2474 |
<\addcytoks<#1>> |
| 2475 |
<\addcytoks< >> |
| 2476 |
This \fbox<is a> test. |
| 2477 |
\endtokencycle\medskip |
| 2478 |
|
| 2479 |
\detokenize\expandafter<\the\cytoks> |
| 2480 |
\end{exampleA} |
| 2481 |
|
| 2482 |
Angle brackets are now seen in the above detokenization. |
| 2483 |
Until subsequently changed, cat-1,2 angle brackets now appear in |
| 2484 |
detokenized \TokCycle{} groups, even if other cat-1,2 tokens were |
| 2485 |
used in the input stream. |
| 2486 |
Bottom line: |
| 2487 |
\begin{itemize} |
| 2488 |
\item adding, deleting, or changing catcode 1,2 explicit |
| 2489 |
grouping tokens, e.g., \{\}, (in conjunction with their |
| 2490 |
associated implicit \macname{bgroup}\macname{egroup}) |
| 2491 |
tokens will not affect \TokCycle's ability to digest proper |
| 2492 |
grouping of the input stream, regardless of which tokens |
| 2493 |
are catcode 1,2 at the moment. |
| 2494 |
|
| 2495 |
\item The grouping tokens used in \TokCycle's output |
| 2496 |
default to \verb|{}| braces (with cat-1,2), but can be |
| 2497 |
changed deliberately using \macname{settcGrouping}. |
| 2498 |
|
| 2499 |
\item The package, currently, has no way to reproduce in the |
| 2500 |
output stream the actual grouping tokens that occur in the |
| 2501 |
input stream, but one should ask, for the particular |
| 2502 |
application, if it really matters, as long as the the |
| 2503 |
proper catcodes-1,2 are preserved? |
| 2504 |
|
| 2505 |
\end{itemize} |
| 2506 |
|
| 2507 |
\endgroup |
| 2508 |
|
| 2509 |
\subsection{Catcode 10 space tokens\label{s:spc}} |
| 2510 |
|
| 2511 |
\begingroup |
| 2512 |
|
| 2513 |
Here we demonstrate that \TokCycle{} can handle arbitrary redesignation |
| 2514 |
of tokens to cat-10, as well as implicit space tokens (both implicit |
| 2515 |
macro spaces and active-implicit character spaces). |
| 2516 |
|
| 2517 |
\ddbend While it should seem natural, we note that implicit |
| 2518 |
space tokens are directed to the Space directive rather than |
| 2519 |
the Character directive. |
| 2520 |
However, \macname{ifimplicittok} may still be used to |
| 2521 |
differentiate an explicit space from an implicit one. |
| 2522 |
|
| 2523 |
|
| 2524 |
\ddbend |
| 2525 |
If the implicit space is \textit{also} an active character, |
| 2526 |
\macname{ifactivetok} is also set, for the user's benefit. |
| 2527 |
Likewise, \macname{ifactivechar} is also checked, to see |
| 2528 |
if the charcode of the active space is still, indeed, active. |
| 2529 |
While \verb|#1| may be used to represent the space in the space directive, |
| 2530 |
a special technique may be required to recover the detokenized name of the |
| 2531 |
active space character. |
| 2532 |
In particular, if the active token is an implicit space, but the charcode |
| 2533 |
of that token is no longer active, \verb|#1| will, for this case, |
| 2534 |
contain a generic implicit space token, \macname{tcsptoken}. |
| 2535 |
However, for all active space tokens, the name of the associated active |
| 2536 |
character in the input stream will be defined as a cat-12 token in |
| 2537 |
\macname{theactivespace}. |
| 2538 |
|
| 2539 |
\medskip |
| 2540 |
Note in the following examples that cat-10 tokens do \textit{not} |
| 2541 |
get under-dots. |
| 2542 |
The next three examples all use the same input, but with different |
| 2543 |
catcode settings for the space and the underscore. |
| 2544 |
|
| 2545 |
\fboxsep=1pt |
| 2546 |
|
| 2547 |
\begin{exampleA}[lefthand width=10cm]{space cat-10{,} underscore cat-12} |
| 2548 |
\catcode`\_=12 % |
| 2549 |
\catcode`\ =10 % |
| 2550 |
|
| 2551 |
\tokencycle{\addcytoks{\underdot{#1}}}% |
| 2552 |
{\processtoks{#1}}% |
| 2553 |
{\addcytoks{#1}}% |
| 2554 |
{\addcytoks{#1}}% |
| 2555 |
\fbox{a_c d} b_g\itshape f\upshape\endtokencycle |
| 2556 |
\end{exampleA} |
| 2557 |
% |
| 2558 |
\begin{exampleA}[lefthand width=10cm]{space cat-10{,} underscore cat-10} |
| 2559 |
\catcode`\_=10 % |
| 2560 |
\catcode`\ =10 % |
| 2561 |
|
| 2562 |
\tokencycle{\addcytoks{\underdot{#1}}}% |
| 2563 |
{\processtoks{#1}}% |
| 2564 |
{\addcytoks{#1}}% |
| 2565 |
{\addcytoks{#1}}% |
| 2566 |
\fbox{a_c d} b_g\itshape f\upshape\endtokencycle |
| 2567 |
\end{exampleA} |
| 2568 |
|
| 2569 |
% |
| 2570 |
|
| 2571 |
\begin{exampleA}[lefthand width=10cm]{space cat-12{,} underscore cat-10} |
| 2572 |
\catcode`\_=10 % |
| 2573 |
\catcode`\ =12 % |
| 2574 |
|
| 2575 |
\tokencycle{\addcytoks{\underdot{#1}}}% |
| 2576 |
{\processtoks{#1}}% |
| 2577 |
{\addcytoks{#1}}% |
| 2578 |
{\addcytoks{#1}}% |
| 2579 |
\fbox{a_c d} b_g\itshape f\upshape\endtokencycle |
| 2580 |
\end{exampleA} |
| 2581 |
|
| 2582 |
The next two examples introduce implicit and active-implicit spaces. |
| 2583 |
In the first example, functionally identical to the prior example, |
| 2584 |
we see that \TokCycle{} can digest and direct such |
| 2585 |
tokens to the Space directive, here in the form of \macname{z}. |
| 2586 |
|
| 2587 |
%%% |
| 2588 |
\resettokcycle |
| 2589 |
\Characterdirective{\addcytoks{\underdot{#1}}} |
| 2590 |
|
| 2591 |
\begin{exampleA}[lefthand width=10cm]{Implicit spaces also work} |
| 2592 |
\resettokcycle |
| 2593 |
\Characterdirective{\addcytoks{\underdot{#1}}} |
| 2594 |
\def\:{\let\z= } \: % |
| 2595 |
\catcode`\_=10 % |
| 2596 |
\catcode`\ =12 % |
| 2597 |
|
| 2598 |
\tokencyclexpress |
| 2599 |
\fbox{a\z{}c d} b_g\itshape f\upshape |
| 2600 |
\endtokencyclexpress |
| 2601 |
\end{exampleA} |
| 2602 |
|
| 2603 |
In the second example, an active implicit space token is created, \texttt{Q}, |
| 2604 |
and stored in a definition, \macname{tmp}. |
| 2605 |
A \macname{string} is applied by the token cycle at one point to the |
| 2606 |
space tokens and we see that the proper implicit tokens are decoded, whether |
| 2607 |
they are active or macro. |
| 2608 |
|
| 2609 |
\medskip |
| 2610 |
The one case deserving special note is the final case of the example, |
| 2611 |
in which the catcode of \texttt{Q}, the active character, is restored |
| 2612 |
to a value of 12, before the previously tokenized, active-implicit |
| 2613 |
\texttt{Q} is passed to the token cycle. |
| 2614 |
In such a case, where future incantations of \texttt{Q} are no longer active, |
| 2615 |
then the active-implicit space token that gets passed to the |
| 2616 |
Space directive (by way of \macname{tmp}) is taken as a generic |
| 2617 |
active-implicit \macname{tcsptoken}, instead of the original |
| 2618 |
active-implicit token (\texttt{Q}). |
| 2619 |
|
| 2620 |
\medskip |
| 2621 |
Nonetheless, the original string of the active-implicit token (that is, a |
| 2622 |
cat-12 \texttt{Q}) is preserved in \macname{theactivespace}. |
| 2623 |
The reason for this behavior is that the absorption of spaces in \TokCycle{} |
| 2624 |
is a destructive process, in which the spaces are absorbed as strings, |
| 2625 |
so that a proper interpretation of their implicitness can be ascertained. |
| 2626 |
With the character no longer active, it would be more difficult to |
| 2627 |
recreate an active-implicit version of \textit{that} token to |
| 2628 |
pass to the Space directive. |
| 2629 |
Thus, a generic active-implicit space token is, instead, employed. |
| 2630 |
|
| 2631 |
|
| 2632 |
\begin{exampleA}[lefthand width=9.05cm]{Active Implicit spaces work{,} too} |
| 2633 |
\resettokcycle |
| 2634 |
\def\:{\let\z= } \: % |
| 2635 |
\catcode`Q=\active % |
| 2636 |
\def\:{\let Q= } \: % |
| 2637 |
\catcode`\_=10 % |
| 2638 |
|
| 2639 |
\def\tmp{xQx x_x\z{}x} |
| 2640 |
\expandafter\tokencyclexpress\tmp\endtokencyclexpress |
| 2641 |
|
| 2642 |
\Spacedirective{\addcytoks{\textcolor{cyan}\bgroup}% |
| 2643 |
\addcytoks[x]{(\string#1\ifactivetok |
| 2644 |
\ifimplicittok\ifactivechar\else\noexpand |
| 2645 |
\textcolor{red}{:\theactivespace}\fi\fi\fi)}% |
| 2646 |
\addcytoks{\egroup}} |
| 2647 |
|
| 2648 |
\medskip |
| 2649 |
\expandafter\tokencyclexpress\tmp\endtokencyclexpress |
| 2650 |
|
| 2651 |
\medskip |
| 2652 |
\catcode`Q=12 |
| 2653 |
\expandafter\tokencyclexpress\tmp\endtokencyclexpress |
| 2654 |
|
| 2655 |
\end{exampleA} |
| 2656 |
\endgroup |
| 2657 |
|
| 2658 |
|
| 2659 |
\subsection{Changes to catcode 0} |
| 2660 |
|
| 2661 |
\begingroup |
| 2662 |
|
| 2663 |
\begin{exampleA}[]{Cat-0 changes are not a hindrance to \TokCycle} |
| 2664 |
\let\littlet=t |
| 2665 |
\catcode`\! 0 !catcode`!\ 12 |
| 2666 |
!Characterdirective{!ifimplicittok |
| 2667 |
!addcytoks{!fbox{#1}}!else!ifcatSIX |
| 2668 |
!addcytoks{!fbox{!fbox{#1}}} |
| 2669 |
!else!addcytoks{#1}!fi!fi} |
| 2670 |
!tokencyclexpress Here, {!scshape!bgroup |
| 2671 |
on !today!itshape{} we are !egroup |
| 2672 |
!littlet es!littlet ing} cat-0 |
| 2673 |
changes{!bgroup}!egroup |
| 2674 |
!endtokencyclexpress!medskip |
| 2675 |
|
| 2676 |
!detokenize!expandafter{!the!cytoks} |
| 2677 |
\end{exampleA} |
| 2678 |
|
| 2679 |
\endgroup |
| 2680 |
|
| 2681 |
|
| 2682 |
\end{document} |
| 2683 |
|