[metapost] generic TeX interface?

Stephan Hennig mailing_list at arcor.de
Sat Dec 9 15:34:11 CET 2006


there are several ways to invoke (La)TeX from MetaPost to render
pictures from text input:

1. the classic btex..etex construct,

2. the TEX() macro provided by package tex.mp,

3. the textext() macro provided by package latexmp.mp.

The situation seems quite good since all alternatives have their uses.
But what if you have a macro that processes TeX generated pictures?  You
would have to write something like

mymacro(btex my text etex);
mymacro(TEX("my text"));
mymacro(textext("my text"));

depending on your TeX setup.  Calling the macro several times with
different parameters required typing an endless number of different
btex..etex, TEX or textext sequences.  It would be much more convenient
to only pass the raw string "my text" to the macro and process that by
TeX only in the macro.

But how?  By calling btex..etex, TEX or textext?  Going this route
required a unified interface from MetaPost to TeX.  This interface
should not deal with the TeX setup, but only communicate how to invoke
TeX from MetaPost.  That is, one variable indicating the TeX mode and
one generic macro that invokes TeX with the correct macro sequence
should be enough.

I've attached a first attempt to implement such an interface to this
mail.  Unfortunately, my solution doesn't work for the btex..etex case.
 Since text between btex..etex is interpreted verbatim you can't write
something like

btex s etex

to compile a string s.  I've tried

scantokens("btex " & s & " etex")

but scantokens refuses to process btex..etex constructs complaining

| ! You can only use `btex' or `verbatimtex' in a file.

I think that's the reason the TEX package was written.  Is there any
work around that?

Additionally, let me add, that in the longer term I'd like to see native
support for something like that in MetaPost, not only a package.  What
do you think?

Best regards,
Stephan Hennig

The following package y_tex.mp provides a variable y_tex_flavour that
indicates the mode to call TeX from within MetaPost and a macro y_tex
that takes as input a string and returns a picture generated by TeX
using either btex..etex, TEX or textext, depending on variable

if known y_tex_version: endinput; fi

%%% Variable y_tex_version contains the package version number.
newinternal y_tex_version;
y_tex_version := 0.1;

%%% Variable y_tex_flavour determines the mode MetaPost generates
%%% pictures containing TeX copy.  The following values are supported:
%%% 0  -  Use basic btex..etex.
%%% 1  -  Use macro TEX() provided by the tex.mp package.
%%% 2  -  Use macro textext() provided by the latexmp.mp package.
%%% Other values raise a warning on the console.
newinternal y_tex_flavour;
y_tex_flavour := 0;

%%% User macro.
%%% This generic macro takes as input a string containing TeX copy and
%%% returns a corresponding picture generated by TeX using the mode
%%% indicated by variable y_tex_flavour.
vardef y_tex(expr s)=
save pic;
picture pic;
  if y_tex_flavour=0:
    pic := scantokens("btex " & s & " etex");
  elseif y_tex_flavour=1:
    pic := TEX(s);
  elseif y_tex_flavour=2:
    pic := textext(s);
    message "!! package y_tex !!: TeX processor unknown!";

More information about the metapost mailing list