4

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?

3
  • 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. Mar 13, 2012 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. Mar 13, 2012 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, 2012 at 14:55

2 Answers 2

5
\makeatletter
\newcommand\mySpaceOne{\@ifnextchar\mySpaceTwo{\hspace{1em}}{}}
\makeatother

This will consume all space tokens, however.

3
  • I think you might consider adding \let\mySpaceTwo\relax to enable \mySpaceTwo to be standalone command doing nothing.
    – yo'
    Mar 13, 2012 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, 2012 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, 2012 at 16:44
4

Similar answer to egreg but using the TeX primitive \futurelet

\documentclass{article}
\begin{document}
\def\myspacetwo{}
\def\myspaceone{%
 \futurelet\ifoptions
    \choosemacro
}

\def\choiceone{\rule{20pt}{20pt}}
\def\choicetwo{}
\def\choosemacro{%
 \ifx\ifoptions\myspacetwo%
     \let\choice = \choiceone 
 \else
    \let\choice = \choicetwo
 \fi
\choice
}
\myspaceone\myspacetwo 
\myspaceone
\end{document}

A \futurelet takes normally three tokens:

     \futurelet\tokena\tokenb\tokenc

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .