Space after LaTeX commands

If you create a macro without arguments you should always invoke it with an empty statement after it: \arnold{}

The reason behind this is that LaTeX ignores (typed) spaces directly after the macro (they just stop the scanning for the macro’s name). You need to break that using either a protected space (as you already wrote) or an empty statement {}. I'd recommend using an empty statement, as using a protected space can generate nasty effects — for example, if that protected space is directly followed by a line break. In that case LaTeX might print two spaces instead which looks ugly and isn't wanted. Using an empty statement prevents this.

How you can add the space directly to the macro has already been answered, but do you really want this? You'd get into trouble as soon as the macro is to be followed by a punctuation mark (or if the macro is followed by a \footnote, etc.):

\arnold,
Arnold Schwarzenegger ,

I'd recommend going for the empty statement option -- one gets used to that quite fast.


Yes, look at the xspace package.

\usepackage{xspace}

And later on...

\newcommand{\arnold}{Arnold Schwarzenegger\xspace}

*This is a summary answer, highlighting the benefits and drawbacks for the different techniques available to preserve spaces following a control word. Conceptually, this is intended for LaTeX implementations where the control word does not accept arguments, expands to simple, possibly formatted, text, and is used primarily in prose.

Background:

The TeX input processor, after building a control sequence consisting of characters with category code = 11 (a control word), switches to state S, which skips characters with category code = 10 (space). Therefore, to preserve spaces following a control word, a character with category code < 10 or category code > 11 must be used between the control word and the following space. This the essence of the first five options below. The sixth option works after the input processor and adds a space depending on the following token.

Evaluation Criteria:

The question defines a single basis for comparison:

  1. Minimize the number of additional characters beyond the baseline control word (\arnold as an example).

Brevity should not be the only factor considered. Qualitative characteristics are also important. For example, does the approach:

  1. Allow for expected trailing spaces to be removed by mistake? In other words, are there valid variants of the control word which would remove an explicit space?
  2. Potentially add unexpected trailing spaces after the control word?
  3. Raise an error if the control word is already defined?
  4. Apply to existing control words (e.g., \LaTeX)?
  5. Enforce consistent use of the control word in the prose? As with (2), are there valid variants of the control word? The desire for consistent use stems from the otherwise reasonable interpretation of a control word as being a direct substitute for the underlying text.
  6. Create unintended consequences/conflicts with other aspects of LaTeX?

As the weight of each point above is subjective and there is no perfect solution, the best approach is largely a matter of personal preference. Factor (1) is listed in the heading of the options while the remaining factors are assessed in Benefits and Drawbacks. Each of the options below have implementations (those "With Check") to satisfy (4) and it is therefore omitted.


Option 1: Add a control space (\␣ = backslash + space) after the control word when a space is desired. Impact: 0 or 1 additional characters.

The "explicit" solution given on page 8 of The TEXbook. This yields a control sequence token (different from the character token which is generated by a space alone) and is therefore retained.

Definition: \newcommand{\arnold}{Arnold Schwarzenegger}

Example uses:

  1. Space after: He yelled ``Get to the choppa!'' in his best \arnold\ impression.
  2. Punctuation after: According to \arnold, it is pronounced Kal-e-four-knee-yah.

Benefits:

  • (3) Does not add unexpected trailing spaces.
  • (5) Applicable to preexisting control words.

Drawbacks:

  • (2) If omitted, trailing spaces will be removed (i.e., best \arnold impression above yields best Arnold Schwarzeneggerimpression).
  • (6) Not consistent usage (i.e., \arnold\ , above yields Arnold Schwarzenegger ,).

Option 2: Add {} after the control word when a space is desired or after each use.

Implementation A: Impact: 0 or 2 additional characters.

As suggested in bluebrother's answer, the { character following the control word in this approach causes the input processor to switch to state M and retain the first following space.

Definition: \newcommand{\arnold}{Arnold Schwarzenegger}

