GeneralSpace after Command

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
Laurentius
Posts: 132
Joined: Wed Feb 11, 2009 11:38 pm

Space after Command

Post by Laurentius »

How does \& prevent the space following it from being swallowed?

Code: Select all

\documentclass{article}

\newcommand\scil{sc.}

\begin{document}

Cats \& dogs.

Pigs \scil cops.

% \show\& says \& = \char"26, but

Cats \char"26 dogs.

\end{document}
Can that be done for other commands as well?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Space after Command

Post by Johannes_B »

The simplest thing would be to add an empty group \&{}.

If this doesn't help you, please provide a minimal working example.


Best regards
Johannes
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Laurentius
Posts: 132
Joined: Wed Feb 11, 2009 11:38 pm

Space after Command

Post by Laurentius »

I already did provide one. \&{} is unnecessary, since \& already has the desired behaviour.

Code: Select all

\documentclass{article}

\def\scil{sc.}

\begin{document}

Cats \& dogs.

Cats \&c.

Fair \scil square. % This should have a space

(Fair \scil) square. % This should still be possible

\end{document}
My problem is that I don't know how to produce this for other commands (without using the package xspace).
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Space after Command

Post by cgnieder »

This is normal TeX behaviour: control sequence names can either consist of arbitrarily many letters (that is characters with category code 11) or of one character with another category code. Since TeX can only determine where a control sequence name of letters ends by the first non-letter you need some marker to tell TeX where the name ends. This can be a space, a opening brace, a punctuation mark... On the other hand control sequences whose name is made of one non-letter can't be longer than the one character so it does not need any marker to tell TeX where it ends. The first sort have one problem: what if you want the control sequence immediately followed by letters? Say, you have the control sequence \foo followed by the letters bar:

Code: Select all

\foobar
This can't work since it would be interpreted as the control sequence \foobar

Code: Select all

\foo{}bar
Not ideal: it would break possible ligatures. Hence the solution as it is: trailing spaces are ignored.

Code: Select all

\foo bar
That's just how it is. The usual way if one does not want that the space is ignored is one of the two following ways:

Code: Select all

\foo\ bar or \foo{} bar
With a non-letter control sequence the problem doesn't exist as

Code: Select all

\&bar
will work nicely.

That being said: there is xspace.

Regards
site moderator & package author
Laurentius
Posts: 132
Joined: Wed Feb 11, 2009 11:38 pm

Re: Space after Command

Post by Laurentius »

Thanks, Clemens! I've been puzzled by this for a long time.

Beste Gruesze,
Lorenz
Post Reply