Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
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

2 Answers 2

up vote 5 down vote accepted
\makeatletter
\newcommand\mySpaceOne{\@ifnextchar\mySpaceTwo{\hspace{1em}}{}}
\makeatother

This will consume all space tokens, however.

share|improve this answer
    
I think you might consider adding \let\mySpaceTwo\relax to enable \mySpaceTwo to be standalone command doing nothing. –  tohecz Mar 13 '12 at 17:29
    
Excellent; thank you! –  Zach Mar 14 '12 at 16:17
    
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

\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.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.