In the article class appears the following code related with the font selection:


What exactly does the commands at the code?


The declaration




Let's see what happens when LaTeX finds $\rm a$ in the following minimal document:

$\rm a$

Let's start:

\rm ->\protect \rm  

\rm is a robust command, so this is its normal expansion. Now, since \protect is \relax, \rm is expanded:

\rm  ->\@fontswitch {\normalfont \rmfamily }{\mathrm }

Next \@fontswitch is expanded:

\@fontswitch #1#2->\ifmmode \let \math@bgroup \relax 
\def \math@egroup {\let \math@bgroup \@@math@bgroup
\let \math@egroup \@@math@egroup }#2\relax \else #1\fi 
#1<-\normalfont \rmfamily 

We see the arguments. Since we are in math mode, the "true" branch is followed, so the redefinitions of \math@bgroup and \math@egroup are performed and \mathrm is expanded:

\mathrm ->\protect \mathrm  

\mathrm  ->\relax \ifmmode \else \non@alpherr \mathrm  \fi
\use@mathgroup \M@OT1 \symoperators 

\use@mathgroup #1#2->\relax \ifmmode \math@bgroup \expandafter
\ifx \csname M@\f@encoding \endcsname #1\else #1\fi \mathgroup #2\relax   
\expandafter \math@egroup \fi 

Again, we're in math mode (\mathrm has some code for disallowing its use outside of it); the tests are performed using the fact that the expansion of \f@encoding is

\f@encoding ->OT1

Now it remains to expand \math@egroup:

\math@egroup ->\let \math@bgroup \@@math@bgroup
\let \math@egroup \@@math@egroup 

and the business is finished. Essentially, this is transformed into


If {\rm a} is found in text mode, then it becomes essentially

{\normalfont\rmfamily a}

that is, the old behavior of the \rm command is emulated.

This is why {\sf\sl a} won't use a slanted sans serif font, but a serif slanted one: it becomes equivalent to

{\normalfont\sffamily\normalfont\slshape a}

Moral: never use the old font commands. You gain nothing and lose much of the flexibility of the new ones. Well, after 18 years they aren't really new.


Digging into source2e yields the following information:

[\DeclareOldFontCommand] is the function used to create declarative font-changing commands that can also be used to change alphabets in math-mode.

Basically the lines you quoted just make \rm, etc., equivalent to \normalfont\rmfamily in text mode and equivalent to \mathrm in math mode. This is done so that documents using these depreciated font commands still work correctly with LaTeX2e. (Note that the correct way to use declarative font-switches is \rmfamily, etc.)

Fragile and robust commands are one of the thornier issues in LaTeX's commands. Whilst typesetting documents, LaTeX makes use of many of TeX's features, such as arithmetic, defining macros, and setting variables. However, there are (at least) three different ocassions when these commands are not safe. These are called ‘moving arguments’ by LaTeX, and consist of:

  • writing information to a file, such as indexes or tables of contents.
  • writing information to the screen.
  • inside an \edef, \message, \mark, or other command which evaluates its argument fully.

The method LaTeX uses for making fragile commands robust is to precede them with \protect.


[\DeclareRobustCommand] is a package-writers command, which has the same syntax as \newcommand, but which declares a protected command.

Essentially, the use of \DeclareRobustCommand guarantees that nothing strange happens when (e.g.) a section title with a \cal is written to the .aux file.

\DeclareOldFontCommand uses \DeclareRobustCommand internally.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.