[Tex/LaTex] When is it appropriate to use \outer

macrostex-core

\outer is a strange beast. Any control sequence declared \outer cannot be used as either the parameter text or replacement text of a macro, at least if the control sequence is declared \outer at the time the parameters are tokenized or the definition is scanned, respectively. That is, both the definition of \bar and the use of \baz are errors:

\outer\def\foo{asdf}
\def\bar{\foo}
\def\baz#1{#1}
\baz\foo

However, changing this slightly to

\def\bar{\foo}
\def\baz#1{\outer\def\foo{asdf}#1}
\baz\foo
\bar

so that \foo is only declared \outer after the definition of \bar and after the parameters to \baz have been parsed, compiles correctly.

I could see requiring a macro to appear only in vertical mode, but \outer doesn't do that. In fact, \outer control sequences can appear in any of vertical, internal vertical, horizontal, restricted horizontal, math, and display math modes.

\outer\def\foo{asdf}
\foo% vertical mode

foo \foo% horizontal mode

\hbox{\foo}% restricted horizontal mode

\vbox{\foo}% internal vertical mode

$\foo$% math mode

$$\foo$$% display math mode
\bye

So given all of that, what is the utility of \outer?

Best Answer

\outer isn't used in LaTeX2e and I'm not aware of any packages that do, either (there are probably some, though). However, I've come across occasion to think it might sometimes be a good idea.

\outer prevents a macro from being used inside any other macro. But when is this even desirable? Well, consider the use of \verb: because it changes catcodes, and catcodes are frozen when tokenization occurs, \verb can never be (reliably) used inside any other macro. For examples, this will not work:

\section{The \verb|\outer| primitive}

Now, \verb contains its own processing to give a suitable error message in this case anyway, but there are other packages that define commands that use catcode-mucking to do their work. For examples, some indexing commands and, say, \url, can give bad output if they're used inside macros. In theory, you could use \outer to enforce their use in running text only, but this isn't done very often.

Note also that eTeX gives \scantokens which relieves many of the restrictions of only being able to change catcodes before tokenization occurs. So \outer is less useful these days from that point of view.

Related Question