# show content of token register as a string in the document

I'm trying to implement a command that can execute its argument, but also print it.

To that end I'm dumping the argument into a toks register and use LuaTex to put it back with inactive catcodes; as it says in the manual:

tex.print(<number> n, <string> s, ...) If n is −1, the currently active catcode regime is used. If n is −2, the resulting catcodes are the result of \the\toks: all category codes are 12 (other) except for the space character, that has category code 10 (space)

\newcommand{\showandtell}[1]{\toks0{#1}%
{using the argument #1 executes}\\
{printing with current
\directlua{tex.print(-1, tex.toks[0])} executes}\\
{printing with inert \texttt{
\directlua{tex.print(-2, tex.toks[0])}} shows the code}\\
}
\noindent\showandtell{\bfseries\color{red}}


From "the resulting catcodes are the result of \the\toks" in the manual I was expecting \the\toks0 to have the opposite effect.

How can I get the effect of print(-2 in pure TeX?

The token register appears to be stored as a string, discarding the original catcodes, but then how can packages like verbatim just use just \the\verbatim@line and produce plain output?

You can obtain the same effect in e-TeX (not in Knuth TeX) with \detokenize, which is available when LaTeX is used (on TeX distributions not more than ten years old).

\documentclass{article}
\usepackage{color}

\newcommand{\showandtell}[1]{%
{using the argument #1 executes}\\%
printing with detokenize \texttt{\detokenize{#1}} shows the code%
}

\begin{document}

\noindent
\showandtell{\bfseries\color{red}}

\end{document}


If you want to use a token register, then \detokenize\expandafter{\the\toks0} will do.

Verbatim modes attain their purpose by changing category codes before starting to read the material, which is why they can't be used in the argument to a command. Note, however, that \detokenize, like the LuaTeX code you used, adds a space after control words (which verbatim modes don't).

• by "plain tex" I just meant "not using Lua" here, so that works for me. I was confused by the apparent use of character strings for the register, but "token registers accept and return Lua strings. Lua strings are converted to and from token lists" (so I guess the original catcode will be part of the token which is invisible in the string) – pascal Mar 18 '15 at 0:52

It is also possible to achieve this in Knuth TeX via \meaning. Although there is no reason not to use \detokenize if it is available.

\documentclass{article}
\usepackage{color}

\makeatletter
\let\StripPrefix\strip@prefix % Latex kernel command
\makeatother

\newcommand{\showandtell}[1]{%
{using the argument #1 executes}\\%
\def\TEMP {#1}%
playing tricks with meaning \texttt{\expandafter\StripPrefix\meaning\TEMP} shows the code%
}

\begin{document}

\noindent
\showandtell{\bfseries\color{red}}

\end{document}


and if you want to show things from \toks0 register you can do

\expandafter\def\expandafter\TEMP\expandafter{\the\toks0}


or simpler

\edef\TEMP{\the\toks0}


and then the \meaning thing.