Text Formattingxspace | Redefine \LaTeX command

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

xspace | Redefine \LaTeX command

Post by svend_tveskaeg »

Hi all.

Is is possible to (i.e., how do I) redefine the \LaTeX command so that I do not have to write either {\LaTeX} or \LaTeX{} in order to get the correct spacing after the word?

I have tried the following:

Code: Select all

\documentclass{article}

\usepackage{xspace}
\renewcommand*{\LaTeX}{\LaTeX\xspace}

\begin{document}
\LaTeX
\end{document}
I then get the error

Code: Select all

! TeX capacity exceeded, sorry [input stack size=5000].
\LaTeX ->\LaTeX 
                \xspace 
l.7 \LaTeX
Thank you in advance!
Last edited by svend_tveskaeg on Fri Jan 25, 2013 9:00 pm, edited 1 time in total.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)

Recommended reading 2024:

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

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

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

xspace | Redefine \LaTeX command

Post by localghost »

You have to "save" the original command into a new one and later use this new command to redefine the old one.

Code: Select all

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{xspace}

\let\orgLaTeX\LaTeX
\renewcommand*{\LaTeX}{\orgLaTeX\xspace}

\begin{document}
  \LaTeX.

  \LateX rocks!
\end{document}
Otherwise you get an infinite loop, thus the error.


Thorsten
User avatar
Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

xspace | Redefine \LaTeX command

Post by Stefan Kottwitz »

You can use \expandafter to change the order of the macro expansion, which fixes the problem:

Code: Select all

\expandafter\def\expandafter\LaTeX\expandafter{\LaTeX\xspace}
Another possibility would be using an internal macro to attach \xspace to \LaTeX:

Code: Select all

\makeatletter
\g@addto@macro\LaTeX\xspace
\makeatother
Stefan
LaTeX.org admin
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Re: xspace | Redefine \LaTeX command

Post by svend_tveskaeg »

Nice.

Thank you, Thorsten and Stefan!
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

xspace | Redefine \LaTeX command

Post by cgnieder »

The reason why your definition fails is that TeX and thus LaTeX is a macro language. A macro gets replaced by its definition. I would prefer a description for \newcommand/\renewcommand as follows:
  • \newcommand\csname{<replacement text>}
So, let's take your definition:

Code: Select all

\renewcommand*{\LaTeX}{\LaTeX\xspace}
and see what happens. I'll use => with the meaning “is replaced by”

Code: Select all

\LaTeX => \LaTeX\xspace => \LaTeX\xspace\xspace => \LaTeX\xspace\xspace\xspace => ...
You can see that this ends up in an endless loop and that's why the capacity exceeded.

The standard way would be something like this:

Code: Select all

% save previous definition:
\let\originalLaTeX\LaTeX
% redefine:
\renewcommand\LaTeX{\originalLaTeX\xspace}
See also the TeX FAQ entry on Patching existing commands.

Regards

PS: bah, too late again ;)
site moderator & package author
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Re: xspace | Redefine \LaTeX command

Post by svend_tveskaeg »

Thank you, Clemens.

Nice explanation.

P.S. Better late than never. ;)
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
Post Reply