# Prevent small, automatic spaces from splitting a word

I'm not sure if this is fundamental to LaTeX, or if this depends on the viewer, but I am facing the following phenomenon:

\texttt{\textit{C}D}


When I run pdflatex and view the result (in Okular) and search for text, I find this result a "C D", but not as "CD". The italic correction seems to make the viewer think that there's a space.

I've seen similar splittings of the search text in more complex settings involving manual kerning and mixing italics and uprights, but I think this is a simple, representative case.

Is there a general mechanism for keeping the word together logically so that I can search for it as expected?

• It depends on the viewer (for me at least). In SumatraPDF, the search finds "CD", while in Adobe Acrobat, it does not. – Paul Gessler Dec 11 '15 at 15:35
• @PaulGessler: OK, thanks -- I was fearing something like that... – Kerrek SB Dec 11 '15 at 15:36

One possibility would be to use the accsupp package, which allows us to define alternate texts for use by screen readers. A side-effect of this is that the text stream that is copied/searched from the PDF is also modified.

accsupp can be used ad-hoc (second paragraph of my document below) or wrapped in a command if you have many such patterns throughout the document. The accsupp method is not suitable for anything that could be expected to be split by a page break, but that should not occur for your usage here.

The highlighting in the PDF viewer can get weird (I noticed this in Adobe Acrobat DC), but the correct text is found by the search function when I tested in SumatraPDF and Adobe Acrobat DC on Windows 7. I do not know if this approach will work for all viewers.

\documentclass{article}
\usepackage{accsupp}

\newcommand*\mycommand[2]{%
\BeginAccSupp{ActualText=#1#2}%
\texttt{\textit{#1}#2}\EndAccSupp{}%
}

\begin{document}
\texttt{\textit{C}D}

\BeginAccSupp{ActualText=CD}\texttt{\textit{C}D}\EndAccSupp{}

\mycommand{C}{D}
\end{document}


For use in headings, the command must be robust (I used etoolbox for this, but there are other ways to accomplish it). I'm guessing you're probably also using hyperref, so I checked the bookmark list: this also functions correctly. The ActualText value is used for the bookmark text.

\documentclass{article}
\usepackage{accsupp}
\usepackage{etoolbox}
\usepackage{hyperref}

\newrobustcmd*\mycommand[2]{%
\BeginAccSupp{ActualText=#1#2}%
\texttt{\textit{#1}#2}\EndAccSupp{}%
}

\begin{document}
\texttt{\textit{C}D}

\BeginAccSupp{ActualText=CD}\texttt{\textit{C}D}\EndAccSupp{}

\mycommand{C}{D}

\section{\mycommand{C}{D}}
\end{document}

• Ah, yes, I was trying that the other day. Do accsup commands work inside section heading (and generally in things that get inserted into TOCs etc.)? – Kerrek SB Dec 11 '15 at 16:05
• @KerrekSB just one minor change is needed for the headings, you can see it in the second half of my edited answer. – Paul Gessler Dec 11 '15 at 16:32
• Thanks! One thing: I get "token not allowed" and "removing \mycommand'" errors from hyperref. The bookmark shows up correctly, though... – Kerrek SB Dec 11 '15 at 17:31
• Yeah, the only way to get rid of that warning is to use \texorpdfstring{...}{...} inside the \section command. I tried putting it inside the definition of \mycommand` but the warning still appears. It's harmless, but annoying nonetheless. – Paul Gessler Dec 11 '15 at 17:33
• Yep, thanks, I got it -- a final layer of wrapping does the trick. Thanks a lot! – Kerrek SB Dec 11 '15 at 17:53