Here's the same answer as others have given, but from a different perspective.
As a new LaTeX user, it's natural to think of TeX as being a markup language consisting mostly of actual text with occasional formatting directives (\textbf
, math mode $...$
, and so on) and macros that stand for long bits of text that should be abbreviated. Indeed, LaTeX goes to some trouble to present this appearance to the user.
This is not how things actually are. A directive such as \mymacro
might be defined in such a way as to completely transform the text appearing after it, or even change the way TeX reads its input. It is better to imagine that everything you write is a command for telling TeX how to typeset: a letter tells TeX to typeset itself, but macros (and other things even more horrible) can either tell it to typeset something else, or tell it to typeset things in a different way entirely.
From this perspective, there is no way that TeX could do a simple "substitution run" on its input: macros can mean something far more serious than simple text replacement and there is no way to tell until they are expanded in order. Furthermore, the result of that expansion may be typesetting commands like \hbox
that don't mean expand into plain text at all and can never be eliminated. As an example of this, here is what the command \TeX
means:
T\kern -.1667em\lower .5ex\hbox {E}\kern -.125emX
It's a "macro" as you consider it, but it doesn't just say "write the letters TeX", but rather describes how close they must be written (\kern
) and at what height (\lower
), so as to draw the TeX logo. That just doesn't have any meaning in a plain text file.
You might be interested in the question Can LaTeX be persuaded to produce text output?, though I'm afraid the answers are not encouraging.
Really, the answer to your question is that the "full expansion" of a TeX file is a finished document, since that is the minimal format that can meet the requirements expressed in the input. If you know that the document only contains plain text (no pictures or symbols), then pdftotext
may give you a reasonable approximation to what you want.
\newcommand{\foo}{Foo}\foo\renewcommand{\foo}{Bar}\foo
inside a document: this defines\foo
, uses that macro, redefines\foo
, and uses it again, showing that macros are not defined once, but can change during document "compilation". Getting the output of the "second step" is really tricky, because the expansion of macros can depend on the status of the typesetting.