在 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」實際輸出時才知道。
這裡有一個latex檔案來做一些實驗,其中有一個名為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}
lua文件:
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
LuaTeX 具有token.create
創建代幣用戶值的功能。透過將它們放入表中,可以將它們組合成令牌列表。對於\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}