# 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.

-

## marked as duplicate by Red, Thorsten, Andrew Swann, Stefan Kottwitz♦Sep 20 '13 at 14:17

This question was marked as an exact duplicate of an existing question.

Actually this is more relevant tex.stackexchange.com/questions/3174/latex-macro-expander however it is dated 2010. Are there any new developments? – Bordaigorl Sep 20 '13 at 12:37
Short answer: not possible. Some macro could have modified the meaning of \blatrue to \blafalse, for instance. – egreg Sep 20 '13 at 12:52
Yes I understand that, that's why I am saying a naive parser wouldn't do. However it is in principle possible to replay exactly what tex would do and make decisions based on the current state at the point where it swallows the \bla token. I am aware it's a daunting task to implement it but I wanted to know if anybody attempted to do something like that. – Bordaigorl Sep 20 '13 at 13:54
It's hard to say what the end state of such an evaluation would be. Ultimately all macros would have to be expanded down to primitives, of course. \catcode and all forms of \def and \let would have to be evaluated, but since you don't want typesetting you would have to leave, say, \hbox unevaluated. Except that inside an \hbox there is effectively an entire new document that needs "partial evaluation". And you would need to evaluate some boxes, for example \box0=\hbox{x}\ifdim\wd0>0.... (continued) – Ryan Reich Sep 20 '13 at 14:06
(continued) In summary, I think this is impossible without enforcing a lot of structure on the document and on its programming style. This may be possible in LaTeX but I suspect not. More likely in ConTeXt. – Ryan Reich Sep 20 '13 at 14:07