# XeLaTeX and missing spaces in PDF text

I use xelatex to compile a PDF from this tex-file:

\documentclass[14pt]{scrartcl}

\usepackage{fontspec}
\usepackage{xunicode}
\usepackage{xltxtra}
\usepackage{polyglossia}
\usepackage{lipsum}
\setmainfont{Times New Roman}

\begin{document}
\lipsum
\end{document}


When I copy text from the PDF (in Foxit Reader or SumatraPDF), in some lines there are no spaces. For example, there is line:

metusami. Morbiacorcietnislhendreritmollis. Suspendisseutmassa. Crasnecante.


If I comment out \setmainfont{Times New Roman}, all the spaces appear. How can I font selection and have normal text copying from my PDF files?

P.S.: Miktex 2.9, Windows

-
It works fine for me, with Adobe Reader and TeX Live 2011. –  Leo Liu Jun 18 '11 at 5:33
I'm install Adobe Reader, and using it all the spaces exist. So it looks like trouble of pdf-viewers. Foxit Reader don't copy spaces, SumatraPDF don't copy some of spaces. Hm... –  Molurd Jun 18 '11 at 5:56

The PDF format does not require that spaces be represented explicitly in order for them to display correctly (due to its history as a format for representing the printed page). Therefore, PDF viewers are required to use some heuristics to decide when the gap between glyphs is large enough to count as an interword space. Due to the stretching and shrinking of spaces required for setting justified text, this is not entirely trivial. Adobe Reader has heuristics that work nearly all the time. SumatraPDF is newer and has a smaller development team so its heuristics are not quite as fine-tuned: they are tracking this as their issue #1316 and request people to submit documents to them which exhibit this problem so they can try to tweak things. I recommend you to submit your file because in my experience they are very quick to fix issues that they are able to reproduce.

With modern developments around PDF, people are interested in things like automatic reflow for small screens and structural information for document processing or interfacing to screenreaders for people with visual impairment. Due to this, it is now possible to represent the spaces explicitly (I believe it is even a requirement for some grade of PDF/A compliance). There is a patch for pdftex here after which I believe you are supposed to add the following to your tex file:

\pdfmapline{+dummy-space <dummy-space.pfb}
\pdfgeninterwordspace


I don't know if the patch still applies (the bug tracker claims it has been replaced by a branch, probably this one), and I haven't tested it to see if it actually solves the problem. I don't know if there is something similar for xetex. Let me know if you find something that works for you.

Edit to add: to answer your original question, you can indeed have font selection and proper text copying, if you increase the interword space of your font (computer modern has unusually large interword spacing compared to the microsoft fonts). For example can write something like: \fontdimen3\font=4.7pt (I chose 4.7pt from the result of \showthe\fontdimen3\font when using computer modern). This will indeed solve your problem, although messing with the font designer's intended spacing is not good typographical practice...

-
I don't know why, but for SumatraPDF in case of MS fonts, like Times New Roman, Calibri or Cambria spaces disappears. In case of Computer Modern Unicode or DejaVu — all the right. Foxit Reader don't copy spaces anyway. –  Molurd Jun 18 '11 at 16:42
And about heuristics (as I think): for the code $2\times2$, SumatraPDF copying 2 × 2, but Foxit & Adobe 2 (U+0002) 2. –  Molurd Jun 18 '11 at 16:44
@Molurd, for copying $2\times2$ to work in all readers try adding \input glyphtounicode.tex \pdfgentounicode=1 to the preamble. –  Lev Bishop Jun 18 '11 at 16:55
@Lev Bishop, thanks for help. But it looks like trick for pdflatex, not xelatex. In xelatex I'm obtain errors like Undefined control sequence. \pdfglyphtounicode{A}{0041}. –  Molurd Jun 18 '11 at 17:05
I am not very familiar with xelatex, but it looks like you are right that it doesn't support \pdfglyphtounicode. I don't know if there is some equivalent feature. –  Lev Bishop Jun 18 '11 at 17:57