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.

Weijun Zhou
  • 282
  • 1
  • 11

1 Answers1

5

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.

David Carlisle
  • 757,742
  • I think this one may be useful under some cases. Glad to learn about it. – Weijun Zhou Apr 08 '19 at 10:37
  • 3
    a\scantokens{b}c vs. a\scantokens{b\noexpand}c – Henri Menke Apr 08 '19 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 08 '19 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 08 '19 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 08 '19 at 11:34
  • 1
    By the way: \noexpand generally is useful for temporarily suppressing the \outer-property of macros that are defined in terms of \outer: \outer\def\bar{baz} and \expandafter\def\expandafter\foo\expandafter{\noexpand\bar} versus \outer\def\bar{baz} and \def\foo{\bar}. – Ulrich Diez Apr 08 '19 at 15:53
  • @WeijunZhou I just found your valuable comment which provides the link to How to do scantokens inside edef without triggering runaway definition. I wish to apologize as I should have seen this comment earlier. The information available via that link already covers the aspect of neutralizing the "outerness" of ends of files. Thus my remark about the usage of \noexpand for neutralizing the outerness of ends of files is obsolete. Please pardon me for not noticing this earlier. – Ulrich Diez Apr 08 '19 at 16:08
  • @UlrichDiez your comment was useful though thanks as it alerted me to the fact that Henri's issue was different, I just saw "scantokens and assumed that he was referring to the end of file token:-) – David Carlisle Apr 08 '19 at 16:23