# Insert LaTeX code at line break

Is there a way to insert arbitrary (La)TeX-code at the line break of a typeset line?

I am aware that this isn't really possible with (La)TeX as such. But looking at all the interesting chickenize-features applied on a line-basis, I started wondering whether LuaTeX could facilitate a simple mechanism to do this. To illustrate: I'd like the box with the time to be typeset right after "nulla":

\documentclass{article}

\usepackage{datetime}

\newcommand{\doatlinebreak}[1]{#1}

\begin{document}

Vivamus pharetra posuere sapien. Nam consectetuer.
Sed aliquam, nunc eget euismod ullamcorper, lectus nunc
ullamcorper orci, fermentum bibendum enim nibh eget ipsum.
Donec porttitor ligula eu
dolor. \doatlinebreak{\makebox[0pt][l]{\currenttime}}Maecenas
vitae nulla consequat libero cursus venenatis.

\end{document}

• Where do you want the box to be placed? Within the text area (influencing the line break) or somewhere in the margin/next to the line break? – TeXnician Sep 23 '18 at 8:37
• The box is probably not a good example anyway... I am hoping for a very general solution, but for my specific purpose (documenting the actual linebreaks of a typeset text) I'd need a hook after the last word that TeX decides to put on the specific line where the command is issued. So theoretically my commands would be influencing the line breaks but practically I'd put it in a zero-width box to avoid just that ;) – Florian Sep 23 '18 at 8:50
• As far as I understand it, that's not how it works. Here's a link to the available callbacks which contains the callbacks linebreak_filter and post_linebreak_filter. You should be aware that you are working on the level of nodes and that you might want to insert a whatsit and not some "zero-width" box. See also some examples at wiki.luatex.org/index.php/Post_linebreak_filter and wiki.luatex.org/index.php/Show_the_hyphenation_points. – TeXnician Sep 23 '18 at 8:58
• the post linebreak filter is called once for the whole paragraph so any time added will be the same for all the lines in a paragraph is that what you intend? – David Carlisle Sep 23 '18 at 9:33
• @DavidCarlisle thanks, I didn't know that, even though it makes sense thinking about it. Actually also the time is only an example where I put in a command off the shelf for the sake of simplicity. In the end I'd like to build a command that puts in a marker for the actual line breaks (in an ideal solution also with the line numbers) so that I can export it together with the text having documented the linebreaks of the printed edition. I tried to simplify my problem to a general case but obviously it would have been clearer to ask to what I am trying to achieve in the end... – Florian Sep 23 '18 at 9:56

This might get you started it inserts a glyph node for A at the end of every line

\documentclass{article}

\directlua{
%
function my_post_lb_filter(h,c)
local cntr=0

for n in node.traverse(h) do
% pull out the boxes and add A
if n.id==0 then
local  newn= node.new(node.id("glyph"))
newn.font=font.current()
newn.char=65 % this is a single character box, for A
node.insert_after(n.list,node.tail(n.list),newn)
end
%
end
return h
end
}

\begin{document}

Vivamus pharetra posuere sapien. Nam consectetuer.
Sed aliquam, nunc eget euismod ullamcorper, lectus nunc
ullamcorper orci, fermentum bibendum enim nibh eget ipsum.
Donec porttitor ligula eu
dolor. Maecenas
vitae nulla consequat libero cursus venenatis.

\end{document}


No need for LuaTeX. Simply use \marginpar.

\documentclass{article}

\usepackage{datetime}

\begin{document}

Vivamus pharetra posuere sapien. Nam consectetuer.
Sed aliquam, nunc eget euismod ullamcorper, lectus nunc
ullamcorper orci, fermentum bibendum enim nibh eget ipsum.
Donec porttitor ligula eu
dolor. \marginpar{\currenttime}Maecenas
vitae nulla consequat libero cursus venenatis.

\end{document}


Alternatively, using the answer to question 14.28 of the TeXbook:

\documentclass{article}

\usepackage{datetime}

\newcommand{\doatlinebreak}[1]{%
\kern-\dp\strutbox
\vtop to \dp\strutbox{
\baselineskip=\dp\strutbox
}
}%
}

\begin{document}

Vivamus pharetra posuere sapien. Nam consectetuer.
Sed aliquam, nunc eget euismod ullamcorper, lectus nunc
ullamcorper orci, fermentum bibendum enim nibh eget ipsum.
Donec porttitor ligula eu
dolor. \doatlinebreak{\currenttime}Maecenas
vitae nulla consequat libero cursus venenatis.

\end{document}