# [texhax] moreverb and write18 difficulty

Stephen Hicks sdh33 at cornell.edu
Sat Mar 5 22:16:52 CET 2011

First, I really do hope you're kidding about "very simple", because
TeX inside metapost inside TeX is certainly not simple.

I was unable to reproduce your "correct" result when moving the
\write18 outside the environment.  But I can tell you why it's not
working:

Try factoring out the \write18 into a separate macro:
\def\mpost{\immediate\write18{mpost -tex=latex vafa.mp}}
\def\cat{\immediate\write18{echo TEST.AUX;echo ========;cat
test.aux;echo ========}}
\newenvironment{vafa}{\verbatimwrite{vafa.mp}}{\endverbatimwrite\cat\mpost}

I've inserted an extra "cat test.aux" before the call to mpost.
Running this reveals that test.aux is in fact empty at the time mpost
is being called.  Adding another \cat after the environment gives the
same result.  My initial guess was that the pertinent line in the .aux
file should be written the output routine for the page, and since your
\immediate\write18 would happen before the OR, there was no way it
would see that line.  Unfortunately, removing the \immediate and/or
adding a few extra pages before the call to \cat (or \mpost) didn't
help matters at all.

Without digging through the LaTeX source, I don't know off hand when
the .aux file will be written.  But maybe you're better off with a
different approach that's less sensitive to output routine details.
Rather than linking in the .aux file in the metapost, maybe you can
allow LaTeX to expand a few macros in the source file.  I'm unfamiliar
with \verbatimwrite, but you can, by hand, construct an "environment"
that changes catcodes of special characters (including newline and/or
space) to 12, and then defines its own escape/grouping characters that
can be used to expand the \ref (or if you know \ref is the only thing
you want to expand before writing, you could instead define an active
character to do that...).  Or, you could make everything catcode 12
and then make \ active and have it look ahead to see if it's followed
by 'r', 'e', f', '{', and if so, expand the following reference...
For an example of how to write this sort of environment, see slides
8-12 or so of http://www.physics.cornell.edu/~shicks/slides/2009-bachotex.pdf

Now that I've said all this, I'll reiterate a point from those slides
- while it's certainly possible, and maybe cute, to make TeX do some
crazy things, it's not necessarily a good idea.  There's probably a
better, less hacky, way to accomplish the same goal - perhaps a
makefile or a shell script?  Or maybe an already-existing package like
mfpic could work?

Cheers,
steve

On Fri, Mar 4, 2011 at 10:49 PM, Vafa Khalighi <vafakhlgh at gmail.com> wrote:
> I have something very simple like:
>
> \documentclass{minimal}
>
> \usepackage{moreverb}
>
> \newenvironment{vafa}{\verbatimwrite{vafa.mp}}{\endverbatimwrite%
>
> \immediate\write18{mpost -tex=latex vafa.mp}}
>
> \begin{document}
>
>
>
> 1+2=3\label{eq1}
>
>
>
> \begin{vafa}
>
> prologues:=3;
>
> verbatimtex
>
> \documentclass{article}
>
> \input{test.aux}
>
> \begin{document}
>
> etex
>
> beginfig(1);
>
> numeric u;
>
> u = 1cm;
>
> draw (0,2u)--(0,0)--(4u,0);
>
> pickup pencircle scaled 1pt;
>
> draw (0,0){up}
>
> for i=1 upto 8: ..(i/2,sqrt(i/2))*u endfor;
>
> label.lrt(btex $\sqrt x$ etex, (3,sqrt 3)*u);
>
> label.bot(btex $x$ Equation \ref{eq1} etex, (2u,0));
>
> label.lft(btex $y$ etex, (0,u));
>
> endfig
>
> end
>
> \end{vafa}
>
> \end{document}
>
> but I do not know why in the output (vafa.1), instead "Equation 1", I get
> "Equation ??" on the x-axis. I tried putting \immediate\write18{...} outside
> of vafa environment, that fixed the problem but it is not what I want. How
> can I fix this?
>
> Thanks
>
> --
> If some one say: "You divide ten into two parts: multiply the one by itself;
> it will be equal to the other taken eighty-one times." Computation: You say,
> ten less thing, multiplied by itself, is a hundred plus a square less twenty
> things, and this is equal to eighty-one things. Separate the twenty things
> from a hundred and a square, and add them to eighty-one. It will then be a
> hundred plus a square, which is equal to a hundred and one roots. Halve the
> roots; the moiety is fifty and a half. Multiply this by itself, it is two
> thousand five hundred and fifty and a quarter. Subtract from this one
> hundred; the remainder is two thousand four hundred and fifty and a quarter.
> Extract the root from this; it is forty-nine and a half. Subtract this from
> the moiety of the roots, which is fifty and a half. There remains one, and
> this is one of the two parts.
>
> Muḥammad ibn Mūsā al-Khwārizmī
>
> _______________________________________________
> TeX FAQ: http://www.tex.ac.uk/faq
> Mailing list archives: http://tug.org/pipermail/texhax/
> More links: http://tug.org/begin.html
>
> Automated subscription management: http://tug.org/mailman/listinfo/texhax
> Human mailing list managers: postmaster at tug.org
>

More information about the texhax mailing list