2

I needed to make spaces at the beginning of line copyable in pdf, and came up with the following trick.

In lm-rmtt.enc I change /tilde to /nbspace and then use this code:

\font\myfont=rm-lmtt10
\def\myspace{{\myfont\char`~}}
{\obeyspaces\global\let =\myspace}

This way, when when I say \obeyspaces, I get copyable spaces at the beginnig of line in pdf:

\font\tentt=cmtt10
\tentt
\obeyspaces
         hello

This works beautifully, but I wanted to ask if this way is "legal". What becomes with the metrics if pdftex is used directly, and if dvipdfmx is used? I know they take metrics from Type1 fonts, without paying attention to tfm files. What metrics are used in this case?

  • Neither take metrics directly from type1 fonts. They both rely on the .tfm files. The only type1 font files they use are the .pfb (or .pfa files which do not contain any metrics. The metrics are contained in the .afm (or .pfm files but neither TeX nor pdfTeX uses those. When you create font support files, you use those files to create the .tfmfiles, along with other information. (Or, for pdfTeX, you can also create them from .ttf files for use with truetype fonts.) – cfr Dec 24 '15 at 2:32
  • @cfr: I don't understand .enc, .map and .pfb files. I just stumbled to this trick by occasion. Why does it work? In this case if .enc file is changed what metrics in taken for /nbspace? This character is not in .tfm at all. – Igor Liferenko Dec 24 '15 at 2:43
  • \newdimen\tempadimen \advance\tempadimen by 10pt ?? – cfr Dec 24 '15 at 2:46
  • @cfr: what do you mean? – Igor Liferenko Dec 24 '15 at 2:47
  • 3
    It clearly depends on the PDF viewer. PDFKit based ones (on Mac OS X) don't copy spaces. Adobe Reader does. – egreg Dec 24 '15 at 10:42
3

First I tried compiling your example with \pdfcompresslevel=0 and I got

BT
/F51 9.9626 Tf 91.925 759.927 Td [(~~~~~~~~~)]TJ/F30 9.9626 Tf 47.074 0 Td [(hello)]TJ/F1 9.9626 Tf 164.51 -654.747 Td [(1)]TJ
ET

As you see, the PDF file contains the repeated tilde, but the font resource has the modified encoding and so this tilde will appear as something else to the PDF viewer.

I did something else, then: I changed also /i into /nbspace and modified your example file into

\pdfcompresslevel=0
\pdfmapline{=rm-lmtt10 LMMono10-Regular " enclmrmtt ReEncodeFont " <lm-rmtt-mod.enc <lmtt10.pfb}

\font\myfont=rm-lmtt10
\def\myspace{{\myfont\char`~}}
{\obeyspaces\global\def {\myspace}}

\font\tentt=cmtt10
\tentt
\obeyspaces
         hello

\def\myspace{{\myfont\char`i}}%
         hello

\bye

(note that lm-rmtt-mod.enc is the modified .enc file, because I didn't want to tamper with default files). Here's what I get in the PDF file

BT
/F51 9.9626 Tf 91.925 759.927 Td [(~~~~~~~~~)]TJ/F30 9.9626 Tf 47.074 0 Td [(hello)]TJ/F51 9.9626 Tf -47.074 -11.955 Td [(iiiiiiiii)]TJ/F30 9.9626 Tf 47.074 0 Td [(hello)]TJ/F1 9.9626 Tf 164.51 -642.792 Td [(1)]TJ
ET

Here's what I see when I select all the text in Adobe Reader

enter image description here

which shows all spaces are “seen”.

If I do the same “select all” operation on Skim (an Apple PDFKit based previewer) I see instead

enter image description here

No spaces are copied. Therefore, the possibility of copying the spaces depends on the previewer.

Note however that, if I perform “copy” from Adobe Reader on the top line and then paste in an editor window, I get

enter image description here

What's “seen” are spaces, but the underlying text still has tildes.

  • Yes. Okular (and so probably Evince and other poppler-based viewers) behaves as Skim. – cfr Dec 25 '15 at 1:35
4

You could also try \pdffakespace and \pdfinterwordspaceon. But while in the following example the "f"'s copy fine from the pdf, spaces are sometimes missing, or there is only one instead of many, 00A0 is copied as 0020 -- I don't think that TeX is removing them, it also happens with boxes between the spaces. So imho the pdf viewer is trying to be to intelligent.

\documentclass{article}
\begin{document}

\pdfglyphtounicode{space}{0066} %f to see it better
{\obeyspaces\gdef {~\pdffakespace}}

hello\pdffakespace hello

\obeyspaces 
x       hello

        g
\end{document}

This copies then as

hellofhello
x f f f f f f fhello
f f f f f f f fg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.