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'?
Asked
Active
Viewed 2,649 times
30
twsh
- 2,718
1 Answers
39
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:

\documentclass{article}
\newcommand*{\foo}{\leavevmode\unskip foo\ignorespaces}
\begin{document}
Here is~\foo some text \foo~with spaces\foo{}and nothing\ \foo\ else.
\end{document}
\leavevmode prevents oddities around the start of a paragraph.
Werner
- 603,163
\leavevmodeat the start or it will have an interesting effect at the start of a paragraph. Also no that neither\unskipnor\ignorespacesworks by expansion so technically doesn't answer the question as asked (but may answer the OP's real problem). – David Carlisle Oct 01 '13 at 17:50bar\quad\foo barwould also produce 'barfoobar'? – Ansa211 Aug 29 '18 at 17:52