TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If I have a \newcommand*{\foo}{foo} it will 'eat' space after it. Is there a way to tell it to eat space before, so that the result of e.g. bar \foo bar would be 'barfoobar'?

share|improve this question
up vote 24 down vote accepted

You can define \foo to \unskip before foo and gobble spaces afterward using \ignorespaces:

\newcommand*{\foo}{\leavevmode\unskip foo\ignorespaces}

The latter is not needed if you're using in-line. Note that forced spaces after \foo are still adhered to:

enter image description here

\newcommand*{\foo}{\leavevmode\unskip foo\ignorespaces}
Here is~\foo some text \foo~with spaces\foo{}and nothing\ \foo\ else.

\leavevmode prevents oddities around the start of a paragraph.

share|improve this answer
+1 although you might want to put \leavevmode at the start or it will have an interesting effect at the start of a paragraph. Also no that neither \unskip nor \ignorespaces works by expansion so technically doesn't answer the question as asked (but may answer the OP's real problem). – David Carlisle Oct 1 '13 at 17:50
Fair point. I don't need it to work by expansion. I've edited the question. – Tom Oct 1 '13 at 17:52

Your Answer


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.