# XeLaTeX misplaces ActualText box

The following MWE uses accessibility features of PDF provided by the package accsupp to replace the content of the selection corresponding to BBB with ZZZ in the string AAABBBCCC.

\documentclass{article}
\usepackage{accsupp}
\begin{document}
AAA\BeginAccSupp{ActualText=ZZZ}BBB\EndAccSupp{}CCC
\end{document}


If it is compiled with pdflatex everything works fine.

Compilation with xelatex results in a gross misplacement of the box corresponding to ZZZ, as can be seen in the following picture produced using evince (other viewers show the same issue).

Why is this happening? Can the problem be fixed?

(I cannot give up xelatex in favour of pdflatex.)

# Further analysis

I decompressed the PDFs using qpdf and examined the outputs. The crucial differences are all in a single stream (i.e. replacing the xelatex one by the one produced with pdflatex and adjusting the fonts everything works as expected).

XeLaTeX outputs

stream
q 1 0 0 1 72 769.89 cm
BT
/F1 9.963 Tf 76.71 -62.76 Td[(AAA)]TJ
ET
q 1 0 0 1 99.13 -62.76 cm
/Span<</ActualText(ZZZ)>>BDC
Q
BT
/F1 9.963 Tf 99.13 -62.76 Td[(BBB)]TJ
ET
q 1 0 0 1 120.3 -62.76 cm
EMC
Q
BT
/F1 9.963 Tf 120.3 -62.76 Td[(CCC)]TJ 110.83 -567.88 Td[(1)]TJ
ET
Q
endstream


while PDFLaTeX does

stream
BT
/F8 9.9626 Tf 148.712 707.125 Td [(AAA)]TJ
ET
/Span<</ActualText(ZZZ)>>BDC
BT
/F8 9.9626 Tf 171.128 707.125 Td [(BBB)]TJ
ET
EMC
BT
/F8 9.9626 Tf 192.299 707.125 Td [(CCC)]TJ 110.834 -567.87 Td [(1)]TJ
ET
endstream


It seems that the only difference is that XeLaTeX prefers jumping back and forth with coordinate transformations. Maybe this raw code can help someone shed some light on the behaviour.

• Could you explain a bit more how that works or how I can reproduce this error? I just see AAABBBCCC in two different viewers but I am not that familiar with accsupp. – LaRiFaRi Aug 21 '15 at 13:36
• @LaRiFaRi The problem is not visual but functional. Try selecting the text (dragging from A to C): the highlighted area corresponding to BBB is offset, like in the picture. This means that if one tries to select BBB nothing happens because the actual box is not where BBB appears. accsupp replaces the content of text selection: if you copy AAABBBCCC you will paste AAAZZZCCC. – Paolo Brasolin Aug 21 '15 at 13:39
• Works perfectly for me. Thanks for the description! Like that I could test it. I use Adobe Reader 11.0.1 and it works. In Sumatra, the replacement does not work, but the selection works. – LaRiFaRi Aug 21 '15 at 13:47
• @LaRiFaRi it does? It doesn't work for me with xetex in cygwin texlive 2015 – David Carlisle Aug 21 '15 at 13:53
• @DavidCarlisle Yes, I checked again. TeXStudio, MikTeX2.9, Win7. Compiled once with XeLaTeX, opend in Adobe Reader 11.0.1, selected and copied back in TeXstudio. AAAZZZCCC (I will rebuild my XeLaTeX now... keep your fingers crossed that I don't destroy my setup!) – LaRiFaRi Aug 21 '15 at 13:55

Good question, which includes a thorough analyis!

I did not find any official documentation of XeTeX for the equivalent for pdfTeX's \pdfliteral direct. The manual of dvipdfm, some predecessor of XeTeX describes \special{pdf:content ...}. Nothing in xetex-reference.pdf or XeTeX-notes.pdf.

In file pgfsys-dvipdfm.def the following "ugly hack" can be found:

\def\pgfsys@invoke#1{\special{pdf: content Q #1 q}} % ugly, but necessary...


dvipdfmx seems to support a new keyword code, pgfsys-dvipdfmx.def contains:

% pdf:code' is an abbreviation of pdf:literal direct', the idea of which
% came from pdfTeX's primitive \pdfliteral direct.
\def\pgfsys@invoke#1{\special{pdf:code #1}}


This seems also be supported in XeTeX.

Thus the following patch should fix the issue:

\documentclass{article}
\usepackage{accsupp}

\makeatletter
\def\ACCSUPP@bdc{%
\special{pdf:code \ACCSUPP@span BDC}%
}
\def\ACCSUPP@emc{%
\special{pdf:code EMC}%
}
\makeatother

\begin{document}
AAA\BeginAccSupp{ActualText=ZZZ}BBB\EndAccSupp{}CCC
\end{document}


The PDF page stream (reformatted):

stream
q
1 0 0 1 72 769.89 cm
BT
/F1 9.963 Tf
76.71 -62.76 Td
[(AAA)]TJ
ET
/Span<</ActualText(ZZZ)>>BDC
BT
/F1 9.963 Tf
99.13 -62.76 Td
[(BBB)]TJ
ET
EMC
BT
/F1 9.963 Tf
120.3 -62.76 Td
[(CCC)]TJ
110.83 -567.88 Td
[(1)]TJ
ET
Q
endstream

• +1 I had the feeling that you'd be the one to answer this:-) – David Carlisle Aug 21 '15 at 20:04