Re: Feature request: Access to MP variable values from TeX/LaTeX?

Hans Hagen via metapost <[email protected]> Thu, 23 Oct 2025 22:06:13 +0200
Newsgroups gmane.comp.tex.metapost
Message-ID <[email protected]>
On 10/23/2025 7:07 PM, Toby Thurston wrote:
> Currently `luamplib` sets the values `\MPwidth` and `\MPheight` after each figure is processed to provide information about the bounding box, but would it be possible to get access to the value of other variables that MP has calculated?
> 
> For example, you might calculate the length of the side of a triangle and then want to refer to it in the text after the figure.  Sort of the opposite of the `\mpdim` mechanism which allows you to access TeX values from inside MP.
> 
> Something like this:
> 
>>    text before the figure.
>>
>>    \begin{mplibcode}
>>      beginfig(1);
>>      % calculate values....
>>      n = 42;
>>      % etc...
>>      endfig;
>>    \end{mplibcode}
>>    
>>    From the preceding figure we can see that $n$ equals \MPvalue{n}...
> 
> The `tkz-elements` package provides `\tkzUseLua` that lets you get at a Lua value, so maybe there is a generic Lua way of doing this?

This is not really a question for this list, more for some latex support 
forum. Anyway,

\begin{mplibcode}
     save n; numeric n ; n := 3cm ;
     fill fullcircle scaled n ;
     runscript ("FOO = " & decimal n);
     runscript ("tex.print('\\gdef\\FOO{" & decimal n & "pt}')");
\end{mplibcode}

This circle is \ctxlua{tex.print(FOO)}pt wide so let's
repeat this: \FOO !

might work but I can't test it. You can of course wrap that in some 
metapost macro.

In ConTeXt (lmtx, luametatex, luametafun) one would do something

\startMPcode
     save n; numeric n ; n := 3cm ;
     fill fullcircle scaled n ;
     setglobaldimen("globalscratchdimen",n)
     setglobalmacro("MyMacro",decimal n)
\stopMPcode

\the\globalscratchdimen==\MyMacro

or create a parameterset or ... but that is of little use here. 
Implementation choices also depend a bit on the load (one time access, 
tens of thousands ...).

Hans


-- 

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
--
http://tug.org/metapost/