I would like to create a two-part spacing command. Specifically, I'd like two commands: \mySpaceOne and \mySpaceTwo. If \mySpaceOne is followed by a \mySpaceTwo, then it generates a certain amount of space; otherwise, it does nothing and does not consume the next token.

Is this possible? How would one begin to create such a command?

  • Welcome to TeX.SE. It is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. This will also serve as a test case and ensure that the solution actually works for you. – Peter Grill Mar 13 '12 at 14:46
  • This is doable, but depends on what you mean by "followed by", and what is meant by not "followed by". So, the test case should cover these cases. – Peter Grill Mar 13 '12 at 14:48
  • Understood; thanks for the heads up. My lack of an MWE reflected my uncertainty as to exactly what it was I wanted here. Not knowing TeX as well as I would like, I didn't want to limit my possibilities by requesting something too specific. :) – Zach Mar 14 '12 at 14:55

This will consume all space tokens, however.

  • I think you might consider adding \let\mySpaceTwo\relax to enable \mySpaceTwo to be standalone command doing nothing. – yo' Mar 13 '12 at 17:29
  • This is simple and works just as I asked. At this point, though, I'm not sure why it doesn't seem to work in a command. I created \newcommand{\hello}{\mySpaceTwo hello \mySpaceOne} and then tried \hello\hello, but there is no space between them; this is probably because the first \mySpaceOne sees the \hello coming up (since it hasn't expanded yet). Defining \newcommand{\hello}{\mySpaceTwo hello \expandafter\mySpaceOne} also didn't help. Any suggestions? – Zach Mar 14 '12 at 16:38
  • With the \expandafter it works and it obviously can't work with the first definition: TeX sees \hello, which is not \mySpaceTwo. You're probably not using the right approach, but it's difficult to say more without knowing what you're trying to do. – egreg Mar 14 '12 at 16:44

Similar answer to egreg but using the TeX primitive \futurelet


     \let\choice = \choiceone 
    \let\choice = \choicetwo

A \futurelet takes normally three tokens:


TeX executes a let\tokena=\tokenc generating a copy of \tokenc stored under the name of \tokena.

It then removes \tokena from the main token list. Following TeX expands \tokenb.

This token is for all practical purposes a macro with the following properties:

  1. The macro will use \tokena, which is a copy of \tokenc, to find out what \tokenc is, in other words what token is to be expected later.
  2. It then causes another macro to be expanded which will ultimately absorb \tokenc.

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.