
나는 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 노드" 장에 설명되어 있습니다. 이 특정 예에서는 just glyph
및 노드를 처리해야 하지만 노드 목록은 재귀적이며 다양한 노드에 , 등 glue
의 하위 목록이 포함될 수 있다는 점을 명심해야 합니다. 현재 노드 속성 에 대한 재귀 호출을 통해 이를 지원할 수 있습니다 .hlist
vlist
nodeText
head
글리프 노드의 경우 char
속성에는 오픈타입 또는 트루타입 글꼴을 사용하는 경우에만 유니코드 값이 포함되며, 이전 8비트 글꼴을 사용하는 경우 실제 인코딩은 사용된 글꼴 인코딩에 따라 달라지는 8비트 값만 포함하므로 쉽지 않습니다. 유니코드로 변환하려면
\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)