
Eu sei que o TeX não pode escrever o conteúdo do hbox em um arquivo auxiliar (Analise novamente o conteúdo de um registro de caixa).
Isso significa que
\newwrite\foo
\immediate\openout\foo=\jobname.txt
\setbox0=\hbox{bar}
\immediate\write\foo{\box0}
não consigo escrever. Porém, o LuaTeX consegue fazer isso? eu encontrei
\directlua{
n = tex.getbox(0)
}
Mas não entendo o que n
está representando e se poderia usar para escrever o conteúdo da caixa em um arquivo.
Responder1
Editar: aqui está um novo código que funciona para ligaduras:
\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}
Resulta em hello.txt
:
Příliš žluťoučký kůň úpěl ďábelské ódy, diffierence, difference
Resposta original:
A variável n
no seu exemplo é uma lista de nós. Existem vários tipos de nós, como glyphs
para caracteres, glue
para espaçamento ou hlist
qual é o tipo que você obtém para seu arquivo \hbox
. hlist
contém nós filhos, que são acessíveis no n.head
atributo. Você pode então fazer um loop nesta lista secundária para glifos e colas.
Cada tipo de nó é distinguível pelo valor do n.id
atributo. Tipos de nós específicos e atributos possíveis são descritos no capítulo "8 Nós". Neste exemplo específico, precisamos processar apenas nós glyph
e glue
, mas você deve ter em mente que as listas de nós são recursivas e vários nós podem conter listas filho, como , hlist
, vlist
etc.nodeText
head
Em relação aos nós de glifo, char
o atributo contém valor unicode apenas no caso de você usar fontes opentype ou truetype, se você usar fontes antigas de 8 bits, ele contém apenas valores de 8 bits cuja codificação real depende da codificação de fonte usada e não é fácil para convertê-lo para 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
função retorna texto contido na lista de nós. É usado para imprimir \hbox
conteúdo no terminal e gravar no arquivo hello.txt
neste exemplo.
Para obter informações básicas sobre o estilo da fonte, você pode tentar usarl4 estilos de fontemódulo, assim:
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)