
Para aprender, estoy tratando de usar (y comprender) el código LuaTeX para notas marginales deaquíen LuaLaTeX.
Por lo tanto hice esta configuración mínima:
\documentclass{article}
\usepackage{luatexbase}
\directlua{
local HLIST = node.id("hlist")
local RULE = node.id("rule")
local GLUE = node.id("glue")
local KERN = node.id("kern")
local WHAT = node.id("whatsit")
local COL = node.subtype("pdf_colorstack")
mark_lines = function (head)
for mark in node.traverse_id(WHAT, head) do
local attr = node.has_attribute(mark, 100)
if attr then
local item = mark.next
while item do
if item.id == HLIST then
node.set_attribute(item, 100, attr)
item = nil
else
item = item.next
end
end
head = node.remove(head, mark)
end
end
return head
end
process_marginalia = function (head)
local remainingheight, first, item = 0, true, node.slide(head)
while item do
if node.has_field(item, "kern") then
if not first then
remainingheight = remainingheight + item.kern
end
elseif node.has_field(item, "spec") then
if not first then
remainingheight = remainingheight + item.spec.width
end
elseif node.has_field(item, "height") then
if first then
first = false
else
remainingheight = remainingheight + item.depth
end
local attr = node.has_attribute(item, 100)
if attr then
local note = node.copy(tex.box[attr])
local upward = note.depth - node.tail(note.list).depth
if upward > remainingheight then
upward = remainingheight - upward
else
upward = 0
end
local kern = node.new(KERN, 1)
kern.kern = upward - note.height - item.depth
node.insert_before(note.list, note.list, kern)
note.list = kern
note.height, note.depth = 0, 0
node.insert_after(head, item, note)
note.shift = tex.hsize + tex.sp("1em")
first = true
remainingheight = upward
else
remainingheight = remainingheight + item.height
end
end
item = item.prev
end
end
luatexbase.add_to_callback("post_linebreak_filter", mark_lines, "mark_lines")
}
\newcount\notecount
\def\note#1{%
\advance\notecount 1%
\expandafter\newbox\csname marginnote_\the\notecount\endcsname%
\expandafter\setbox\csname marginnote_\the\notecount\endcsname=\vtop{\hsize=4cm\rightskip=0pt plus 1fil\noindent\it #1}%
\bgroup\attribute100=\expandafter\the\csname marginnote_\the\notecount\endcsname\vadjust pre {\pdfliteral{}}\egroup%
}
\begin{document}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.\note{Please do not fill this with too much text}
\output{\directlua{process_marginalia(tex.box[255].list)}\shipout\box255}
\end{document}
Pero LuaLaTeX siempre arroja un error en la \setbox
línea (LuaTeX también lo hace en una versión modificada):
! Missing number, treated as zero.
<to be read again>
\marginnote_1
l.85 ...{Please do not fill this with too much text}
?
No entiendo del todo por qué surge esto y cómo solucionarlo. Entonces mi pregunta es: ¿Por qué a TeX no le gusta eso \setbox
?
Respuesta1
Es un manifiesto “síndrome del espacio perdido”
\def\note#1{%
\advance\notecount 1
\expandafter\newbox\csname marginnote_\the\notecount\endcsname%
\expandafter\setbox\csname marginnote_\the\notecount\endcsname=\vtop{\hsize=4cm\rightskip=0pt plus 1fil\noindent\it #1}%
\bgroup\attribute100=\expandafter\the\csname marginnote_\the\notecount\endcsname\vadjust pre {\pdfliteral{}}\egroup%
}
Tuviste un %
después 1
.
También necesitas \usepackage{luatex85}
. Después de estas correcciones, obtuve
¿Lo que está sucediendo? El espacio que falta después de la constante 1
desencadena la expansión del siguiente token, que es \expandafter
, que a su vez desencadena\csname
, por lo que elanterior\notecount
Se utiliza el valor de .
Obtiene un "número faltante" porque los registros de cuadro son en realidad números enteros; su macro se asignó \marginnote_0
y \setbox
en su lugar ve \marginnote_1
.
Ver¿Cuándo es perjudicial agregar un carácter de porcentaje al final de las líneas en un \newcommand o similar?para más información.