
我知道TeX無法將hbox的內容寫入輔助檔案(重新解析盒子寄存器的內容)。
這意味著
\newwrite\foo
\immediate\openout\foo=\jobname.txt
\setbox0=\hbox{bar}
\immediate\write\foo{\box0}
寫不了 但是LuaTeX可以嗎?我已經發現
\directlua{
n = tex.getbox(0)
}
但我不明白它n
代表什麼,以及我是否可以用它來將框內容寫入文件中。
答案1
編輯:這是適用於連字的新程式碼:
\documentclass{article}
\usepackage{fontspec}
\begin{document}
\setbox0=\hbox{Příliš žluťoučký \textit{kůň} úpěl \hbox{ďábelské} ódy, diffierence, difference}
\directlua{
% local fontstyles = require "l4fontstyles"
local char = unicode.utf8.char
local glyph_id = node.id("glyph")
local glue_id = node.id("glue")
local hlist_id = node.id("hlist")
local vlist_id = node.id("vlist")
local disc_id = node.id("disc")
local minglue = tex.sp("0.2em")
local usedcharacters = {}
local identifiers = fonts.hashes.identifiers
local function get_unicode(xchar,font_id)
local current = {}
local uchar = identifiers[font_id].characters[xchar].tounicode
for i= 1, string.len(uchar), 4 do
local cchar = string.sub(uchar, i, i + 3)
print(xchar,uchar,cchar, font_id, i)
table.insert(current,char(tonumber(cchar,16)))
end
return current
end
local function nodeText(n)
local t = {}
for x in node.traverse(n) do
% glyph node
if x.id == glyph_id then
% local currentchar = fonts.hashes.identifiers[x.font].characters[x.char].tounicode
local chars = get_unicode(x.char,x.font)
for _, current_char in ipairs(chars) do
table.insert(t,current_char)
end
% glue node
elseif x.id == glue_id and node.getglue(x) > minglue then
table.insert(t," ")
% discretionaries
elseif x.id == disc_id then
table.insert(t, nodeText(x.replace))
% recursivelly process hlist and vlist nodes
elseif x.id == hlist_id or x.id == vlist_id then
table.insert(t,nodeText(x.head))
end
end
return table.concat(t)
end
local n = tex.getbox(0)
print(nodeText(n.head))
local f = io.open("hello.txt","w")
f:write(nodeText(n.head))
f:close()
}
\box0
\end{document}
導致hello.txt
:
Příliš žluťoučký kůň úpěl ďábelské ódy, diffierence, difference
原答案:
您的範例中的變數n
是一個節點清單。存在各種類型的節點,例如glyphs
字元節點、glue
間距節點,或hlist
您為\hbox
.hlist
包含子節點,可以在n.head
屬性中存取這些子節點。然後,您可以循環此子列表以獲取字形和粘合。
每個節點類型都可以透過n.id
屬性值來區分。特定的節點類型和可能的屬性在「8 節點」一章中描述。在這個特定的範例中,我們只需要處理glyph
和節點,但您應該記住,節點列表是遞歸glue
的,並且各種節點可以包含子列表,例如hlist
、等。vlist
支持它們。nodeText
head
關於字形節點,char
屬性僅在使用 opentype 或 truetype 字體的情況下才包含 unicode 值,如果使用舊的 8 位元字體,它只包含 8 位元值,實際編碼取決於使用的字體編碼,這並不容易將其轉換為unicode。
\documentclass{article}
\usepackage{fontspec}
\begin{document}
\setbox0=\hbox{Příliš žluťoučký \textit{kůň} úpěl \hbox{ďábelské} ódy}
\directlua{
local fontstyles = require "l4fontstyles"
local char = unicode.utf8.char
local glyph_id = node.id("glyph")
local glue_id = node.id("glue")
local hlist_id = node.id("hlist")
local vlist_id = node.id("vlist")
local minglue = tex.sp("0.2em")
local usedcharacters = {}
local identifiers = fonts.hashes.identifiers
local function get_unicode(xchar,font_id)
return char(tonumber(identifiers[font_id].characters[xchar].tounicode,16))
end
local function nodeText(n)
local t = {}
for x in node.traverse(n) do
% glyph node
if x.id == glyph_id then
% local currentchar = fonts.hashes.identifiers[x.font].characters[x.char].tounicode
table.insert(t,get_unicode(x.char,x.font))
local y = fontstyles.get_fontinfo(x.font)
print(x.char,y.name,y.weight,y.style)
% glue node
elseif x.id == glue_id and node.getglue(x) > minglue then
table.insert(t," ")
elseif x.id == hlist_id or x.id == vlist_id then
table.insert(t,nodeText(x.head))
end
end
return table.concat(t)
end
local n = tex.getbox(0)
print(nodeText(n.head))
local f = io.open("hello.txt","w")
f:write(nodeText(n.head))
f:close()
}
\box0
\end{document}
nodeText
函數傳回節點清單中包含的文字。在本例中,它用於將\hbox
內容列印到終端並寫入文件。hello.txt
有關字體樣式的基本信息,您可以嘗試使用l4字體樣式模組,像這樣:
local fontstyles = require "l4fontstyles"
...
if x.id == glyph_id then
table.insert(t,char(x.char))
local y = fontstyles.get_fontinfo(x.font)
print(y.name,y.weight,y.style)