34

To simplify some text I created some commands. One command has no arguments. It uses math mode but is used inside normal text. I would like to remove any trailing space between the command and following text. This will allow me to simply the call such as \cmd sometext instead of {\cmd}sometext. Is there any way to create a negative space?

2 Answers 2

49

If your command is truly a single macro without arguments, i.e. \cmd, then it gobbles spaces written after it anyway. To ensure that any command will absorb following spaces, just make the last thing to appear in it \ignorespaces, which causes TeX to consciously discard space characters until the first non-space appears.

By the way: {\cmd}sometext does not do what you think, if part of the action of \cmd is to make definitions of some kind. Those definitions will be discarded inside the group created by {...} unless you make them global. The way to have two items bump up against each other but be parsed separately is to write \cmd{}sometext. However, as I said, \cmd itself will eat spaces no matter how you define it.

4
  • 2
    I've should have known this. I've been using latex for a few years and it just didn't occur to me that it automatically deleted spaced. (I guess because 99% of the stuff I do is in math mode or has no arugmentless commands)
    – Uiy
    Commented Feb 11, 2012 at 15:22
  • I believe that you ment to say "outside" instead of "inside" in the sentence "Those definitions will be discarded...". Right?
    – gebruiker
    Commented Jun 8, 2017 at 9:43
  • @gebruiker Yes.
    – Ryan Reich
    Commented Jun 8, 2017 at 19:19
  • Thanks for this answer. I found that I had left an extra trailing space inside the brackets of a new command. \newcommand{\fullname}{Tom Anderson } ended up putting a space between my name and the comma when I used \fullname, \date (i.e. it outputted "Tom Anderson , 2017"). I knew that the spaces should be gobbled so checked the command definition to find my error. Commented Aug 17, 2017 at 12:09
9

That is the default behavior as the output of \foo bar shows below (the first line). To introduce negative horizontal space you could use \kern:

enter image description here

\documentclass{article}
\newcommand*{\foo}{foo}%
\newcommand*{\fooWithKern}{foo\kern-1ex}%
\begin{document}
\foo bar

\fooWithKern bar
\end{document}

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.