I'm generating a LaTeX document as so:

\usepackage[letterpaper, margin=0.75in]{geometry}
\usepackage{parskip}% http://ctan.org/pkg/parskip
Request function: \verb#SNTP_POLL_FAILURE#

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

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. Nov 3, 2017 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, 2017 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. Nov 3, 2017 at 18:57
  • Thank you @leandriis, this leads to using "spverbatim", which I'm finding does the job nicely! Wow! Nov 3, 2017 at 18:59
  • 2
    @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. Nov 3, 2017 at 19:18

1 Answer 1


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)).


Request function: \verb#SNTP_POLL_FAILURE#\\
\begin{minted}[breaklines=true]{cpp}      % C++ lexer
msg = some_interesting_function_call(&etc)
VectorHistogram::VectorHistogram(size_t bins, size_t cache) : bins(std::vector<double>(bins)), cache(std::min(cache, MAX_CACHE_ENTRIES)) {}


enter image description here

  • This looks gorgeous! Minted requires shell access which is a bit spooky, but in any case your sample satisfies what I'm looking for. For those who are concerned with minted requiring shell there is the "spverbatim" solution. Nov 9, 2017 at 7:34

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.