I have defined a macro like that: \newcommand{\code}[1]{\texttt{#1}\xspace}

In some situations, I'd like to write the plural of some object and the trailing (plural) 's'-character should not be marked as code. So it should look like that: ClassInstances.

However, when I do \code{ClassInstance}s I get an extra space, i.e. it will look like: ClassInstance s.

What can I do?


Considering @egreg's answer, it seems that I have left out an essential part of the macro. In fact, the definition of \code also modifies the text color. Only then the use of \xspace seems to be necessary at all.

In order to reproduce the issue, run this minimal example:



    Bounds are constructed by closed \code{Curve} objects.
    Bounds are constructed by closed \colcode{Curve} objects.
    Bounds are constructed by closed \colcodex{Curve} objects.

    Sometimes we have even two \colcodex{Curve}s.

From this I get: enter image description here

  • Why using \xspace to begin with?
    – egreg
    Commented May 27, 2022 at 9:03
  • @egreg I started to use \xspace in order to allow a space between a \code macro and the next word, such as in The \code{ClassInstance} is ... which would yield an output without a space between "ClassInstance" and "is" without the \xspace command.
    – Amos Egel
    Commented May 27, 2022 at 9:08
  • If you remove \xspace you'll see that the space is preserved.
    – egreg
    Commented May 27, 2022 at 9:14
  • 1
    Remark on the update, the actual issue is color - How does changing colour affect spacing? - TeX - LaTeX Stack Exchange (although in this case the space after, not before, the \color is removed, so another option is to append \leavevmode... however that's probably what \textcolor already does behind the scene, so no need to reinvent the wheel)
    – user202729
    Commented May 27, 2022 at 15:38

1 Answer 1


The only case, if any, where \xspace might be useful is when commands that take no argument are being defined.

Try with


and you'll see that with

The \code{ClassInstance} is ... 


the space is preserved in the first case and not added in the second one.

To put it differently, \xspace must never be used when defining commands that take arguments.


The \colcode command is wrongly defined: instead, do


which would avoid both the problem of space gobbling and the need to get back to the “previous” color.

  • Thanks for this important information. In fact, my command was slightly different from what I initially posted (I wanted to make it simpler). Please see the edited question.
    – Amos Egel
    Commented May 27, 2022 at 14:06
  • 1
    @AmosEgel \newcommand{\colcode}[1]{\textcolor{blue}{\texttt{#1}}}. See the update
    – egreg
    Commented May 27, 2022 at 14:10

You must log in to answer this question.

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