直接使用 node.write() 构建节点时出现多页输出的问题

直接使用 node.write() 构建节点时出现多页输出的问题

平均能量损失

麦格

\directlua{tex.enableprimitives('',tex.extraprimitives())}
\output={\shipout\box255}
\pagewidth=210mm
\pageheight=2in
\hoffset=1in
\voffset=1in
\directlua{dofile("mwe.lua")}
\end

mwe.lua

tex.outputmode = 1

-- Build a simple paragraph node from given text. This code does not do any complex shaping etc.
--
-- adapted from: http://tex.stackexchange.com/questions/114568/can-i-create-a-node-list-from-some-text-entirely-within-lua
local function text_to_paragraph(text)
  local current_font = font.current()
  local font_params = font.getfont(current_font).parameters

  local para_head = node.new("local_par")

  local last = para_head

  local indent = node.new("hlist",3)
  indent.width = tex.parindent
  indent.dir = "TRT"
  last.next = indent
  last = indent

  for c in text:gmatch"." do  -- FIXME use utf8 lib
    local v = string.byte(c)
    local n
    if v < 32 then
      goto skipchar
    elseif v == 32 then
      n = node.new("glue",13)
      node.setglue(n, font_params.space, font_params.space_shrink, font_params.space_stretch)
    else
      n = node.new("glyph", 1)
      n.font = current_font
      n.char = v
      n.lang = tex.language
      n.uchyph = 1
      n.left = tex.lefthyphenmin
      n.right = tex.righthyphenmin
    end
    last.next = n
    last = n
    ::skipchar::
  end

  -- now add the final parts: a penalty and the parfillskip glue
  local penalty = node.new("penalty", 0)
  penalty.penalty = 10000

  local parfillskip = node.new("glue", 14)
  parfillskip.stretch = 2^16
  parfillskip.stretch_order = 2

  last.next = penalty
  penalty.next = parfillskip

  node.slide(para_head)
  return para_head
end

local content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

for i = 1,3 do
  local head = text_to_paragraph(content)
  -- Break the paragraph into vertically stacked boxes
  local vbox = tex.linebreak(head, { hsize = tex.hsize })
  node.write(vbox)
  node.write(node.copy(tex.parskip))
  node.write(node.copy(tex.baselineskip))
  print("PAGE TOTAL " .. tex.pagetotal)
end

输出

请注意,它被截断了。我原本以为会出现分页符,其余内容会移至下一页。

输出不佳

注意事项/问题

这个想法是完全在 Lua 中创建段落节点,然后使用 将它们提供给 LuaTeX node.write()

  • 我通过执行以下操作用默认输出例程覆盖 Plain TeX 输出例程:\output={\shipout\box255}

  • tex.pagetotal参数似乎不会自动增加。不确定这是否是问题的一部分

  • 我猜想另一种方法是自己填充整个盒子并使用将其发送出去tex.shipout,但我希望练习 TeX 内部的页面构建例程以在正确的位置中断页面,而不是自己做。

我怎样才能解决这个问题?

答案1

事实证明,这是我的一个相当愚蠢的疏忽。我只是调整了\pageheight\vsize使用的参数是 Plain TeX 宏中设置的参数(即 8.9 英寸)。如果我将 的大小更改\vsize为略小于\pageheight,那么一切就正常了。

\directlua{tex.enableprimitives('',tex.extraprimitives())}
\output={\shipout\box255}
\pagewidth=210mm
\pageheight=2in
\vsize=1in
\hoffset=1in
\voffset=1in
\directlua{dofile("mwe.lua")}
\end

相关内容