
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)