Erro de setbox no LuaTeX

Erro de setbox no LuaTeX

Para aprender estou tentando usar (e entender) o código LuaTeX para notas marginais deaquiem LuaLaTeX.

Portanto fiz esta configuração 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}

Mas LuaLaTeX sempre lança um erro na \setboxlinha (LuaTeX também o faz em uma versão modificada):

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

? 

Não entendo completamente por que isso surge e como resolvê-lo. Então minha pergunta é: Por que o TeX não gosta disso \setbox?

Responder1

É uma manifesta “síndrome do espaço 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%
}

Você teve um %depois 1.

Você também precisa \usepackage{luatex85}. Depois dessas correções, consegui

insira a descrição da imagem aqui

O que está acontecendo? O espaço faltante após a constante 1aciona a expansão do próximo token, que é \expandafter, que por sua vez aciona \csname, então oanterioro valor de \notecounté usado.

Você obtém “número faltante” porque os registradores de caixa são, na verdade, inteiros; sua macro alocada \marginnote_0e, \setboxem vez disso, vê \marginnote_1.

VerQuando é prejudicial adicionar caracteres de porcentagem no final das linhas em um \newcommand ou similarPara maiores informações.

informação relacionada