On unprotecting (expanding) \protected macros (or, "the space after command name")
\protected
stops expansion in \edef
and \write
and (unlike the LaTeX protection mechanism) it's not so easy to take off once it is applied. It's a bit hard to know what you need here as you ask for teh "string" but TeX doesn't have strings.
you can do
\makeatletter
\protected\def\foo#1{---{\large#1}---}
\edef\x{\expandafter\strip@prefix\meaning\foo}
\show\x
which produces
> \x=macro:
->---{\large #1}---.
l.8 \show\x
but note here that \x
does not contain the \large
command in its definition, it has the 6 tokens \ l a r g e
(plus other tokens---{}---
).
To see why you need protection, compare
\documentclass{article}
\makeatletter
\def\foo#1{---{\large#1}---}
\edef\x{\foo 1}
\show\x
putting a fragile unprotected command in an edef produces
! TeX capacity exceeded, sorry [input stack size=5000].
\@nomath ...e \@font@warning {Command \noexpand #1
invalid in math mode}\fi
l.8 \edef\x{\foo 1
}
! ==> Fatal error occurred, no output PDF file produced!
David has explained this one way, I will take a slightly different tack.
First, what is going on? When e-TeX finds a protected macro, it will not expand it inside an \edef
, \write
and some similar circumstances, which usually exhaustively expand everything. That is to ensure you can see the protected token in the result. For example,
\protected\def\a{}
\typeout{%
\a%
b%
}
will show \a b
, even though there is no space between \a
and b
as far as TeX is concerned. That's because the alternative is \ab
, which as we can't see the tokens would be misleading (does it mean \a
followed by b
or a different macro \ab
?).
You can 'force' expansion of a protected macro by using the fact that they do respect \expandafter
:
\protected\def\a{b}
\edef\test{\expandafter\empty\a}
\show\a
using \empty
as something to expand after which will not result in anything remaining behind.
Second, why do you need protection? Some operations in TeX simply will not work within an \edef
as they use TeX primitives which are not 'expandable'. The classic ones here are assignments (\def
, \let
and so on). If you try
\let\a\undefined
\def\b{a}
\edef\test{\let\a\b}
you will not find that \a
ends up defined to give a
. Instead, you'll get an error: in this case 'Undefined control sequence
'. That is because \let
is not expandable. So TeX simply 'leaves it alone' inside the \edef
, and tries to expand \a
. That's not possible, and so an error arises. Thus in general it is not a good idea to try to expand protected macros.