I'm just starting to figure out what 'expandable' means in LaTeX, and why it is useful to have commands that expand as far as possible. In particular:
- If you want to
\typeout
some string to the log file, you need to be able to expand all the way to it. - If you want to do any kind of unit-testing (like with
qstest
), you need to be able to expand at least down to the strings you want to compare.
I've been reading a lot about this topic. As I understand it, a command is not expandable if it changes the 'state' in some way, i.e., uses \def
, \let
, etc. In particular, I've also read the question "Why isn't everything expandable". But my question is less theoretical than that one. I'd like to know why I haven't encountered any 'solutions' yet.
The thing is, 'making everything expandable' is conceptually so simple. TeX is perfectly capable of typesetting the result of 'unexpandable' commands, simply by performing the state changes in the execution processor. The point being, TeX does eventually arrive at the final string.
(Edit: Please note that I'm talking about textual characters here. I understand that one cannot capture the notion of, e.g., an image, flexible whitespace or a state-manipulation in a string.)
It seems inconceivable to me that we can't somehow intercept it and use it. I would have expected someone to have created a command like this:
\getresult { <expr> } { <result-macro> }
It's not expandable, but we can still get our hands on the fully expanded result in order to log it... compare it... what have you.
Questions like this one suggest to me that this has not been accomplished.
Why?
Ok... I'm willing to cheat. What if we spawn an extra TeX process to fully evaluate the expression and (almost) typeset it. Can we get the string that way? Doesn't \scantokens
use a similar trick to overcome another inherent TeX limitation (that catcodes are fixed once a token is read)?
Can it be done?
\hbox{ABC\hskip 1 cm DEF}
be? For example, take a look at the\tracingall
output of a typeset page.