No se pueden compilar scripts Lua/LuaTeX con corrutinas [coroutine.yield] vs

No se pueden compilar scripts Lua/LuaTeX con corrutinas [coroutine.yield] vs

Esta pregunta trata sobre las corrutinas de Lua, una funcionalidad a la que quiero acceder en mis scripts de Lua/LuaTeX. Los siguientes fragmentos tienen o no tienen argumentos para aprobar (para este último, eso es lo que quiero como objetivo final).

Como principiante total con los scripts Lua/LuaTeX, hice muchas pruebas y errores sin éxito, pero el error original debe ser ciertamente obvio para los expertos. Después de leer varias publicaciones y documentaciones, hay dos orientaciones disponibles para declarar corrutinas: mediante función nombrada o no. Por estas dos formas de hacerlo, parece que el problema proviene de las funciones coroutine.yieldy (y/o) coroutine.results.

Los pocos ejemplos con LuaTeX que vi datan de 2010/2011; Es posible que haya algunas modificaciones mientras tanto. ¿Qué me he perdido en caso de cambio? Entonces, ¿por qué mi codificación está "sucia"? (Es un poco difícil depurarlo por el momento)

Prueba de código

\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}

Respuesta1

El mensaje de error aparentemente significa que estás llamando a la rutina como una función. Llamarlo vía coroutine.resumese ejecuta sin errores, pero no estoy seguro de qué resultado se pretende.

ingrese la descripción de la imagen aquí

\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}

información relacionada