# Is there a list of expandable TeX primitives? LaTeX? e-TeX? others?

According to Christian Hupfer's comment I understood that calculations are possible in a keyval list when the macro is expandable. The macro \numexpr being expandable, calculations are possible.

Hence the interest in knowing which macros are and which are not expandable from TeX, LaTeX, e-TeX, etc.

I am not asking if there is a test, but if there is a list already made.

Joseph has already answered, interpreting the "expandable in your question as "fully expandable" (or what is perhaps better named as "safe in an expansion only context" see

However to answer the question as actually asked, note that \numexpr is neither a macro nor expandable.

All macros are by definition expandable, a macro works by expanding to its replacement text. There is no list of these as it is any command defined via \def or its variants \edef, \gdef, \xdef.

The TeXBook lists all the TeX primitivies, these are marked by a * in the index, although you need to check the description of each individually to see if they are expandable.

In classic TeX, the expandable primitives are (I think:-)

\expandafter
\csname
\the
\number
\romannumeral
\if
\ifx
\ifcat
\ifcase
\ifnum
\ifodd
\ifdim
\ifeof
\iftrue
\iffalse
\ifhbox
\ifvbox
\ifvoid
\ifinner
\ifvnode
\ifhmode
\ifmmode
\else
\or
\fi
\input % (\@@input in LaTeX)
\jobname
\meaning
\noexpand
\string
\topmark
\firstmark
\botmark
\fontname


in e-TeX the additional expandable primitives are

\topmarks
\firstmarks
\botmarks
\ifdefined
\ifcsname
\iffontchar
\unless
\eTeXrevision
\unexpanded
\detokenize
\scantokens


in addition to all the above pdfTeX adds the following expandable primitives

\pdfescapestring
\pdfescapename
\pdfescapehex
\pdfstrcmp
\pdfmatch
\ifpdfabsnum
\ifpdfabsdim
\pdfuniformdeviate
\pdfnormaldeviate
\pdffilemoddate
\pdffilesize
\pdfmdffivesum
\pdffiledump
\pdfcolorstackinit
\ifincsname
\ifpdfprimitive
\pdfcreationdate
\pdfinsertht
\pdftexbanner
\pdftexrevision
\expanded


XeTeX has the primitives of etex, and adds the following expandable primitives

\ifincsname
\ifprimitive
\normaldeviate
\uniformdeviate
\filedump
\filemoddate
\filesize
\mdfivesum
\expanded
\XeTeXrevision
\Uchar
\Ucharcat
\strcmp


LuaTeX has the expandable primitives of etex plus (at least)

\luatexbanner
\luatexrevision
\formatname
\Uchar
\directlua
\luaescapestring
\scantextokens
\csstring
\expanded
\ifincsname
\pdfvariable
\pdffeedback


pTeX (Japanese TeX engine) adds the following expandable primitives, which are available in ptex, uptex, eptex and euptex:

\euc
\ifdbox
\ifddir
\ifmbox
\ifmdir
\iftbox
\iftdir
\ifybox
\ifydir
\jis
\kansuji
\kuten
\ptexrevision
\sjis


upTeX (Unicode-aware pTeX) adds the following expandable primitives, which are available in uptex and euptex:

\ucs
\uptexrevision


e-pTeX (pTeX + e-TeX) adds the following expandable primitives, which are available in eptex and euptex:

\expanded
\ifincsname    % for TL2020
\ifpdfprimitive
\pdfcreationdate
\pdffiledump
\pdffilemoddate
\pdffilesize
\pdfmdfivesum
\pdfnormaldeviate
\pdfstrcmp
\pdfuniformdeviate


If you find missing ones, feel free to edit this answer here....

• @AndréC yes, macros are all user defined commands, although some expandable primitives act very much like pre-defined macros. (but \numexpr isn't expandable either) – David Carlisle Dec 26 '18 at 10:30
• Then I should have asked the question with primitives and not macros. "Is there a list of expandable TeX primitives? LaTeX ? e-TeX?" Can I change the title? – AndréC Dec 26 '18 at 10:32
• @AndréC you can, although not sure it makes any difference really, neither Joseph or I answered the question in the title, although we "interpreted" it differently to give slightly different answers. Mine is probably closest to the lhe literal meaning of your question, but as Joseph notes in a comment under his answer, it is just a list and not that useful really. – David Carlisle Dec 26 '18 at 10:35
• @Dr.ManuelKuehner classic tex mostly, the others I scanned over the manual, I may have missed a few but I'll edit if anyone spots any errors – David Carlisle Dec 26 '18 at 11:35
• @HironobuYAMASHITA thanks for the edit – David Carlisle Oct 15 at 15:07

A list of all expandable macros is entirely impossible to provide: there are an open-ended number of cases. What one can do is provide a list of primitives which work by expansion, and general rules for macros.

Essentially, any primitives which carry out assignment or typesetting are not expandable, whilst other primitives are. Thus for example \def, \let, etc. are all non-expandable, as are \hbox, \raise, \setbox, whereas \expandafter, \the and \ifx are all expandable. Note that some primitives will be expandable in the right context: \numexpr is not expandable in itself, but is a valid token coming after \the or \number:

\edef\testa{\numexpr 1+2\relax}\show\testa
\edef\testb{\number\numexpr 1+2\relax}\show\testb


In terms of macros, we have to be clear what we mean by 'expandable'. TeX is a macro expansion language, so if we have for example

\def\baz{}
\def\foo{\def\baz{bar}}


then we can do

\edef\test{\foo}\show\test


although the result is not what is likely wanted/useful. As such, when we talk about expandable macros we normally mean macros which contain only expandable primitives and which thus can be used 'safely' inside an \edef or similar. With e-TeX, it' possible to ensure that macros which don't meet this criterion don't 'blow up':

\def\baz{}
\protected\def\foo{\def\baz{bar}}
\edef\test{\foo}\show\test


Where does that take us? Any macro which contains:

• Any assignment primitive
• Any typesetting primitive
• Any \protected macro
• Any macro which itself meets one of the three criteria above

is not expandable. That is the majority of 'useful' macros, so as mentioned in comments, if you are not sure, assume non-expandable.

Note that the list of non-expandable primitives is quite long, and we do have to remember about the context. For example, something like \tracingcommands acts like a count register, so is non-expandable unless if follows \the. Thus unless we implement a token-by-token processor, just seeing \tracingcommands doesn't tell us that a macro containing this token is not expandable.

\def\foo{\tracingcommands=1 }% Not expandable
\def\baz{\the\tracingcommands}% Expandable


In LaTeX3/expl3, all macros are either fully expandable or are \protected. Moreover, the expandable ones are all marked as such (with a star) in the documentation. The reason is that it requires some knowledge to see if something is expandable, in particular checking all macro 'dependencies'. As such, one has to work carefully to track expandable macros.

• There are around 300 TeX90 primitives, plus a lot more added by e-TeX, XeTeX, LuaTeX, ... A list of all non-expandable (or indeed expandable) ones is going to be long, and I'd say not all that useful. – Joseph Wright Dec 26 '18 at 9:37
• oops I just noticed this comment, which accurately predicts my answer:-) – David Carlisle Dec 26 '18 at 10:10
• @JosephWright This is something I've never understood either. Is it close to say that expandable == without side effects? – dedded Oct 14 at 13:25
• @dedded Close but of course \csname <some-undefined-name>\endcsname adds a \relax` to the table of names, plus things like the PDF random number generator step the seed, etc. – Joseph Wright Oct 14 at 13:27