코루틴 [coroutine.yield] 대 [coroutine.result(name)]을 사용하여 Lua/LuaTeX 스크립트를 컴파일할 수 없습니다.

코루틴 [coroutine.yield] 대 [coroutine.result(name)]을 사용하여 Lua/LuaTeX 스크립트를 컴파일할 수 없습니다.

이 질문은 Lua/LuaTeX 스크립트에서 액세스하려는 기능인 Lua 코루틴을 다룹니다. 다음 스니펫은 통과할 인수가 있거나 없습니다(후자의 경우 이것이 제가 최종 목표로 원하는 것입니다).

Lua/LuaTeX 스크립트의 완전 초보자로서 저는 성공하지 못한 채 많은 시행착오를 겪었지만 전문가에게는 원래 오류가 확실히 분명할 것입니다. 여러 게시물과 문서를 읽은 후 코루틴을 선언하기 위해 명명된 함수를 통할지 여부라는 두 가지 방향을 사용할 수 있습니다. 이 두 가지 방식의 경우 문제는 coroutine.yieldand(및/또는) coroutine.results함수에서 발생하는 것 같습니다.

내가 본 LuaTeX의 몇 가지 예는 2010/2011년부터였습니다. 그 동안 약간의 수정이 있을 수 있습니다. 변경 시 무엇을 놓쳤나요? 그렇다면 내 코딩이 "더러워"진 이유는 무엇입니까? (지금은 디버깅하기가 조금 어렵습니다.)

코드 테스트

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

답변1

오류 메시지는 분명히 코루틴을 함수로 호출하고 있음을 의미합니다. 호출하면 coroutine.resume오류 없이 실행되지만 어떤 결과가 의도되는지 잘 모르겠습니다.

여기에 이미지 설명을 입력하세요

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

관련 정보