將 Lua 函式與 mplib 和 mpgraph 結合使用

將 Lua 函式與 mplib 和 mpgraph 結合使用

這個問題參考了這個問題Lua、Luamplib 和 Mpgraph 錯誤 我正在嘗試在 mplib 中使用 lua 函數。詳細代碼在這裡。

\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

您可以使用函數從 mplib 存取 Lua 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}

在此輸入影像描述

相關內容