This is a follow-up question to David Carlisle's answer here (Insert LaTeX code at line break).
In addition to glyph nodes, is there a way to insert (almost) arbitrary LaTeX commands with the help of post_linebreak_filter?
For example, is there a way to insert auto-incremented counters, or even ‘cut into slices’ paragraphs as in the following 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}
As this mwe shows, the counter in myhooki is inserted, but not incremented. And I didn't figure out a way of inserting either cmds["cmdi"] or cmds["cmdii"]. Am I right in thinking there is no way to do this?
EDIT
Please note that in the mwe above, as @DavidCarlisle pointed out in his comment below, the value of countlines cannot be incremented from within a box. This was a mistake on my part.

\makebox[2em][l]{#1}just above\parbox[b]{\textwidth}{\unvbox0}(I am referring to your answer), then saying\begin{myenv}{\cntln}(with just one argument). However, one cannot use this to cut paragraphs into slices so that\thepagereturns a correct value as in my example. I think we're kind of stuck here, aren't we? – Robert Alessi Nov 01 '18 at 13:28\stepcounteronce so clearly that counter will not change (a box contains a node list not tex instructions) You could arrange to increment the counter each time your hook is called and re-set the box, but also the code posted is in the linebreak filter (so prior to page breaking) so your extra question about\thepageadded in comments has the same issue it always has that\thepagewill have the value at point the paragraph is broken in to lines (so the same value for the whole paragraph) – David Carlisle Nov 01 '18 at 13:47\thepagethroughpost_linebreak_filtertogether with some “cutting paragraphs into slices” mechanism called at the breaking point of each line. Should we consider this impossible? In my opinion, given how TeX works with paragraphs and page-breaking, I think we should. I just wanted to make sure of that. – Robert Alessi Nov 01 '18 at 14:09\pagerefor in luatex you could conceivably internalise the second pass by making each page be shipped to some internal box store rather than be shipped out then navigate over those node lists in a second pass before shipping out the final document. – David Carlisle Nov 01 '18 at 14:19