Lua/LuaTeX-Skripte können mit Coroutinen [coroutine.yield] vs [coroutine.result(name)] nicht kompiliert werden.

Lua/LuaTeX-Skripte können mit Coroutinen [coroutine.yield] vs [coroutine.result(name)] nicht kompiliert werden.

Diese Frage beschäftigt sich mit Lua-Coroutinen, einer Funktionalität, auf die ich in meinen Lua/LuaTeX-Skripten zugreifen möchte. Die folgenden Snippets sind ohne oder mit zu übergebendem Argument (für Letzteres ist das mein Endziel).

Als absoluter Anfänger in Sachen Lua/LuaTeX-Skripte habe ich viel erfolglos ausprobiert, aber der ursprüngliche Fehler muss für Experten offensichtlich sein. Nach dem Lesen mehrerer Beiträge und Dokumentationen stehen zwei Möglichkeiten zur Verfügung, um Coroutinen zu deklarieren: über eine benannte Funktion oder nicht. Bei diesen beiden Vorgehensweisen scheint das Problem von den coroutine.yieldund (und/oder) coroutine.results-Funktionen herzurühren.

Die wenigen Beispiele mit LuaTeX, die ich gesehen habe, stammen aus den Jahren 2010/2011; möglicherweise hat es inzwischen einige Änderungen gegeben. Was habe ich im Falle von Änderungen übersehen? Warum ist mein Code also „schmutzig“? (Derzeit ist er für mich etwas schwer zu debuggen.)

Code-Tests

\documentclass{article}

\usepackage{luacode}

\begin{luacode}
-- Main function of the coroutine
--function luadisplay (argval)
--  tex.print("..argval..")
function luadisplay ()
  for i = 0, 1, 1 do
    if i==0 then
      tex.print{"OK0"}
      tex.print(coroutine.yield())
    end
    if i==1 then
      tex.print{"OK1"}
      tex.print(coroutine.yield())
    end
  end
end
-- Creating coroutine
luacoroutine = coroutine.create(luadisplay)
-- Starting and obtaining results
--tex.print("coroutine.resume(luacoroutine)") -- Does not work if "coroutine.yield" uncommented
\end{luacode}

\begin{luacode} -- That's compile but can't obtain result
  -- Creating the damned coroutine directly (without argument)
  myco = coroutine.create( function()
    for i = 0, 1, 1 do
      if i==0 then
        tex.print{"OK0"}
        tex.print(coroutine.yield())
      end
      if i==1 then
        tex.print{"OK1"}
        tex.print(coroutine.yield())
      end
      coroutine.resume(myco)
    end
  end)
\end{luacode}

%\directlua{require("luatexse.lua")} % If external file

%\newcommand{\luacoroutinecmd}{\directlua{luacoroutine()}} -- Without argument not works
%\newcommand{\luacoroutinecmd}[1]{\directlua{luacoroutine("#1")}} -- With argument not works

%\newcommand{\mycocmd}{\directlua{myco()}} -- Without argument not works

\parindent=0pt

\begin{document}

Is it work?

\bigskip\bigskip

%\luacoroutinecmd{Hi!}

\end{document}

Antwort1

Die Fehlermeldung bedeutet anscheinend, dass Sie die Coroutine als Funktion aufrufen. Der Aufruf über coroutine.resumeläuft ohne Fehler, aber ich bin nicht sicher, welches Ergebnis beabsichtigt ist.

Bildbeschreibung hier eingeben

\documentclass{article}

\usepackage{luacode}

\begin{luacode}
-- Main function of the coroutine
--function luadisplay (argval)
--  tex.print("..argval..")
function luadisplay (m)
  for i = 0, 1, 1 do
    if i==0 then
      tex.print{"OK0[" .. m .. "]"}
      tex.print(coroutine.yield())
    end
    if i==1 then
      tex.print{"OK1[" .. m .. "]"}
      tex.print(coroutine.yield())
    end
  end
end
-- Creating coroutine
luacoroutine = coroutine.create(luadisplay)
-- Starting and obtaining results
--tex.print("coroutine.resume(luacoroutine)") -- Does not work if "coroutine.yield" uncommented
\end{luacode}


\newcommand{\luacoroutinecmd}[1]{\directlua{coroutine.resume(luacoroutine,"#1")}} %-- With argument not works


\parindent=0pt

\begin{document}

Is it work?

\bigskip\bigskip

A \luacoroutinecmd{Hi!}

\bigskip\bigskip

B \luacoroutinecmd{Hi!}

\bigskip\bigskip

C \luacoroutinecmd{Hi!}

\end{document}

verwandte Informationen