After playing with the answer the question here, I noticed some differences in insert_after and insert_before. node.insert_after adds contents to right margin, node.insert_before doesn't seem to add to left margin. Seems like they both grow as if the contents were placed starting at left margin. The question is how to grow the contents of the hlist n into the left margin (using luatex node library) instead of pushing the main box in right margin.
A not in left margin:
A in right margin:
In the following code, I have commented 3 different insert recipes. Uncomment them one by one to see the effect. Interestingly Append-2 recipe (insert_before) doesn't produce any result, even though its definition in luatex manual looks like Append-1 recipe (insert_after)
\documentclass{article}
\usepackage{lua-visual-debug}
\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
% Append-1 works fine, A goes in margin:
% node.insert_after(n.list,node.tail(n.list),newn)
% Append-2 produces no result:
% node.insert_before(n.list,n.head,newn)
% Append-3 instead of putting A in margin makes text violate right margin
% n.head,n=node.insert_before(n.list,n.head,newn)
end
%
end
return h
end
luatexbase.add_to_callback('post_linebreak_filter', my_post_lb_filter, 'add A at eol')
}
\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}