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.
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.
There are several existing programs that do this in limited contexts for example
https://ctan.org/tex-archive/support/de-macro
However it is in general not possible to expand all macros without a full tex execution.
Consider
\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.
\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{...\foobar... Besides this\let-assignments might be nice, too:\newcommand\foobar{...}...\let\foobar=\whatsoever...\whatsoever`.xii.tex in this answer, don't tempt me:-)
– David Carlisle
Nov 20 '18 at 10:45