Using \let to remove a space

On the {TeX} Q&A site, there was a question recently about splitting the first token off a list, with the requirement that spaces are not skipped. In my answer, I’ve used \let to remove one space. The question is how to do this. Normally, if you want to use \let you do

\let\TokenA\TokenB

In this case, TeX will skip spaces after \let and \TokenA, so we can’t use it to \let to a space. However, what we can do is notice that TeX allows us to have an optional = followed by one space in the syntax for \let. We also need to make sure that TeX does not discard two spaces in the early stage of parsing, so can use \@firstonone:

\@firstofone{\let\TokenA= }

This will \let \TokenA to the next token in the input, even if it is a space. I’ve used this to remove the next token from some input in combination with \afterassignment:

\long\def\firstofone#1{#1}
\def\GobbleExactlyOne{%
  \afterassignment\NextThing
  \firstofone{\let\TokenA= }%
}

Not something you need every day, but worth knowing about I think.

4 thoughts on “Using \let to remove a space

  1. I’ve made the definition of firstofone general, and therefore long. This is what the LaTeX kernel does for @firstofone, for example. For internal macros, you never know where par tokens might pop up.

  2. TeX allows one optional space after the ‘=’. So with

    @firstofone{letTokenA=}%
    

    if the next token is a space then TeX treats it as the optional one for the let syntax. On the other hand, with

    @firstofone{letTokenA= }%
    

    the optional space is included already. So if there is another space then it will be the token that let is looking for. We need the @firstofone so that TeX does not read ‘one or more’ spaces as a single space: the one inside the @firstofone is distinct from any subsequent ones.

Leave a Reply