# What is the difference between \unexpanded and \protected in ConTeXt?

EDIT: It turns out that \unexpanded in ConTeXt is different from \unexpanded in ε-TeX, so the earlier description doesn't apply. I'll update the question accordingly.

From the ε-TeX manual:

• Protected macros (defined with the \protected prefix) are not expanded when building an expanded token list (for \edef, \xdef, \message, \errmessage, \special, \mark, \marks or when writing the token list for \write to a file) or when looking ahead in an alignment for \noalign or \omit.

From the ConTeXt Wiki:

When expansion of a macro gives problems we can precede it by \unexpanded, like so:

\unexpanded\def\somecommand{... ... ...}

This will prevent the macro from being expanded in places where no typesetting occurs, like when strings are written to the tuo file.

In practice, I've seen the two used in seemingly the exact same situations, although \unexpanded\def... seems more common. Is there any notable difference between the two that I should be aware of?

• \unexpanded\def is only used by ConTeXt, which has different namings. – egreg Oct 14 '18 at 9:06
• I probably should have mentioned in the original post that I'm working with ConTeXt, sorry. – N.D. Oct 14 '18 at 14:31

If I compile with context the following

\show\unexpanded


I get

> \unexpanded=\protected

tex error       > tex error on line 1 in file /Users/enrico2013/provaccia/sep2018/tttsssttt.tex: ?

l.1 \show\unexpanded

1 >>  \show\unexpanded
2


In /usr/local/texlive/2018/texmf-dist/tex/context/base/mkiv/syst-aux.mkiv, line 61 reads

\let\unexpanded\normalprotected


If I add \show\normalprotected, then the output is

> \normalprotected=\protected


Of course, the meaning of \unexpanded could be reassigned, but macros defined when \unexpanded has that meaning will be \protected (in e-TeX lingo).

• I did look through syst-aux.mkiv and found that line, but given David Carlisle's answer and the fact that I get different errors when I use \unexpanded vs. \normalprotected, I'm no longer so sure that they're the same, or whether the ε-TeX definition is still relevant. – N.D. Oct 14 '18 at 14:56

This is comparing the etex primitive \unexpanded not the context macro of the same name. The Question has since been clarified that the context command was what was intended.

They are not really comparable:

\protected is a modifier of \def and makes the resulting token have an internal flag set so that its expansion is suppressed in certain contexts. So it is a property of the command.

\unexpanded is used to prevent the expansion of a token list so it is a property of the context not of the commands that are in it.

Of course there is some overlap, if you are writing to a file and need to prevent expansion then you could (as in classic latex) declare that fragile commands will fail and so arrange that all definitions are made with \protect so there are less fragile commands. Or you could use \unexpanded and prevent all expansion whether or not the commands are made with \protect, this would also stop any user defined commands expanding to their replacements which may or may not be what you want, depending on circumstances.

• @G.S. no that's a syntax error unexpanded works like \edef\foo{zzz \unexpanded{... \scarymacrohere..} zzz} that is it does the same as \the\toks@ in an \edef without the need for using a toks register. It is unrelated to definitions. – David Carlisle Oct 14 '18 at 14:58
• Oh I see from comments under the question you are comparing the etex primitive \protected to the context macro \unexpanded rather than to the etex primitive \unexpanded That might explain the question:-) – David Carlisle Oct 14 '18 at 15:00
• To use the \unexpanded primitive in ConTeXt you have to use \normalunexpanded. – Wolfgang Schuster Oct 14 '18 at 15:04
• @WolfgangSchuster Thanks, but it turns out I actually meant to ask about ConTeXt's \unexpanded macro (I've updated the question). Mostly I've been indecisive on whether to add \unexpanded or \normalprotected before definitions, especially since \setuvalue & similar commands defined in syst-aux.mkiv use \normalprotected while most other uses of \def I've seen in the source code use \unexpanded. – N.D. Oct 14 '18 at 15:08
• @G.S. You should ask these questions on the ConTeXt list because only Hans Hagen can give you an answer for this. – Wolfgang Schuster Oct 14 '18 at 15:12