This question already has an answer here:

Can I expand all \newcommands without doing anything else in LaTeX?

Edited: My true goal is standardizing math papers in plain text so that theorems and definitions can be accurately and easily extracted without losing mathematical symbols.

marked as duplicate by Werner macros Nov 19 '18 at 21:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • No (maybe there exists an external program to preprocess your LaTeX file) – Henri Menke Nov 19 '18 at 21:02
  • @HenriMenke Yeah I think I will just write one. It shouldn't be hard. It's pretty much just putting all new commands into a python dictionary and recursively substituting them to the point that they are all gone. – Ying Zhou Nov 19 '18 at 21:04

There are several existing programs that do this in limited contexts for example


However it is in general not possible to expand all macros without a full tex execution.


\newcommand\foo{\ifx\zzz\undefined no\else yes\fi}

which expands to no or yes depending on the state of TeX at that time or

\newcommand\foo{\sbox0{hello}\ifdim\wd0>2cm yes\else no\fi}

which expands to no or yes depending on the width of some text set in the current font.

In simple cases the document will behave the same way if you expand the macros but for example if you expand \zzz (and remove its definition) then the expansion of \foo will change.

If however you are restricting to expanding out simple shortcut macros used for authoring convenience, which contain no conditional or recursive calls then simple string replacement in any text processing tool will probably do the right thing, on a good day.

  • Also consider that a phrase of tex-input might yield different tokens/different replacement depending on the catcode-régime at the time of tokenizing. \newcommand\foobar{...} versus \verb|\foobar| or \string\foobar or \csname foobar\macro\endcsname. (In the latter case, the result depends on the definition of \macro.) Or \catcode\r=13... \def r{<something>}...\foobar... Besides this \let-assignments might be nice, too: \newcommand\foobar{...}` ... \let\foobar=\whatsoever...\whatsoever. – Ulrich Diez Nov 20 '18 at 10:10
  • @UlrichDiez I managed to avoid including a copy of xii.tex in this answer, don't tempt me:-) – David Carlisle Nov 20 '18 at 10:45
  • I like your xii.tex. It reminds me of Michael Downes' Around the Bend questions 10 and 11 (Obfuscated TeX code / Decoding obfuscated TeX code). – Ulrich Diez Nov 20 '18 at 11:00

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