2

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[hidelinks]{hyperref}
\usepackage{spverbatim}
\usepackage{listings}
...
\subsubsection{HEADER\_SNTP\_POLL\_FAILURE}
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 03 '17 at 18:50
  • https://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: https://tex.stackexchange.com/q/14342/134144 – leandriis Nov 03 '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 03 '17 at 18:57
  • Thank you @leandriis, this leads to using "spverbatim", which I'm finding does the job nicely! Wow! – Benjamin Damm Nov 03 '17 at 18:59
  • An alternative might be the minted package, which seems to render code snippets searchable. – Marijn Nov 03 '17 at 19:03
  • 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. – barbara beeton Nov 03 '17 at 19:18
  • LaTeX does not (usually) embed space characters in a PDF. If you use pdflatex (but not LuaLaTeX) there is a package that will insert spaces, but using it may have other repercussions (unknown to me). Some PDF readers, such as Adobe Reader (not Pro) see no spaces, and thus copy or extract no spaces. Other PDF readers guess spaces based on visual separation of the characters; the guess is not always correct. Moral: TeX really is not the best way to prepare PDF for text extraction or search. Consider placing compiled TeX math expressions as high-resolution images in Scribus or Word. –  Nov 03 '17 at 19:43
  • @barbarabeeton as requested. – Marijn Nov 03 '17 at 20:15

1 Answers1

4

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}
\section{HEADER\_SNTP\_POLL\_FAILURE}
Request function: \verb#SNTP_POLL_FAILURE#\\
Assignments:
\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)) {}
\end{minted}
\end{document}

Result:

enter image description here

Marijn
  • 37,699
  • 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. – Benjamin Damm Nov 09 '17 at 07:34