Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

\documentclass[11pt,reqno,fleqn]{amsart}
\newcommand{\myPhrase}[1][\ ]{This is the dog{#1}}
\begin{document}
\myPhrase that I was telling you about.  \myPhrase{,} which is a collie.
\end{document}

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

\myPhrase{,}

returns

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

share|improve this question
4  
Look at xspace package. Also see the TeX-FAQ: tex.ac.uk/cgi-bin/texfaq2html?label=xspace –  Aditya Jun 17 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. –  Ian Thompson Jun 17 at 20:42

1 Answer 1

up vote 4 down vote accepted

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:

\documentclass[11pt,reqno,fleqn]{amsart}
\newcommand{\myPhrase}[1][\ ]{This is the dog{#1}}
\begin{document}
\myPhrase that I was telling you about.  \myPhrase[,] which is a collie.
\end{document}

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.

share|improve this answer
    
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 Jun 18 at 22:43

Your Answer

 
discard

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.