[Tex/LaTex] Always safe to use empty braces `{}` to guard space

spacing

I'm generating LaTeX code and, as such, am faced with the difficulty to preserve spaces between inserted LaTeX commands and ensuing text. They way I'm doing it now is as suggested by the answer to a similar question, i.e. by always placing a pair of empty braces {} after the command.

For example, assuming that there is a definition \newcommand{\arnold}{Arnold Schwarzenegger}, a source text xxx $foo is a xxx, and $foo stands in for \arnold, I'm translating the source to xxx \arnold{} is a xxx.

I'm aware of the fact that I could use xxx {\arnold} is a xxx which should be much safer (syntactically speaking)—but that would make it impossible to insert a begin-group here and an end-group there, which is an essential must-have for me.

Also, the xspace package is not a solution since I do not control what commands users would want to insert into their scripts, and I'm not even going to think about re-defining/wrapping existing commands—given the mad complexity of TeX's inner workings (which is often compared to digestion or the outcome of it), such an approach is best left for parallel worlds with unlimited resources to attempt. (What's more, Dave Carlisle himself recommends not to use xspace, so thanks, but no, thanks.)

This works to preserve the space (or lack thereof) in this particular case; however, I'm worried that some funky (La)TeX command might happen to be defined so that \foo x and \foo{} x (or \bar{argument} x and \bar{argument}{} x) turn out to produce different things.

In case that should turn out to be impossible or not so by chance, I'm fine to leave it at that; but if there are such critical commands, what alternative device could I use?

Edit Thanks to the info provided by One Well-Known TeXpert, below, I now know that the first option is no more: I want users to be able to insert arbitrary TeX fragments into their scripts, and those should always work as expected provided the entirety of the generated TeX makes sense. You can define macros so that absence vs. presence of ensuing {} does make a difference, so someone will have done that already; therefore, transparently inserting {} might cause unexpected things to happen.

IOW, I'm in need of an alternative solution—preferrably one that works in math mode as well, but I could live with treating math mode specially, because, well, because math mode has always been like a whole other thing in TeX anyways, and I can put suitable restrictions and content-checks in place to take care of that situation.

Best Answer

If you know \foo does not take arguments and is not in math mode then

\foo{}

is safe enough.

You also mentioned the case where the command does have arguments

\bar{argument}{}

But there the {} can not possibly do anything useful as spaces would not be dropped after {argument} anyway, so best not to add them in that case.

It is easy to define \foo in a way that this does affect things

\def\foo{\@ifnextchar\bgroup{yes}{no}}

for example will make \foo a say no and \foo{} a say yes. But if you are doing that, you presumably intend that to happen.

An empty brace group {} in math mode can affect spacing, compare ${}-1$ and $-1$ so it is best not to add {} in math mode, but as spaces are ignored in math mode, the issue of spaces after command names does not apply anyway.


one possibly safer alternative is to use

\IeC{\foo}

which is the commad inputenc uses for this purpose.

aaaa    \IeC{\foo}  bbbb

will work as will

aaa \IeC{\bar}{argument} aaa

\IeC is more or less \def\IeC#1{#1} apart from some checing about \protect (which you could also use)

If you haven't loaded inputenc it is:

\def\IeC{%
  \ifx\protect\@typeset@protect
    \expandafter\@firstofone
  \else
    \noexpand\IeC
  \fi
}
Related Question