6

Is it possible to see the output of the TeX ‘pre-processor’, i. e. the intermediate step before the actual output is done but with all user-defined macros replaced and only a subset of TeX primitives left?

Or is there no such intermediate step?

Alexey Malistov
  • 26,407
  • 13
  • 68
  • 88
Debilski
  • 66,976
  • 12
  • 110
  • 133

5 Answers5

7

Write

\edef\xxx{Any text with any commands. For example, $\phantom x$.}

And then for output in the log-file

\show\xxx

or for output in your document

\meaning\xxx
Alexey Malistov
  • 26,407
  • 13
  • 68
  • 88
4

There is no "pre-processor" in TeX. The replacement text for any control sequence at any stage can vary (this is used for a lot of things!). For example

\def\demo{\def\demo{cde}}
\demo

will first define \demo in one way and then change it. In the same way, you can redirect TeX primitives. For example, the LaTeX kernel moves \input to an internal position and alters it. A simplified version:

\let\@@input\input
\def\input#1{\@@input#1 }
Joseph Wright
  • 2,857
  • 22
  • 32
  • But your examples can clearly be analysed. `\def\demo{\def\demo{cde}}\demo` evaluates to `\def\demo{\def\demo{cde}}\def\demo{cde}` and because there are no evaluations possible after this step, we can delete all `\def` statements and thus have it reduced to ∅. — `\def\demo{\def\demo{c{\it de}}}\demo\demo` however would eventually evaluate to `c{\it de}`. – Debilski Mar 17 '10 at 20:45
  • In this case yes, but in a real use we could have several definitions for \demo and it used in several places. The point I was getting at is that each macro does not have a fixed replacement text, and so you can't just replace things with their code. Of course TeX itself does replace things if they are macros, but not in "one shot": each token is examined as it is needed. – Joseph Wright Mar 18 '10 at 19:37
4

Try the Selective Macro Expander.

lhf
  • 70,581
  • 9
  • 108
  • 149
  • @holdenlee, try https://web.archive.org/web/20100310064605/http://www.astro.indiana.edu/~jthorn/software.html. – lhf Feb 27 '16 at 10:48
4

TeX has a lot of difference tracing tools built in, including tracing macro expansion. This only traces live macros as they are actually expanded, but it's still quite useful. Full details in The TeXbook and probably elsewhere.

When I'm trying to debug a macro problem I generally just use the big hammer:

\tracingall\tracingonline

then I dig in the output or the .log file for what I want to know.

Norman Ramsey
  • 198,648
  • 61
  • 360
  • 533
  • According to [this answer](http://tex.stackexchange.com/a/60494/15665), `\tracingonline` requires a parameter. (But `\tracingall` should already cover everything, no?) – Evgeni Sergeev Feb 11 '16 at 16:34
0

There's a lot of discussion of this issue on this question at tex.SE, and this question. But I'll take the opportunity to note that the best answer (IMO) is to use the de-macro program, which is a python script that comes with TeXLive. It's quite capable, and can handle arguments as well as simple replacements.

To use it, you move the macros that you want expanded into a <something>-private.sty file, and include it into your document with \usepackage{<something>-private}, then run de-macro <mydocument>. It spits out <mydocument>-clean.tex, which is the same as your original, but with your private macros replaced by their more basic things.

Accácio
  • 3
  • 1
  • 4
Mike
  • 19,114
  • 12
  • 59
  • 91