5

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?

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

1 Answer 1

5

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}

Added after request for section headings:

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}
5
  • 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
    Commented Dec 11, 2015 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. Commented Dec 11, 2015 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
    Commented Dec 11, 2015 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. Commented Dec 11, 2015 at 17:33
  • 1
    Yep, thanks, I got it -- a final layer of wrapping does the trick. Thanks a lot!
    – Kerrek SB
    Commented Dec 11, 2015 at 17:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .