1

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?

enter image description here

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.

  • 1
    The lineno package uses \interlinepenalty=-10000 and modifies the \output routine (Aaagh!). One can also put each paragraph into a savebox and extract one line at a time using \vsplit (see tex.stackexchange.com/questions/442258/…). – John Kormylo Nov 1 '18 at 11:52
  • Very interesting, @JohnKormylo, thank you! This even gives an easy way to do very nice things, as for example line numbering, by adding \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 \thepage returns a correct value as in my example. I think we're kind of stuck here, aren't we? – Robert Alessi Nov 1 '18 at 13:28
  • it isn't really clear what you what to insert or when, you only execute \stepcounter once 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 \thepage added in comments has the same issue it always has that \thepage will have the value at point the paragraph is broken in to lines (so the same value for the whole paragraph) – David Carlisle Nov 1 '18 at 13:47
  • Thank you for your comments, @DavidCarlisle. As a matter of fact, I didn't pay attention to the boxing of the command incrementing the counter—sorry for this mistake—as my interest was actually in getting a correct value of \thepage through post_linebreak_filter together 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 1 '18 at 14:09
  • 1
    even if you split the paragraph in to separate one-line paragraphs you can not know what page a bit of text finally lands on until (for example) all floats have been positioned, as inserting a top float on the page may push the current line to a later page, so no purely local setting can do this, you need the classic two-pass approach using \pageref or 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 1 '18 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.