# How to mark soft wrap lines?

Is there a way to mark all lines which have been wrapped automatically (i.e. places where LuaTeX decided there needed to be a line break)? Something like \localrightbox but which is only active at the end of a line where some text has been wrapped to the next? In the following MWE, the hook arrow should not appear starting at the last line of the first paragraph (because the first paragraph ends on that line, because the second paragraph only needs one line, and because the third has hard line breaks at the end of every line).

\documentclass[12pt]{report}

\usepackage{fullpage}
\usepackage{latexsym}
\newenvironment{speech}%
{%begin
\Large
\setlength{\parindent}{-1cm}
\setlength{\leftmargin}{1cm}
\localrightbox{$\hookleftarrow$}
}
{%end
}

\begin{document}

\begin{speech}
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed faucibus, sem vel suscipit eleifend, ipsum dolor tempus nunc, ut iaculis nibh arcu vitae tortor.
Pellentesque a efficitur lectus, eget sagittis lorem.
Fusce vestibulum feugiat nibh, pulvinar convallis eros sagittis ac.
Pellentesque aliquam arcu a augue malesuada, sit amet eleifend orci vulputate.
Sed nibh dolor, commodo vel risus quis, elementum consequat dui.
Pellentesque eu neque eleifend, tincidunt mauris id, ultricies odio.

Aenean ac nisi congue, mollis leo a, cursus dolor.

Aenean non justo felis.\\
Cras interdum quam eu metus imperdiet ultricies.\\
Fusce finibus pellentesque volutpat.\\
Praesent id lacus eget dui ullamcorper finibus nec nec urna.\\
Quisque vel vestibulum quam.\\
Lorem ipsum dolor sit amet, consectetur adipiscing elit.\\
Mauris vel nisl eros.\\
\end{speech}
\end{document}


If you compile this MWE, you'll see the hook arrow at the end of every line. Ideally, the above should look like this:

• I'm not really sure what you're asking for. Perhaps you should mock up a document that will output a file as you would like. I'm not asking for the impossible: answering your own question. But you certainly can create an output document with the desired appearance as if all the markup had been done, not by hand, but automatically by LaTeX. Do you get what I'm saying? – A.Ellett Sep 27 '18 at 22:46
• @A.Ellett I've edited in an image of the desired result for the MWE. – rpspringuel Sep 28 '18 at 0:51
• Off-topic: The latexsym package is quite obsolete; use amssymb instead. In case you're curious: These days, the only reasonable justification for loading the latexsym package is if the LaTeX document must be compiled in LaTeX2.09 compatibility mode. LaTeX2e superseded LaTeX2.09 in 1994 -- nearly 25 years ago! For sure, I can think of no valid reason for wishing to compile a LuaLaTeX document in LaTeX2.09 compatibility mode. – Mico Sep 28 '18 at 6:58
• @Mico, Thanks. I'll make that switch. I used latexsym simply because it was the first package I found that provided the symbol I wanted. – rpspringuel Sep 28 '18 at 12:29

## 1 Answer

I have come up with a solution which hooks into the post_linebreak_filter. The code loops over all the lines in the paragraph, except for the last line, because the last line cannot be broken (otherwise it wouldn't be the last line). For each line I iterate over all penalties within that line. If there is a penalty with subtype 0 (userpenalty) that indicates that the user inserted a penalty by hand, as is the case for \\. Then we only need to check that the value of the penalty is -10000 and therefore a forced line break. If that is not the case, then this means that there was no forced break and TeX broke the line by itself. Hence we can insert the arrow at the end of the line.

\documentclass[12pt]{report}

\usepackage{fullpage}
\usepackage{amssymb}
\newenvironment{speech}%
{%begin
\Large
\setlength{\parindent}{-1cm}%
\setlength{\leftmargin}{1cm}%
\directlua{luatexbase.add_to_callback("post_linebreak_filter",
mark_small_glue_lines, "speech")}%
}
{%
\par % trigger the last post_linebreak_filter before disabling it
\directlua{luatexbase.remove_from_callback("post_linebreak_filter", "speech")}%
}

\newbox\hookleftarrowbox
\setbox\hookleftarrowbox=\hbox{$\hookleftarrow$}
\directlua{

mark_small_glue_lines = function(head)
local last_line = nil
for line in node.traverse_id(node.id("hlist"), head) do
if last_line then
local has_userpenalty = false
for pen in node.traverse_id(node.id("penalty"), last_line.list) do
if not has_userpenalty then
has_userpenalty = (pen.subtype == 0 and pen.penalty == -10000)
else
break
end
end
if not has_userpenalty then
node.insert_after(last_line.list, node.tail(last_line.list),
node.copy(tex.box.hookleftarrowbox))
end
end
last_line = line
end
return head
end

}
\begin{document}

\begin{speech}
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed faucibus, sem vel suscipit eleifend, ipsum dolor tempus nunc, ut iaculis nibh
arcu vitae tortor.
Pellentesque a efficitur lectus, eget sagittis lorem.
Fusce vestibulum feugiat nibh, pulvinar convallis eros sagittis ac.
Pellentesque aliquam arcu a augue malesuada, sit amet eleifend orci vulputate.
Sed nibh dolor, commodo vel risus quis, elementum consequat dui.
Pellentesque eu neque eleifend, tincidunt mauris id, ultricies odio.

Aenean ac nisi congue, mollis leo a, cursus dolor.

Aenean non justo felis.\\
Cras interdum quam eu metus imperdiet ultricies.\\
Fusce finibus pellentesque volutpat.\\
Praesent id lacus eget dui ullamcorper finibus nec nec urna.\\
Quisque vel vestibulum quam.\\
Lorem ipsum dolor sit amet, consectetur adipiscing elit.\\
Mauris vel nisl eros.
\end{speech}

\end{document}


• Wow. I was wondering what I could put together. I've worked with LuaTeX, maybe that's something I should look into. – A.Ellett Sep 28 '18 at 5:40
• @A.Ellett I actually came up with a much better solution now. – Henri Menke Sep 28 '18 at 6:18