
Aus Lerngründen versuche ich den LuaTeX-Code für Randnotizen zu verwenden (und zu verstehen) vonHierin LuaLaTeX.
Daher habe ich dieses minimale Setup vorgenommen:
\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}
Aber LuaLaTeX gibt an dieser \setbox
Zeile immer einen Fehler aus (in einer modifizierten Version tut LuaTeX das auch):
! Missing number, treated as zero.
<to be read again>
\marginnote_1
l.85 ...{Please do not fill this with too much text}
?
Ich verstehe nicht ganz, warum das passiert und wie man es lösen kann. Meine Frage ist also: Warum mag TeX das nicht \setbox
?
Antwort1
Es ist ein offensichtliches „Missing Space Syndrom“
\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%
}
Du hattest ein %
Danach 1
.
Außerdem brauchen Sie \usepackage{luatex85}
. Nach diesen Korrekturen habe ich
Was passiert? Das fehlende Leerzeichen nach der Konstanten 1
löst die Erweiterung des nächsten Tokens aus, das \expandafter
wiederum auslöst \csname
, sodass dasvorherigeWert von \notecount
wird verwendet.
Sie erhalten eine „fehlende Zahl“, da Boxregister tatsächlich ganze Zahlen sind; Ihr Makro hat sie zugewiesen \marginnote_0
und \setbox
sieht stattdessen \marginnote_1
.
SehenWann ist es schädlich, in einem \newcommand oder ähnlichem ein Prozentzeichen am Zeilenende hinzuzufügen?für mehr Informationen.