# [pdftex] Parametrized btex/etex-blocks?

Hans Hagen pragma at wxs.nl
Sat Nov 30 17:14:32 CET 2002

```At 06:10 PM 11/25/2002 +0100, Magnus Lie Hetland wrote:
>I've noticed that when I write MetaPost code where I need several
>labels typeset with LaTeX, my code seems to become a bit repetitive...
>What I need is a way to parametrize my btex/etex blocks -- or so I
>think...
>
>For example, let's say I write something like
>
>   def foo(expr p, n)=
>       label(btex \$2^n\$ etex, p);
>   enddef;
>
>Of course, here n will be typeset as the letter 'n'. What I'd like is
>a way to substitute the value of the parameter n into the tex text.
>Since btex/etex blocks are processed with a preprocessor this sounds a
>bit impossible -- but since I'm not 100% sure, I thought I'd ask.
>
>An alternative is, I guess, to do something like:
>
>   verbatimtex \newcommand{\foo}[1]{\$2^{#1}\$} etex
>   def foo(expr p, l)=
>       label(l, p);
>   enddef;
>
>And then call it with
>
>   foo((100, 100), btex \foo{3} etex);
>
>or something...? This seems to work fine, but it would be nice to be
>able to abstract away the btex/etex stuff inside the macros... Also,
>it would be nice to be able to calculate things that to into the
>btex/etex blocks.
>
>Is something like this possible? (An alternative would, of course, be
>to use a preprocessor, possibly in a different language...) I assume
>that the possibilities are severely limitid if the preprocessing has
>to take place before any interpretation has taken place...

the btex ... etex is filtered from the file before an mp run; during a run,
it's also filtered from included files, which provides a way to trick mp
into processing them runtime. But, this is quite slow, esp when you have
hundreds of labels.

in metafun (www.pragma-ade.tex -> showcase -> manuals -> metafun) there is
a mechanism that provides the feature you want:

(embedded in tex code:)

\starttext

\startMPpage
for i=1 upto 10 :
draw textext("\$x^{" & decimal i & "}\$") shifted (i*cm,i*cm) ;
endfor ;
\stopMPpage

\stoptext

so, textext does the job, and the variable i needs to be converted to a string

(btw, this problem is unrelated to pdftex [backend] or a macropackage;
two-way interfacing between tex and mp has a few tricky aspects )

Hans
-------------------------------------------------------------------------
Hans Hagen | PRAGMA ADE | pragma at wxs.nl
Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
tel: +31 (0)38 477 53 69 | fax: +31 (0)38 477 53 74 | www.pragma-ade.com
-------------------------------------------------------------------------