Uses:

  1. Space after: He yelled ``Get to the choppa!'' in his best \arnold{} impression.
  2. Punctuation after:
    • According to \arnold, it is pronounced Kal-e-four-knee-yah.
    • According to \arnold{}, it is pronounced Kal-e-four-knee-yah.

Benefits:

  • (3) Does not add unexpected trailing spaces.
  • (5) Applicable to preexisting control words.

Drawbacks:

  • (2) If omitted, trailing spaces will be removed (i.e., best \arnold impression above yields best Arnold Schwarzeneggerimpression).
  • (6) Not consistent usage (inclusion or omission of {} are both acceptable if followed by punctuation).
  • (7) Unintended consequences/conflicts:

    • Prevents kerning and ligatures with subsequent text if used as part of a word/acronym (see Ulrich Diez's answer).

Implementation B: Impact: 2 additional characters.

Alternative approaches which raise errors if {} does not follow the control word may be found at Drawbacks of csname method to avoid spaces after command.

Definition: Varies

Uses:

  • Space after: He yelled ``Get to the choppa!'' in his best \arnold{} impression.
  • Punctuation after: According to \arnold{}, it is pronounced Kal-e-four-knee-yah.

Benefits:

  • (2) Prevents expected trailing spaces from being removed (error raised if {} is omitted).
  • (3) Does not add unexpected trailing spaces.
  • (6) Use is mostly consistent (error raised if {} is omitted; however, {\relax} will compile).

Drawbacks:

  • (5) Not applicable to existing control words.
  • (7) Unintended consequences/conflicts:

    • Some implementations may prevent kerning and ligatures with subsequent text if used as part of a word/acronym (see Ulrich Diez's answer).

Option 3: Surround the control word in { } when a space is desired or for each use. Impact: 0 or 2 additional characters.

Suggested by Trylks in a comment to bluebrother's answer.

Definition: \newcommand{\arnold}{Arnold Schwarzenegger}

Uses:

  1. Space after: He yelled ``Get to the choppa!'' using his best {\arnold} impression.
  2. Punctuation after:
    • According to \arnold, it is pronounced Kal-e-four-knee-yah.
    • According to {\arnold}, it is pronounced Kal-e-four-knee-yah.

Benefits:

  • (3) Does not add unexpected trailing spaces.
  • (5) Applicable to preexisting control words.

Drawbacks:

  • (2) If omitted, trailing spaces will be removed (i.e., best \arnold impression above yields best Arnold Schwarzeneggerimpression).
  • (6) Not consistent usage (inclusion or omission of { } are both acceptable if followed by punctuation).
  • (7) Unintended consequences/conflicts:

    • Places the command in a group, thereby limiting the non-global effects of that command to that scope (as noted by gernot in the comments below).
    • Prevents kerning and ligatures with subsequent text if used as part of a word/acronym (see Ulrich Diez's answer).

Option 4: Add a non-letter character to the end of the control word in its definition and after each use. Impact: 1 additional character.

A variant on Przemysław Scherwentke's answer (see also lockstep's answer and yo's answer), this is perhaps the "canonical" solution (Page 204 of The TEXbook):

You can use this idea to define macros that are intended to be used in sentences, so that users don't have to worry about the possible disappearance of spaces.

By defining the control word with \def and placing a non-letter character at the beginning of the parameter text, the character will be required. As above, the definition may be wrapped in \@ifdefinable to perform the same check as \newcommand and raise an error if the control word is already defined.

Definition:

Without check: \def\arnold/{Arnold Schwarzenegger}

With check (see the Appendix at the end of this answer for an alternative implementation):

\makeatletter
    \@ifdefinable{\arnold}{\def\arnold/{Arnold Schwarzenegger}}
\makeatother

Uses:

  1. Space after: He yelled ``Get to the choppa!'' using his best \arnold/ impression.
  2. Punctuation after: According to \arnold/, it is pronounced Kal-e-four-knee-yah.

Benefits:

  • (2) Prevents expected trailing spaces from being removed (error raised if / is omitted).
  • (3) Does not add unexpected trailing spaces.
  • (6) Use is consistent (error raised if / is omitted).

Drawbacks:

  • (5) Not applicable to existing control words. Added: The Appendix at the end of this answer provides a means of redefining individual existing control words to follow this convention.

Option 5: Add # at the end of the parameter text in the definition to require { to immediately follow the control word. Impact: 2 or ? additional characters.

By defining the control word with \def and placing # at the end of the parameter text, the control word will have to be followed by an opening brace (page 204 of The TEXbook). A closing brace will also be required to close the group (see gernot's comments below), though, at the time of writing, not all editors correctly parse the warning resulting from its omission. The definition may be wrapped in \@ifdefinable to perform the same check as \newcommand and raise an error if the control word is already defined.

Definition:

Without check: \def\arnold#{Arnold Schwarzenegger}

With check (see the Appendix at the end of this answer for an alternative implementation):

\makeatletter
    \@ifdefinable{\arnold}{\def\arnold#{Arnold Schwarzenegger}}
\makeatother

Uses:

  • Space after:

    • He yelled ``Get to the choppa!'' in his best \arnold{} impression.
    • He yelled ``Get to the choppa!'' in his best \arnold{ }impression.
  • Punctuation after:

    • According to \arnold{}, it is pronounced Kal-e-four-knee-yah.

Benefits:

  • (2) Prevents expected trailing spaces from being removed (error/warning raised if {, {}, or {<anything>} is omitted).
  • (3) Does not add unexpected trailing spaces.

Drawbacks:

  • (5) Not applicable to existing control words. Added: The Appendix at the end of this answer provides a means of redefining individual existing control words to follow this convention.
  • (6) Use is not consistent. The control word may be followed by {} or {<anything>}.

Option 6: Use the xspace package and add \xspace at the end of the definition. Impact: 0* additional characters

This is Uri's answer. The xspace package acts after the input processor and adds a space depending on the token following the control word.

Definition: \newcommand{\arnold}{Arnold Schwarzenegger\xspace}

Uses:

  1. Space after:
    • He yelled ``Get to the choppa!'' in his best \arnold impression.
    • He yelled ``Get to the choppa!'' in his best \arnold{} impression.
    • He yelled ``Get to the choppa!'' in his best \arnold\ impression.
  2. Punctuation after:
    • According to \arnold, it is pronounced Kal-e-four-knee-yah.
    • According to \arnold{}, it is pronounced Kal-e-four-knee-yah.

Benefits:

  • (2) Prevents expected trailing spaces from being removed.

Drawbacks:

  • (3) May add unexpected trailing spaces.
  • (6) Use may not* be consistent.
  • (5) Not applicable to existing control words.

*Ideally, this approach would not require any additional characters any time the control word is used. However, in situations where it fails, a {} is necessary.


Appendix

An interface for adding a required delimiter to an existing parameterless macro producing text only.

\documentclass{article}

\makeatletter
\newcommand\requiredelimiter[2][########]{%
  \ifdefined#2%
    \def\@temp{\def#2#1}%
    \expandafter\@temp\expandafter{#2}%
  \else
    \@latex@error{\noexpand#2undefined}\@ehc
  \fi
}
\@onlypreamble\requiredelimiter
\makeatother

\requiredelimiter{\LaTeX}

\newcommand\arnold{Arnold Schwarzenegger}
\requiredelimiter[/]{\arnold}

\requiredelimiter{\foo} % <---- ERROR

\begin{document}

\LaTeX{} is nice

\arnold/ is strong

\end{document}

Without an optional argument, the (redefined) macro will require a pair of braces after it, like \LaTeX in the example. With the optional argument the specified token(s) will be required instead, like for \arnold in the example.

Tags:

Macros

Spacing