
Этот вопрос имеет отношение к вопросуОшибка с Lua, Luamplib и Mpgraph Я пытаюсь использовать функции lua внутри mplib. Подробный код здесь.
\documentclass{article}
\usepackage{luacode}
\usepackage{luamplib}
\begin{document}
\begin{luacode}
require("math")
local sin=math.sin;
\end{luacode}
\def\plot{%
\directlua{
luamplib.process_mplibcode([[
input graph ;
beginfig(0)
draw begingraph(5cm,3cm)
gdraw (0,sin(0)) for x = 1 upto 3: .. (x, sin(x)) endfor ;
endgraph ;
endfig ;
]])}%
}
\plot
\end{document}
Кажется, есть какая-то проблема со смешиванием lua и mplib. Где может быть проблема и как это можно исправить?
решение1
Вы можете получить доступ к Lua из mplib с помощью runscript
функции.
\documentclass{article}
\usepackage{luamplib}
\begin{document}
\def\plot{%
\directlua{luamplib.process_mplibcode([[
vardef sin primary x =
runscript("mp.print(math.sin(" & decimal x & "))")
enddef ;
input graph ;
beginfig(0)
draw begingraph(5cm,3cm)
gdraw (0,sin(0)) for x = 1 upto 3: .. (x, sin(x)) endfor ;
endgraph ;
endfig ;
]])}%
}
\plot
\end{document}