# How can I enable search within verbatim sections of a LaTeX document rendered to PDF?

I'm generating a LaTeX document as so:

\documentclass{article}
\usepackage[letterpaper, margin=0.75in]{geometry}
\usepackage[tocindentauto]{tocstyle}
\usetocstyle{standard}
\usepackage{parskip}% http://ctan.org/pkg/parskip
\usepackage{spverbatim}
\usepackage{listings}
...
Request function: \verb#SNTP_POLL_FAILURE#

Assignments: \begin{lstlisting}
msg = some_interesting_function_call(&etc)
\end{lstlisting}


The difficulty I'm having is twofold:

1. The header is not searchable. If a user searches for "HEADER_SNTP_POLL_FAILURE" they do not get a hit on the subsubsection header. However, "SNTP_POLL_FAILURE" does get a hit, on the "Request function" text inside the \verb.
2. If a user searches for "some_interesting_function_call" they will not get a hit on the lstlisting. And if I copy the text out of the PDF and into a terminal, it comes out like this: s ome i n t e r e s t i n g f u n c t i o n c a l l This makes the code listing pretty useless, as it makes using the document as a reference pretty awkward.

How can I solve this? I want the listings to be useful as a copy-and-paste source, and I want to be able to search for symbols in the listings and in section headings.

• Answering my own question... using "verbatim" instead of "lstlisting" works much better. However, now I end up with code that runs beyond the edge of the page and is not wrapped. A new problem. – Benjamin Damm Nov 3 '17 at 18:50
• tex.stackexchange.com/q/4911/134144 discusses the reason for your original problem. For automated linebreaks in a verbatim environment you can have a look here: tex.stackexchange.com/q/14342/134144 – leandriis Nov 3 '17 at 18:56
• Thanks! I also tried "\lstset{columns=flexible}" and retaining lstlisting, and it is better, but _ are converted to spaces, so still no good. – Benjamin Damm Nov 3 '17 at 18:57
• Thank you @leandriis, this leads to using "spverbatim", which I'm finding does the job nicely! Wow! – Benjamin Damm Nov 3 '17 at 18:59
• @Marijn and benjamin -- having run afoul of spaces in a pdf file prepared with latex from which material was copied-and-pasted, i appreciate this question. two separate answers (or a "joint" answer) with small testable examples for the two suggestions would be welcome. i can't find anything else that covers this exact situation on the site, either in the "related" items or by searching for "search verbatim pdf". i think the title could also be improved, but haven't yet come up with anything better. – barbara beeton Nov 3 '17 at 19:18

The minted package renders code snippets searchable and copyable. The package provides syntax coloring and supports line breaking with the environment option breaklines=true. If you don't want coloring, the lexer text can be used. Run with pdflatex --shell-escape. Additionally, pygmentize must be installed on the system (see, e.g., How to use minted under MikTeX and Windows 7? or I can't get minted package to work under Ubuntu (pygments error)).

MWE:

\documentclass{article}
\usepackage{minted}
\begin{document}