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

Toby Cubitt tsc25 at cantab.net
Wed Apr 22 10:39:50 CEST 2009

Hi Uwe,

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.

Hope that makes it clear,


Uwe Lück wrote:
> Hi Toby,
> could you please just plainly (to a human being) tell what substitutions
> cleveref needs?
> Best, Uwe.
> At 16:57 21.04.09, Toby Cubitt wrote:
>> Yes, I made cleveref write out a sed script. As for the sed/awk/perl
>> debate, I personally prefer to use the simplest tool for the job: sed if
>> possible, awk if sed isn't up to it, and perl if the awk solution
>> would be
>> too complicated. (I've never yet gotten as far as perl :)
>> I completely see why one might like to make cleveref write out a TeX
>> script instead (the script would then need to use one of the string
>> substitution packages you mention). The reasoning, of course, is that if
>> someone is using cleveref, then they're guaranteed to have TeX installed
>> and working, so by far the most portable and robust solution is to
>> generate a TeX script to carry out the desired string substitutions.
>> However, implementing this would be far more work than I'm prepared to
>> put
>> into a feature that is simply a work-around that allows me (and maybe
>> others) to submit papers to journals that accept LaTeX but don't support
>> cleveref (i.e. all of them :-)
>> If someone wanted to contribute a patch that (optionally) generated a TeX
>> (or awk or perl) script, instead of a sed script, I'd be happy to include
>> it in cleveref...
>> Toby
>> PS: I read about the Mars rover project a while back. It definitely falls
>> into the "foolish quest" category :)

More information about the texhax mailing list