0

After reading (and searching through) TeXbyTopic.pdf I have the impression that practical use of \noexpand falls into one of the following three cases,

  1. Inside \edef or \xdef, including the cases where macros are defined so that they are used in the context of \edef, such as

    \def\protect{\noexpand\protect\noexpand}

  2. Inside \write and \message, including the case of movable texts in LaTeX

  3. In the special construction \if\noexpand#1\relax

Are there any other practical uses of \noexpand? Are there any practical use of \noexpand that is not in the expansion-only context, except for #3 listed above? I am new to plain and hope to understand plain better (especailly, the expansion-only context) by understanding various usages of \noexpand.

4

There are lots of tex primitives that expand tokens looking for arguments like \if and \ifcat. \noexpand would have a similar effect with any of them but whether or not that is useful is harder to say.

for example

\def\zz{hello}

\input s\noexpand\zz

\bye

inputs the file s.tex and then typesets hello whereas

\def\zz{hello}

\input s\zz

\bye

produces

! I can't find file `shello'.

But you could more easily have used a space after s than \noexpand to stop the scan for a filename.

As indicated by Henri in comments there is also the use to nullify the end of file token in input or \scantokens

\edef\zz{\scantokens{z}}

is the error

! File ended while scanning definition of \zz.

but

\edef\zz{\scantokens{z\noexpand}}

or

\everyeof{\noexpand}
\edef\zz{\scantokens{z}}

are error free.

  • I think this one may be useful under some cases. Glad to learn about it. – Weijun Zhou Apr 8 at 10:37
  • 3
    a\scantokens{b}c vs. a\scantokens{b\noexpand}c – Henri Menke Apr 8 at 10:43
  • 1
    Related: How to do scantokens inside edef without triggering runaway definition. The link can be added to the answer if you wish. – Weijun Zhou Apr 8 at 11:15
  • You can use \noexpand for tricking TeX's reading apparatus into state S when having things re-tokenized by \scantokens and thus prevent the coming into being of a trailing space token. This is what is shown in the comment of Henri Menke. You can also use \noexpand for neutralizing the circumstance that TeX takes ends of files for something that is similar to outer tokens. You might be interested in the discussion How does TeX handle EOF which took place in the usenet-newsgroup comp.text.tex in March 2008. – Ulrich Diez Apr 8 at 11:29
  • @HenriMenke You can use \noexpand here. But you can as well use \relax: a\scantokens{b}c vs. a\scantokens{b\relax}c. Just something that puts TeX's reading apparatus into stats S (skipping blanks) so that the endline-character which gets inserted when \scantokens' re-tokenization-part is carried out won't get tokenized as a space-token. ;-) With \input/\@@input and \scantokens and the like you can use \noexpand for neutralizing the circumstance that TeX takes ends of files for something that is similar to an \outer-token. – Ulrich Diez Apr 8 at 11:34

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.