# How to find the length of last line of a right to left paragraph

This question is a follow up of my previous question Colors bizarre results with right to left text, \color command works well in right to left text if included at the beginning of paragraph, then I thought I could use it in middle of paragraph like that

\documentclass{article}
\usepackage{xcolor}

\TeXXeTstate=1 % to enable right to left writing

\parindent=0pt

\begin{document}
\everypar{\beginR} % for right to left writing

before \par
\hangindent=-3em % how to measure length of preceding line
\hangafter=-1
\vskip-\baselineskip
{\color{blue}
right to left text right to left text right to left text right to left  text right  to left text right to left text right to left text
\par}
\hangindent=-20em % how to measure length of preceding line
\hangafter=-1
\vskip-\baselineskip
after

\end{document}


The problem is how to calculate the length of the last line preceding colored parts. Any helps to automate the procedure are welcome.

• That looks horrible. I don't think that such a hack can be stable. Beside this: with lualatex the color is fine in RTL-context. – Ulrike Fischer Jul 29 '18 at 16:52
• I need colors with xelatex – Salim Bou Jul 29 '18 at 16:54
• Then you should start to acknowledge the restrictions of the bidi-model of xelatex. Such a hack might work once in a document, but you said you are writing a class. Do you really want to support such code later on? Or do you intend to run away when you have delivered the class and let the users sort out the following problems? – Ulrike Fischer Jul 29 '18 at 17:02
• I can't find a solution of this problem anywhere, the bidi-model of xelatex is based on a series of hacks, maybe this can help to enhance using colors in right to left documents. – Salim Bou Jul 29 '18 at 19:57

The length of the last line is available as \predisplaysize in a display equation. Of course in your case there is no equation, so we have to insert an empty one:

\documentclass{article}
\usepackage{xcolor}

\TeXXeTstate=1 % to enable right to left writing

\parindent=0pt

\makeatletter
\newcommand\hangtolastline[1][]{%
\null$$% \predisplaypenalty=10000 \abovedisplayshortskip-\baselineskip \abovedisplayskip\abovedisplayshortskip \postdisplaypenalty=10000 \belowdisplayshortskip\abovedisplayskip \belowdisplayskip\abovedisplayskip \xdef\mysavedhang{\hangafter=-1\relax\hangindent-\dimexpr\hsize+\the\predisplaysize-2em\relax\relax}%$$%
\par#1\mysavedhang
}
\makeatother

\begin{document}
\everypar{\beginR} % for right to left writing

\noindent before \hangtolastline
{\color{blue}
right to left text right to left text right to left text right to left  text right  to left text right to left text right to left text
\hangtolastline[\aftergroup]}
after

\end{document}


Warning: This is very fragile. For example it breaks after a single line paragraph without \noindent, it might create weird pagebreaks and you can not end the paragraph before \hangtolastline is called.

• Thanks, if the last line is somewhat long it overlap with colored text – Salim Bou Aug 2 '18 at 10:54