# How does one make \textls text copy-and-pasteable in PDF?

Consider the following document:

\documentclass{article}
\usepackage{microtype}

\begin{document}
Some \textls[250]{letterspaced text}.
\textls*[100]{SPEECH}
\end{document}


Running this through pdfTeX, then copying and pasting "letterspaced text" gives l e t t e r s p a c e d t e x t. Note that there's only one space between the words; I can't unpick this automatically. If I use Adobe Caslon (by uncommenting the commented line) then copying and pasting "SPEECH" gives: S P EE C H.

I found plenty of information on deligating ligatures and getting the encoding of text correct for copy and paste in PDF (and indeed, the document I'm actually working on is already nicely set up in this respect!), but I can't find anything on this issue. More generally, I found accsup to provide replacement text, but that has the apparent disadvantage that it requires PDF 1.5, which presumably means that it cannot be used to produce PDF/A-1–compliant documents (as PDF/A-1 (2005) uses PDF v1.4). I don't know whether using PDF/A-2 or -3 is a problem in practice…

I had a go at making a definition:

% Make letterspaced text copy and paste correctly from PDF
\usepackage{accsupp,letltxmacro}
\LetLtxMacro{\plaintextls}{\textls}
\DeclareDocumentCommand \textls {s O{100} m}{%
\BeginAccSupp{method=pdfstringdef,ActualText=#3}%
\IfBooleanTF{#1}{\plaintextls*[#2]{#3}}{\plaintextls[#2]{#3}}%
\EndAccSupp{}%
}


This works reasonably well, but despite \DeclareDocumentCommand making the new command robust, it does not work in some contexts. It seems to have two sorts of problem: it does not work inside some arguments to commands, and it does not work when the text to be displayed contains macros such as \MakeUppercase.

• Imho accsupp is the way to go. You are not giving examples of the problems of your command so I didn't try to find concrete code, but I would add another optional argument for the actual text in case that the main argument is "difficult". (And I would avoid \MakeUppercase anyway.) – Ulrike Fischer Sep 25 '17 at 8:54
• @UlrikeFischer, I think you're right. I didn't give concrete examples because I've nothing close to an MWE to show without considerable work: it's a case of macro arguments being passed through several levels of my own macros. For now I've simply added some duplication at the source level, rather than trying to do LaTeX string processing; it's just too much work for me to understand, even with expl3. – Reuben Thomas Sep 25 '17 at 11:41
• The main problem seems to be \MakeUppercase being incompatible with pdfstrings, giving the error: Undefined control sequence. \MakeUppercase ...ppercaseUnsupportedInPdfStrings This occurs, for example, when \textls's argument contains \MakeUppercase, and when \textls is contained in the argument to \emakefirstuc. – Reuben Thomas Sep 25 '17 at 13:14
• As I wrote: I avoid \MakeUppercase, and most specifically I avoid it in arguments that are passed around. You always fight with expansion problems. – Ulrike Fischer Sep 25 '17 at 13:27
• The trouble is, I need to make text uppercase. Without \MakeUppercase, I have to duplicate the text, which leads to errors. – Reuben Thomas Sep 25 '17 at 19:01