2

Is it possible to run a Lua function from within luamplib environment? This example with runscript does not work, but shows what I like to do:

\documentclass{article}
\usepackage{iftex,luamplib}

\begin{filecontents}[force,noheader]{foo.lua}
function getA()
    return "2cm"
end
\end{filecontents}
\begin{document}

\directlua{kpse.set_program_name("luatex") require "foo.lua" }

\begin{mplibcode}
    beginfig(1);
        a := runscript("getA()") ;
        draw fullcircle scaled a ; 
    endfig ;
\end{mplibcode}

\end{document}

up-to-date TL 2023

2
  • Not that it helps you, but maybe someone else could be interested to know of this context way of doing it. Maybe there is something similar for LaTeX.
    – mickep
    Commented Dec 20, 2023 at 9:42
  • yes, ConTeXt is no problem, but I didn't find the definition of the MP code inside ConTeXt.
    – user187802
    Commented Dec 20, 2023 at 9:52

1 Answer 1

2

This is possible: you just need a return statement in the string you pass to runscript, like so:

\begin{mplibcode}
    beginfig(1);
        a := runscript("return getA()") ;
        draw fullcircle scaled a ; 
    endfig ;
\end{mplibcode}

This draws a 2cm circle, as you intended.

2
  • Yes, that works. I didn't found it in the documentations.
    – user187802
    Commented Dec 20, 2023 at 11:00
  • The luamplib documentation is rather brief indeed. See the minim-mp manual for a longer discussion on the interaction between metapost and lua (minim-mp is a plain-tex-only metapost processor currently, but will support latex in TL2024).
    – renkema
    Commented Dec 20, 2023 at 11:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .