9

In the TeXbook, there is a macro called \ignorespaces:

\ignorespaces ⟨optional spaces⟩. TeX reads (and expands) tokens, doing nothing until reaching one that is not a ⟨space token⟩.

And there is another macro called \relax for which TeX does nothing.

Is \ignorespaces redundant since it seems that \relax can achieve \ignorespaces's functionality? See the following example:

a\ignorespaces b\ignorespaces
    c\ignorespaces   \hskip1em d

a\relax b\relax c\relax \hskip1em d

The effect is the same.

Stephen
  • 3,826

3 Answers3

11

The primitive \ignorespaces gobbles the following space tokens (explicit or implicit) and performs macro expansion. It won't ignore glue specification, so the \hskip1em will remain.

Your example is misleading, because in

a\ignorespaces b

there's no space to be ignored. Nor there is a space in

a\relax b

Try

a\ignorespaces\space b

a\relax\space b

instead and you'll see the difference.

enter image description here

egreg
  • 1,121,712
10

\ignorespaces is not a macro it is an unexpandable primitive (like \relax)

\relax does nothing when executed, \ignorespaces causes following space tokens to be ignored.

Your example did not show a difference as there are no space tokens after \relax the space character in the input is used to terminate the command name then discarded without being tokenized.

enter image description here


a\relax\space b

a\ignorespaces\space b

\uppercase{x\relax} y

\uppercase{x\ignorespaces} y

\bye

David Carlisle
  • 757,742
7

They work differently in definitions:

\documentclass{article}

\begin{document}

\newcommand\testa[1]{test #1\ignorespaces} \testa{a} b

\newcommand\testb[1]{test #1\relax} \testb{a} b \end{document}

enter image description here

Ulrike Fischer
  • 327,261