# \llap mystery with TeX--XeT

Consider

\documentclass{article}
\usepackage{xecolor} % loads fontspec by default
\setmainfont{Times New Roman}
\begin{document}
\fontsize{60}{70}\selectfont

TEST\llap{\textxecolor{red}{TEST}}
\end{document}


As expected, TEST will be in red color. However with

\documentclass{article}
\usepackage{xecolor} % loads fontspec by default
\setmainfont{Times New Roman}
\TeXXeTstate=1
\begin{document}
\fontsize{60}{70}\selectfont

\beginR TEST\llap{\textxecolor{red}{TEST}}\endR
\end{document}


TEST will be in black color. What is the reason?

Originally I thought this might be resolved by using the supported LaTeX syntax of \makebox[0pt][l]{<content>} rather than the plain-TeX \llap, but the behavior is the same. The \Xlap commands cause issues in certain places because they are \hboxes (TeX primitives, not \mboxes (LaTeX macros). See further details here. Might be worth noting in any case.

As you can see below, both syntaxes produce the same result. The difference comes from the direction in which LaTeX scans your input. Put simply, in LTR mode, it prints black and then prints red at the same point on the page. So the result is red. Conversely, in RTL mode, it prints red and then prints black at the same point on the page.

We can see this effect by reversing the order of the original text and the overlapping box:

\documentclass{article}
\usepackage{xecolor} % loads fontspec by default
\setmainfont{Times New Roman}
\TeXXeTstate=1
\newcommand{\tk}{\textxecolor{black}{TEST}}
\newcommand{\tr}{\textxecolor{red}{TEST}}

\begin{document}
Original:

\tk\llap{\tr}

\beginR\tk\llap{\tr}\endR

\LaTeX\ syntax:

\tk\makebox[0pt][r]{\tr}

\beginR\tk\makebox[0pt][r]{\tr}\endR

\LaTeX\ syntax, order reversed:

\makebox[0pt][l]{\tr}\tk

\beginR\makebox[0pt][l]{\tr}\tk\endR
\end{document}


## Output

• Thanks. I guess, it is another e-tex bug. – user56799 Jul 13 '14 at 2:33

This is a bug in the TeX--XeT bidirectional extension (from e-TeX) used in current version of XeTeX, which reverses the order of nodes in right-to-left mode so that the order of the coloured text is reversed.

Next version of XeTeX reverted back to the original TeX-XeT extension from (Knuth and Pierre MacKay) which does not “physically” reverse the nodes, but instead outputs special opcodes in the DVI file that mark the start and end of the right-to-left text and lets the job of the actual reversal to the driver. The driver (xdvipdfmx here) have been updated to support the new opcodes and instead of physically reversing the text, it keeps it in its logical order and just changes the X positions so that it looks reversed. This keeps the logical order of the nodes, so colour and other specials get applied in their correct order.