
Ради обучения я пытаюсь использовать (и понимать) код LuaTeX для заметок на полях изздесьв LuaLaTeX.
Поэтому я сделал такую минимальную настройку:
\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}
Но LuaLaTeX всегда выдает ошибку в \setbox
строке (LuaTeX делает то же самое в модифицированной версии):
! Missing number, treated as zero.
<to be read again>
\marginnote_1
l.85 ...{Please do not fill this with too much text}
?
Я не совсем понимаю, почему это происходит и как это решить. Поэтому мой вопрос: почему TeX это не нравится \setbox
?
решение1
Это явный «синдром нехватки пространства».
\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%
}
У тебя было %
после 1
.
Также вам нужно \usepackage{luatex85}
. После этих исправлений у меня получилось
Что происходит? Отсутствующий пробел после константы 1
запускает расширение следующего токена, который \expandafter
в свою очередь запускает \csname
, поэтомупредыдущий\notecount
используется значение .
Вы получаете «пропущенное число», потому что регистры ящика на самом деле являются целыми числами; ваш макрос выделил память \marginnote_0
и \setbox
вместо этого видит \marginnote_1
.
ВидетьКогда вредно добавлять символ процента в конце строк в \newcommand или подобномЧтобы получить больше информации.