# Redefining Existing Latex Commands To Automatically Preserve Spaces

## The Problem: Lost Spaces in Commands

When using commands in LaTeX, users often encounter the issue of spaces being lost around commands. For example, if a user types "some text \command more text", the spaces around \command will be ignored in the final output. This causes words to run together, reducing readability.

The root of this problem lies in how LaTeX handles commands. By default, LaTeX treats all spaces around commands as irrelevant. This allows flexibility in command usage, but leads to the space loss described.

## How LaTeX Handles Spaces

To understand why spaces get lost around commands, we must explore how LaTeX handles whitespace and commands internally:

- Whitespace characters like spaces and newlines are treated as blanks by LaTeX.
- Blanks at the start and end of a line are ignored.
- Blanks inside the text are converted to spaces in the output.
- LaTeX treats any sequence of commands and blanks as a single unit called a
*box*. - When a box reaches the output, preceding and trailing blanks are ignored per the rules above.

Thus, spaces surrounding a command form blanks at the edges of the command's box. LaTeX discards these blanks, leading to space loss around commands.

## Defining Robust Commands

LaTeX provides a robust command option to preserve spaces around commands. Making a command robust tells LaTeX to treat it as text rather than as a standalone box.

Robust commands act like this:

- Spaces after a robust command are converted to output spaces.
- Spaces before a robust command form an inter-word space in the output.
- Line breaks still delimit paragraphs as usual.

In essence, robust commands get embedded within text lines rather than being isolated. This prevents the space loss issue.

## Example Robust Command Definition

Here is an example of defining a robust command `\newcommand`

using the `\DeclareRobustCommand`

function:

\DeclareRobustCommand{\keywords}[1]{Keywords:#1}

This defines a command \keywords that takes one argument and displays it in bold after the "Keywords:" text. Making it robust preserves spaces around \keywords calls:

Here are some \keywords{key words} related to this topic.

Without robustness, the spaces would be lost around \keywords. But with robustness enabled, the command embeds nicely into the text flow.

## Preserving Spaces in Math Mode

When working in math mode, LaTeX enters a special state ignoring most spaces. So robust commands won't preserve spaces within math constructs like equations.

To make a command robust in math mode, LaTeX provides the `\mathchardef`

macro. For example:

\mathchardef\Abs=`|

This defines a robust absolute value command \Abs usable in math mode. Any spaces around \Abs will now be preserved inside math formulas.

## When Not to Make Commands Robust

While robustness fixes space loss, it can also introduce problems by embedding commands too deeply in text:

- Line and paragraph breaks could occur inside commands.
- Local formatting changes might bleed outside commands.
- Robust column specifications could allow column breaks within data cells.

Additionally, certain commands have specific spacing needs that robustness undermines:

- Table cell separators assume consistent spacing.
- Fixed-width source code listings require strict column alignment.
- Some localized language translations may rely on space patterns.

Therefore, robustness should only be applied to general text-formatting commands where needed. More structural commands are better left non-robust to avoid unintended formatting side-effects.