Luatex는 tex에서 lua로 다양한 양의 인수를 전달합니다(쉼표로 구분 및 이스케이프됨).

Luatex는 tex에서 lua로 다양한 양의 인수를 전달합니다(쉼표로 구분 및 이스케이프됨).

tex 명령에서 lua 함수로 여러 인수를 전달하는 동시에 이스케이프 처리하는 방법은 무엇입니까?

아니면 어떻게 수정하나요?

(수입)

\usepackage{luacode}
\newcommand{\example}[1]{
    \directlua{
        function debug(...)
            local arr = {...}
            for i, v in pairs(arr) do
                print(v)
            end
        end
        debug(#1)
    }
}

그렇게

\example{\notDefined, aNilValue, 5}

다음의 표준 출력을 생성합니다.

\notDefined
aNilValue
5

던지는 대신

  • 정의되지 않은 제어 순서(라텍스 오류)
  • 또는 변수가 aNilValue정의되지 않았기 때문에 아무것도 인쇄하지 않습니다.

\luastring{\unexpanded{...}}with를 사용해 보았지만 \docsvlist계속해서 논쟁이 벌어지고 있습니다.

편집하다 설명: 전달된 모든 인수는 문자열이어야 하므로 local arr = {...}예제에서는 동일해야 합니다.{"\\notDefined", "aNilValue", "5"}

답변1

공백을 더 잘 처리할 수 있지만 이렇게 하면 전체 목록의 루아 문자열이 만들어지고 쉼표로 분할되어 모든 항목이 문자열로 해석됩니다.

\newcommand{\example}[1]{%
    \directlua{
        function debug(s)
            for v in string.gmatch(s,'[^,]*') do
                print(v)
            end
        end
        debug("\luaescapestring{\detokenize{#1}}",",")
    }%
}

\typeout{}

\example{\notDefined, aNilValue, 5}

\stop

터미널 출력을 생성합니다


\notDefined 
 aNilValue
 5

답변2

이 솔루션은 LaTeX3의 쉼표로 구분된 목록을 사용합니다. 의 인수가 \example로그 파일에 기록됩니다.

\documentclass{article}
\usepackage{expl3}

\directlua{
  function debug(...)
      local arr = {...}
      for i, v in pairs(arr) do
          texio.write_nl(v)
      end
  end
}

\ExplSyntaxOn
\newcommand{\example}[1]{
  % construct comma separated list
  \clist_set:Nn \l_tmpa_clist {#1}
  % construct lua string for each component
  % and store them in a sequence
  \seq_clear:N \l_tmpa_seq
  \clist_map_inline:Nn \l_tmpa_clist {
    \str_set:Nn \l_tmpa_str {##1}
    \seq_put_right:Nx \l_tmpa_seq {"\luaescapestring{\l_tmpa_str}"}
  }
  \directlua{debug(\seq_use:Nn \l_tmpa_seq {,})}
}
\ExplSyntaxOff

\begin{document}
\example{\notDefined, aNilValue, 5}
\end{document}

관련 정보