# Is there a way to partially evaluate TeX files? [duplicate]

I am looking for tools or techniques that can partially evaluate (e.g. inline definitions) a TeX source file into an equivalent, simplified TeX file with some parameters fixed.

Specifically I would be interested in a solution achieving the following:

1. eliminate unreachable branches of \if. For example it should be able to simplify

\newif\ifbla
\blatrue
\ifbla
GOOD
\else
\fi

to GOOD.

2. Inline macro definitions. For example transforming

\def\bla#1{bla to #1}
\begingroup
\def\bla#1{blerp at #1}
\bla{me},
\endgroup
\bla{you}

to blerp at me, bla to you

3. Inline files (with the aim of combining this with point 2 in compiling a standalone tex source).

The main difficulty in doing this is reliability: the macro system makes any naive parser break very quickly in the presence of non-trivial macro (re-)definitions.

-