[texhax] Low-level TeX question: string substitution macro

Uwe Lück uwe.lueck at web.de
Wed Apr 22 15:42:43 CEST 2009


At 10:39 22.04.09, Toby Cubitt wrote:
>Firstly, I should make it clear that in normal usage cleveref doesn't need
>any string substitutions at all. It's only the "poorman" option that needs
>them, and even then it doesn't require any string substitutions to
>actually process the LaTeX file and generate the dvi or pdf.
>
>When the "poorman" option is supplied, in addition to the usual processing
>of the LaTeX document, cleveref will write a sed script for you,
>containing substitutions that replace all of the cleveref cross-reference
>commands in the document with something that's (very nearly) equivalent,
>but that uses just the standard LaTeX \ref command.
>
>For example, using cleveref, \cref{eq1,eq2,eq3} would be type-set as
>"equations~(1) to~(3)". With the "poorman" option, the following
>substitution would be written to the sed script:
>
>   s/\\cref{eq1,eq2,eq3}/equations~(\\ref{eq1}) to~(\\ref{eq3})/g
>
>which replaces all occurences of "\cref{eq1,eq2,eq3}" with
>"equations~(\\ref{eq1}) to~(\\ref{eq3})".
>
>Once you've processed the document the requisite number of times to get
>the cross-references right, you can then run the original source file
>through the sed script to produce a new LaTeX source file, from which all
>the cleveref commands have been stripped out and replaced by equivalent
>standard LaTeX, suitable for submitting to a journal (or sending to a
>colleague) who doesn't have cleveref installed.

The last thing is quite important, I think, so "poorman" is almost inevitable.

I understand all of this to the effect that cleveref does the clever thing,
not sed, so cleveref determines an expansion <plain> of a \cref term <clever>.
When cleveref finds *one* <clever> in "poorman" mode, it tells an external
program to replace *all* occurrences of <clever> by <plain>.

To replace sed by TeX, I propose writing to <out>=\jobname.ffd as follows:

     {\let\ref\relax \let\cref\relax
      \immediate\write<out>{\string\AddReplacement{<clever>}{<plain>}}}

(Is cleveref clever enough not to do this twice for *one* <clever>?)

A fifinddo script \inputs \jobname.ffd to set up a sequence of substitution 
macros acting on each line of \jobname.tex.

\AddReplacement would be defined by the next version of fifinddo.
Currently there is only setup for replacement macros with explicit 
identifiers,
\AddReplacement would use this with automatically generated identifiers.

fifinddo is in my view better suited for reworking TeX files than those 
other mere string handling packages. datatool may be well too, but the 
documentation is so large and I don't see that it deals with backslashes 
(it seems to deal with "data", not TeX code). (And it currently suffers 
from a substr.sty bug.)

Hope this will happen -- Uwe.

[OK Tom? You are right. Oh, Philip, you are right as well!]



More information about the texhax mailing list