% visualtoks.tex, version 1.1a % Copyright (C) 2025 plante % This package is released under the LaTeX Project Public License (LPPL) 1.3c. % visualtoks: typeset TeXbook-style visualisations of token lists. % This package may be used in LaTeX by `\usepackage{visualtoks}`, or % in plain TeX and other formats by `\input{visualtoks}`. % See the documentation for typeset examples and caveats. \ifdefined\visualtoks \expandafter\endinput \fi \edef\next{\catcode`\noexpand\@=\the\catcode`\@\space} \catcode`\@=11 \input tokmap \long\def\visualtoks@firstoftwo#1#2{#1} \long\def\visualtoks@secondoftwo#1#2{#2} \newdimen\visualtokssep \visualtokssep=1em \protected\long\def\visualtoks#1{% \begingroup \leavevmode \ifcsname ttfamily\endcsname \ttfamily \else \tt \fi \m@th \hskip-\visualtokssep \tokmap\visualtoksA{#1}% \endgroup } \long\def\visualtoksA#1{% \hskip\visualtokssep \ifx\tokmap@space#1% \char`\ $_{10}$% \else\ifx\tokmap@bgroup#1% \char`\{$_1$% \else\ifx\tokmap@egroup#1% \char`\}$_2$% \else \escapechar`\\% \edef\tempa{\string#1}% \escapechar\m@ne \edef\tempb{\string#1}% \ifx\tempa\tempb \expandafter\visualtoks@firstoftwo \else \expandafter\visualtoks@secondoftwo \fi {\ifnum`\ =`#1\char`\ \else\string#1\fi$_{% \let~\empty \lccode`~=`#1\lowercase{\let~}\empty \ifcat$\noexpand#13\fi \ifcat &\noexpand#14\fi \ifcat##\noexpand#16\fi \ifcat ^\noexpand#17\fi \ifcat _\noexpand#18\fi \ifcat\space\noexpand#110\fi \ifcat a\noexpand#111\fi \ifcat !\noexpand#112\fi \ifcat\noexpand~\noexpand#113\fi }$}% {\visualtoksB{\string#1}% \long\expandafter\def\expandafter\tempa\expandafter##\expandafter1\csname\string#1\endcsname{}% \if\relax\detokenize\expandafter\expandafter\expandafter {\expandafter\tempa\expandafter#1\csname\string#1\endcsname}\relax *\fi }% \fi\fi\fi } \long\def\visualtoksB#1{% \hbox{% \vrule \vtop{% \vbox{% \hrule \kern\p@ \hbox{\vphantom/\thinspace#1\thinspace}% }% \kern\p@ \hrule }% \vrule }% } \next \endinput