| 1 |
% visualtoks.tex, version 1.1b |
| 2 |
|
| 3 |
% Copyright (C) 2025 plante |
| 4 |
% This package is released under the LaTeX Project Public License (LPPL) 1.3c. |
| 5 |
|
| 6 |
% visualtoks: typeset TeXbook-style visualisations of token lists. |
| 7 |
|
| 8 |
% This package may be used in LaTeX by `\usepackage{visualtoks}`, or |
| 9 |
% in plain TeX and other formats by `\input{visualtoks}`. |
| 10 |
|
| 11 |
% See the documentation for typeset examples and caveats. |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
\ifdefined\visualtoks |
| 16 |
\expandafter\endinput |
| 17 |
\fi |
| 18 |
\edef\next{\catcode`\noexpand\@=\the\catcode`\@\space} |
| 19 |
\catcode`\@=11 |
| 20 |
|
| 21 |
\input tokmap |
| 22 |
|
| 23 |
\newdimen\visualtokssep \visualtokssep=1em |
| 24 |
|
| 25 |
\protected\long\def\visualtoks#1{% |
| 26 |
\begingroup |
| 27 |
\leavevmode |
| 28 |
\ifcsname ttfamily\endcsname \ttfamily \else \tt \fi \m@th |
| 29 |
\hskip-\visualtokssep \tokmap\visualtoksA{#1}% |
| 30 |
\endgroup |
| 31 |
} |
| 32 |
\long\def\visualtoksA#1{% |
| 33 |
\hskip\visualtokssep |
| 34 |
\ifx\tokmap@space#1% |
| 35 |
\char`\ $_{10}$% |
| 36 |
\else\ifx\tokmap@bgroup#1% |
| 37 |
\char`\{$_1$% |
| 38 |
\else\ifx\tokmap@egroup#1% |
| 39 |
\char`\}$_2$% |
| 40 |
\else |
| 41 |
\escapechar`\\% |
| 42 |
\edef\tempa{\string#1}% |
| 43 |
\escapechar\m@ne |
| 44 |
\edef\tempb{\string#1}% |
| 45 |
\ifx\tempa\tempb \expandafter\@firstoftwo \else \expandafter\@secondoftwo \fi |
| 46 |
{\ifnum`\ =`#1 \expandafter\@firstoftwo \else \expandafter\@secondoftwo \fi |
| 47 |
{\char`\ }{\string#1}% |
| 48 |
$_{% |
| 49 |
\let~\empty |
| 50 |
\lccode`~=`#1\lowercase{\let~}\empty |
| 51 |
\ifcat$\noexpand#13\fi |
| 52 |
\ifcat &\noexpand#14\fi |
| 53 |
\ifcat##\noexpand#16\fi |
| 54 |
\ifcat ^\noexpand#17\fi |
| 55 |
\ifcat _\noexpand#18\fi |
| 56 |
\ifcat\space\noexpand#110\fi |
| 57 |
\ifcat a\noexpand#111\fi |
| 58 |
\ifcat !\noexpand#112\fi |
| 59 |
\ifcat\noexpand~\noexpand#113\fi |
| 60 |
}$}% |
| 61 |
{\ifx\visualtoks@emptycs\tempb \expandafter\@firstoftwo \else \expandafter\@secondoftwo \fi |
| 62 |
{\visualtoksB{}}% |
| 63 |
{\expandafter\visualtoksB\expanded{{\string#1}}% |
| 64 |
\long\expandafter\def\expandafter\tempa\expandafter##\expandafter1\csname\string#1\endcsname{}% |
| 65 |
\if\relax\detokenize\expandafter\expandafter\expandafter |
| 66 |
{\expandafter\tempa\expandafter#1\csname\string#1\endcsname}\relax *\fi}}% |
| 67 |
\fi\fi\fi |
| 68 |
} |
| 69 |
|
| 70 |
\long\def\visualtoksB#1{% |
| 71 |
\hbox{% |
| 72 |
\vrule |
| 73 |
\vtop{% |
| 74 |
\vbox{% |
| 75 |
\hrule \kern\p@ |
| 76 |
\hbox{\vphantom/\thinspace \tokmap\visualtoksC{#1}\thinspace}% |
| 77 |
}% |
| 78 |
\kern\p@ \hrule |
| 79 |
}% |
| 80 |
\vrule |
| 81 |
}% |
| 82 |
} |
| 83 |
|
| 84 |
\long\def\visualtoksC#1{\ifx\tokmap@space#1\char`\ \else#1\fi} |
| 85 |
|
| 86 |
\begingroup \escapechar=\m@ne |
| 87 |
\xdef\visualtoks@emptycs{\expandafter\string \csname\endcsname} |
| 88 |
\endgroup |
| 89 |
|
| 90 |
\next |
| 91 |
|
| 92 |
\endinput |