Lua에서 토큰 목록 작성

Lua에서 토큰 목록 작성

TeX에서는 '쓰기' 노드가 다음과 같이 삽입됩니다.

\write1{\string\doit{\the\lastypos}}

순수 luatex를 사용하면 다음을 사용하여 노드를 만들 수 있습니다.

local n = node.new(8, 1)
n.stream = 1
n.data = <token-list>

매뉴얼에 따르면, <token-list>작성될 토큰 목록(트리플릿 목록 포함)을 나타내는 테이블입니다. 이 목록이 어떻게 작성되는지에 대한 문서를 찾을 수 없습니다. 문자열이 허용되지만 문자열로 된 문자 목록(\meaning과 유사)으로 변환되므로 \the\lastypos평가되지 않고 그대로 작성됩니다.

다음 코드에 표시된 해결 방법을 찾았습니다.

\setbox0\hbox{\write1{\the\lastxpos}}

\directlua{

  for _,d in ipairs(tex.box[0].head.data) do
    texio.write(' ** ' .. d[1] .. '/' .. d[2] .. '/' .. d[3])
  end

}

a로 상자를 정의한 \write다음 노드를 검사합니다. 실제 코드에서는 인쇄하는 대신 이를 전달 n.data하고 프리미티브가 예상대로 작동합니다(사용자 정의 매크로에 일부 문제가 있음).

내 질문은 필드에 피드를 제공하기 위해 lua에서 토큰 목록을 생성하는 방법입니다 data. [편집하다. 내 질문은 에 관한 것이 아니라 \lastypos해당 필드에 대한 임의의 토큰 목록 작성에 관한 것입니다 data. 또한 TeX의 비동기 특성으로 인해 'write' 노드가 생성될 때 페이지 번호 등은 알 수 없으며 'write'가 실제로 출력될 때만 알 수 있습니다.]

다음은 extra.lua라는 이름의 lua 파일을 사용하여 몇 가지 실험을 수행할 수 있는 라텍스 파일입니다.

\documentclass{article}

\def\donothing#1{}

\directlua{
  require'extra'
}

\setbox0\hbox{\write1{\string\donothing{\the\lastypos}}}

\begin{document}

\directlua{

for _,d in ipairs(tex.box[0].head.data) do
  texio.write(' +++ ' .. d[1] .. '/' .. d[2] .. '/' .. d[3])
end

}

\copy0
\copy0
\copy0

\end{document}

루아 파일:

local n = node.new(8, 1)
n.stream = 1
n.data =  'abcd#&\\the\\lastxpos' 

for _,d in ipairs(n.data) do
  texio.write(' *** ' .. d[1] .. '/' .. d[2] .. '/' .. d[3])
end

답변1

token.createLuaTeX에는 토큰 uservalue를 생성하는 기능이 있습니다 . 테이블에 넣어서 토큰 목록으로 결합할 수 있습니다. 이를 위해서는 \string\donothing{\the\lastvpos}다음과 같습니다:

tl = {
  token.create'string',
  token.create'donothing',
  token.create(string.byte'{'),
    token.create'the',
    token.create'lastypos',
  token.create(string.byte'}')
}

일반적으로 LuaTeX 문서에서 토큰 목록에 대한 참조는 이러한 종류의 테이블을 의미하지만 다른 종류, 즉 숫자 테이블 테이블이 필요합니다. 이러한 숫자를 찾는 것은 쉽지 않지만 위 형식의 토큰 목록을 다른 형식으로 변환할 수 있습니다 {0, v.tok}.v.tok

\directlua{
local function convert_tl(list)
  local new = {}
  for i,v in ipairs(list) do
    new[i] = {0, v.tok}
  end
  return new
end

local n = node.new(8, 1)
n.stream = 3
n.data = convert_tl{
  token.create'string',
  token.create'donothing',
  token.create(string.byte'{'),
    token.create'the',
    token.create'lastypos',
  token.create(string.byte'}')
}

tex.box[0] = node.hpack(n)
}
\copy0
\copy0

결과는 출력

\donothing{0}
\donothing{0}

관련 정보