# [texhax] intermediate output

Graham Toal gtoal at gtoal.com
Tue Nov 6 02:17:03 CET 2007

On 11/5/07, Toby Cubitt <tsc25 at cantab.net> wrote:
> > If your macros don't involve "programming", then a regular-expression
> > replace can do the replacement.
> >
> > If your macros do involve programming, then they have to be eliminated,
> > regardless of substitution method.  That is, your publisher won't allow
> > that stuff.
>
> I faced the same problem with my 'cleveref' package. To get around it, I
> implemented a 'poorman' option that automatically writes a sed script
> when the document is processed. When the original LaTeX source for a
> document is passed through this script, it replaces all the
> journal-unfriendly commands with their expansions, reducing them to
> plain text and standard LaTeX.
>
> If your macros don't involve programming, then I would just do regexp
> replacement using your favourite out of sed, awk, perl or some other

It's not that easy.  The problem lies in macro bodies which use
previously defined macros.  Remember, the macro calls in a macro body
are expanded at the place where the macro is used, not where it is
defined.  I think this is an insurmountable problem for the text
editor model, or at the very least requires several passes over the
file.

This is relatively easy to process with an editor:

\newcommand{\first}{Hello}
\newcommand{\second}{This is the second macro which invokes \first}
\newcommand{\third}{This is the third macro which invoked \second}
\third

This is a little tricker:

\newcommand{\third}{This is the third macro which invokes \second}
\newcommand{\second}{This is the second macro which invokes \first}
\newcommand{\first}{Hello}
\third

And good luck with this!:

\newcommand{\first}[1]{Hello #1}
\newcommand{\second}[2]{#1{#2}}
\newcommand{\third}{\second{\first}{World}}
\third

which is why you need a proper macro processor that works similarly to
TeX.  (and yes, the quick hack I posted last night does get that final
case correct, thank goodness :-) )

G