9

In most cases, by the time output gets to your screen, arguments are fully expanded and my question is moot. But sometimes (verbatim being a common example), one wishes to operate on arguments with different levels of expandedness. My question concerns how to preserve a level of expandedness, or conversely, how to do a partial expansion.

Here's an MWE which demonstrates how the level of expandedness of the argument affects the result:

\documentclass{article}
\usepackage[T1]{fontenc}
\newcommand\display[1]{\expandafter\string#1}
\begin{document}
\display{\large word}\par
\def\x{\large word}
\display{\x}
\end{document}

enter image description here

In the first case, I pass actual text to my macro \display and get the top line. But if I stuff the same text into \x and pass it to the macro \display, it produces a different output, because the the two different invocations supplies the macro with an argument at different levels of expansion.

To the MWE, how does one reconcile the output in either direction (i.e., if the first output was what I actually sought from the second instance and, alternately, if the second output was what I actually sought from the first instance)?

Related to this MWE would be to know if there is something like a \verbatim{\x} which prints out the verbatim of the first level expansion of \x, for the example of the MWE, producing \large word (space included).

More generally, what are the LaTeX techniques for controlling the expansion of an argument, when what your macro gets may be far removed from the original "plain" text.

  • Are we allowed to use e-TeX? If so, you seem to be looking for \detokenize\expandafter{\x} and so on. – Joseph Wright May 23 '13 at 13:43
  • @JosephWright e-TeX is OK to use. – Steven B. Segletes May 23 '13 at 13:56
8

With e-TeX available, the \detokenize primtive does what you want: it turns all of the material into catcode-12 tokens, apart from spaces which are catcode 10. It also inserts spaces after control words. \detokenize has toks-like syntax, so can be used in the form

\def\display#1{\detokenize\expandafter{#1}}

The \detokenize primitive is expandable, so you can for example \edef this.

A non-expandable version without e-TeX can be implemented as

\makeatletter
\newcommand{\display}[1]{%
  \def\@tempa{#1}%
  \expandafter\strip@prefix\meaning\@tempa
}
\makeatother

in LaTeX and with the appropriate definition of \strip@prefix in plain TeX.

(The e-TeX manual describes the action of \detokenize in terms of conceptually saving to a toks and writing this to a file, then reading back with verbatim-like catcodes. The use of \meaning is rather clearer and avoids the file, and in any case both are not expandable.)

8

Perhaps what you are looking for is

\def\display#1{\texttt{\expandafter\strip@prefix\meaning#1}}

which gives the verbatim-ish rendering of the first level expansion of #1 but similarly to your example it doesn't really make sense to pas in more than a single token as #1.

Any later tokens in either your example or this one are just typeset as normal.

But note that the output of \meaning isn't the first level expansion really as it's all catcode 12, so can not be further expanded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.