在换行符处插入 LaTeX 代码(后续)

在换行符处插入 LaTeX 代码(后续)

这是大卫·卡莱尔的回答此处(在换行符处插入 LaTeX 代码)

除了字形节点之外,是否有办法借助插入(几乎)任意的 LaTeX 命令post_linebreak_filter

例如,有没有办法插入自动递增计数器,甚至像下面的 mwe 一样“切成片”的段落?

\documentclass{article}

\usepackage{lipsum}
\usepackage{luacode}

\newcounter{countlines}
\newcommand{\cntln}{\thecountlines\stepcounter{countlines}}

\newbox\myhooki
\setbox\myhooki=\hbox{\stepcounter{countlines}\thecountlines}

\begin{luacode}
-- Is there a way to insert any of the following commands?

cmds = {cmdi="\\textbf{Uh?}",
        cmdii="\\bgroup\\parfillskip=0pt\\par\\noindent\\parskip=0pt\\egroup"}

function my_post_lb_filter(h,c)
  local cntr=0

  for n in node.traverse(h) do
    if n.id==0 then
    node.insert_after(n.list,node.tail(n.list),node.copy(tex.box.myhooki))
    end
  end
return h
end

luatexbase.add_to_callback('post_linebreak_filter', my_post_lb_filter, 'add A at eol')

\end{luacode}

\begin{document}

\lipsum[1-4]

Quo usque tandem abutere, Catilina, patientia nostra?  quam diu etiam
furor iste tuus nos eludet? quem ad finem sese effrenata iactabit
audacia? Nihilne te nocturnum praesidium Palati, nihil urbis vigiliae,
nihil timor populi, nihil concursus bonorum omnium, nihil hic
munitissimus habendi senatus locus, nihil horum ora voltusque
moverunt? Patere tua consilia non sentis, constrictam iam horum omnium
scientia teneri coniurationem tuam non vides? Quid proxima, quid
superiore nocte egeris, ubi fueris, quos convocaveris, quid consili
ceperis quem nostrum ignorare arbitraris? O tempora, o mores! Senatus
haec intellegit, consul videt; hic tamen vivit. Vivit? immo vero etiam
in senatum venit, fit publici consili particeps, notat et designat
oculis ad caedem unum quemque nostrum. Nos autem fortes viri satis
facere rei publicae videmur, si istius furorem ac tela vitamus. Ad
mortem te, Catilina, duci iussu consulis iam pridem oportebat, in te
conferri pestem quam tu in nos omnis iam diu machinaris.
\textbf{(This is page \thepage, is it?)}
\luadirect{tex.sprint(cmds["cmdi"])}
The following line starts a new paragraph\ldots
\luadirect{tex.sprint(cmds["cmdii"])}%
even if it doesn't seem so.
\textbf{(This is page \thepage, is it not?)}
And now, let us continue as if nothing had happened.
And now, let us continue as if nothing had happened.
And now, let us continue as if nothing had happened.
And now, let us continue as if nothing had happened.

\end{document}

正如这个 mwe 所示,计数器已插入,但未增加。而且我还没想出插入或 的myhooki方法。我认为没有办法做到这一点,对吗?cmds["cmdi"]cmds["cmdii"]

在此处输入图片描述

编辑 请注意,在上面的 mwe 中,正如 @DavidCarlisle 在下面的评论中指出的那样,的值countlines不能在框内增加。这是我的一个错误。

相关内容