I'm trying to define a macro replaces a phrase by a single latex expression, e.g., I want \myPhrase to return the phrase "this is the dog". The trouble is that when you do this in the obvious way

\def\myPhrase{this is the dog}

then you always have to remember to add a space after the macro, i.e.,

\myPhrase that I like

will return

this is the dogthat I like.

Since my coauthor will never remember to always write

\myPhrase\ that I like 

I tried to define a macro with an optional argument. The default would be a space, and if you wanted punctuation you would add it as an argument.

For example

\newcommand{\myPhrase}[1][\ ]{This is the dog{#1}}
\myPhrase that I was telling you about.  \myPhrase{,} which is a collie.

In principle the above should do the trick but it adds space even when there's an optional argument, i.e.,



This is my dog , which is a collie

when it should return

This is my dog, which is a collie 

Any advice would be very much appreciated! Thanks Leo

  • 4
    Look at xspace package. Also see the TeX-FAQ: tex.ac.uk/cgi-bin/texfaq2html?label=xspace
    – Aditya
    Commented Jun 17, 2014 at 20:33
  • What if your coauthor needs punctuation, but forgets about the optional argument? S(he) might type '\myPhrase, which is dead.' Solving one potential problem appears to have created another. Commented Jun 17, 2014 at 20:42

1 Answer 1


Your macro is defined just fine, but you invoked it improperly with braces {} instead of brackets []. Optional arguments are passed with the latter. So simply changing your MWE to replace the invocation braces with brackets will give you the expected result:

\newcommand{\myPhrase}[1][\ ]{This is the dog{#1}}
\myPhrase that I was telling you about.  \myPhrase[,] which is a collie.

enter image description here

As Aditya mentioned, the xspace package is also an option, but that, too, comes with "buyer beware" warnings: Drawbacks of xspace

I would add finally, that the standard way that most people deal with this issue is to not have any argument associated with \myphrase, but to use it in the following way:

\myphrase{} that I was telling you about. \myphrase, which is a collie.

Comparing the two approaches shows that your method only saves typing if the \myphrase is followed by a blank more than half of the time. I'm not sure that is necessarily a good assumption.

  • Thanks very much, Steven. How silly of me. In the context of what I am doing---actually specifying a particular technical word, which I wanted to be the same everywhere, but I haven't decided yet precisly which word to use yet---it's a good assumption!
    – Leo Simon
    Commented Jun 18, 2014 at 22:43

You must log in to answer this question.

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