1

Say I have an expression I often use so I build a command shortcut to typeset it:

\documentclass[11pt]{report}
\usepackage{xparse}
\ProvideDocumentCommand \es{s}{expression shortcut\IfBooleanTF{#1}{s}{}}
\begin{document}
This is a \es and These are \es*.
\end{document}

enter image description here

Can I avoid the manual addition of tilde (~) when the call is made within a sentence? In other words, is there a way to detect (using xparse or not) when Latex shouldn't "eat" the following white space?

nadous
  • 746

1 Answers1

2

In fact, according to The Not so short introduction to Latex (section 1.3.3), Latex ignores whitespaces after commands, so, quoting:

If you want to get a space after a command, you have to put either an empty parameter {} and a blank or a special spacing command after the command name

So either you use empty arguments or something like \hspace{0.5em} (or whatever space suits you better).

Edit:

Following Paul's advice, I tried the \xspace command and the trick is to include \usepackage{xspace} and inside the definition, include the command, like

\ProvideDocumentCommand \es{s}{expression shortcut\IfBooleanTF{#1}{s}{}\xspace}

I just tested it :)

oxio
  • 98
  • 4
    Instead of hard-coding the size of the space, it's better to use control space \ so that TeX's glue is not adversely affected. And as mentioned in my comment above, there is also the option of \xspace. BTW, even though you've been around for a while, this is your first answer, so: Welcome to TeX.SX! :-) – Paul Gessler Mar 25 '15 at 13:26
  • Thanks :) I didn't know the option of \xspace, I think I will have to learn about it – oxio Mar 25 '15 at 15:01