Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LaTeX listings package: copy-pastable listings

Tags:

latex

listings

Writing some docs with code snippets which I want to be copyable to run as written. These snippets may include lines with preceding spaces. The listings package formats the text fine, but the spaces are not copyable.

Let's say I have the following example:

\documentclass{article}
\usepackage{listings}
\begin{document}
\lstset{
    basicstyle=\ttfamily,
    frame=single,
    columns=fullflexible
}
\begin{lstlisting}[language=python]
def foo():
    return "bar"
\end{lstlisting}
\end{document}

If I copy and paste the listing somewhere, it becomes:

def foo():
return "bar"

which must be corrected by hand.

Is there a way to make the listings package include the original spaces? Or is there a package better suited for cases like this?

like image 538
Vultaire Avatar asked Aug 20 '10 01:08

Vultaire


1 Answers

This is (most likely) not a problem with listings (or latex at all), but with your PDF rendering software. For instance, with PDFKit-based (Preview, Skim, ...) on OSX, I get the behavior that you describe. By using Xpdf, however, the text is copied correctly.

like image 185
grddev Avatar answered Nov 10 '22 09:11

grddev