LuaTeX 中的 Setbox 错误

LuaTeX 中的 Setbox 错误

为了学习,我尝试使用(并理解)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 或类似命令中,在行尾添加百分号什么时候有害了解更多信息。

相关内容