6

For the sake of learning I'm trying to use (and understand) the LuaTeX code for marginal notes from here in LuaLaTeX.

Therefore I made this minimal setup:

\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}

But LuaLaTeX always throws an error at the \setbox line (LuaTeX does in a modified version as well):

! Missing number, treated as zero.
<to be read again> 
\marginnote_1 
l.85 ...{Please do not fill this with too much text}

? 

I do not fully understand why this comes up and how to solve it. So my question is: Why does TeX not like that \setbox?

1 Answer 1

7

It's a manifest “missing space syndrome”

\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%
}

You had a % after 1.

Also you need \usepackage{luatex85}. After these fixes, I got

enter image description here

What's happening? The missing space after the constant 1 triggers expansion of the next token, which is \expandafter, which in turn triggers \csname, so the previous value of \notecount is used.

You get “missing number” because box registers are actually integers; your macro allocated \marginnote_0 and \setbox instead sees \marginnote_1.

See When is it harmful to add percent character at end of lines in a \newcommand, or similar for more information.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .