I found a solution to the leading whitespace!
Edit: Actually, it doesn't work in Adobe reader. It works in SumatraPDF.
Thanks to Daniel and cgnieder for their comments above.
As described by Martin Monperrus you can use the accsupp package to differentiate text that you copy-paste from the type-set text.
Note that the linked page is not usable out of the box. The way it is written there, surplus spaces are introduced during the accsupp replacement. Removing all the whitespace in the \newcommand seems to solve it. Here is a minimal example that does work:
\documentclass{article}
\usepackage{listings}
\lstset{
basicstyle=\ttfamily,
columns=fullflexible,
literate={\ }{{\copyablespace}}1
}
\usepackage[space=true]{accsupp}
% requires the latest version of package accsupp
\newcommand{\copyablespace}{\BeginAccSupp{method=hex,unicode,ActualText=00A0}\ \EndAccSupp{}}
\begin{document}
Some text.
\begin{lstlisting}
def f(x):
return x+1
Previous line intentionally left blank.
\end{lstlisting}
\end{document}
Notes:
The basicstyle=\ttfamily and the columns=fullflexible are required to get the font monospaced and to prevent alignment-spaces from being introduced in the middle of your code, as detailed here.
The blank newlines still can't be copy pasted. Does anyone know if this can be done with accsupp as well?
I have for the time being ignored the special unicode characters and such that Martin Monperrus talks about. See that page if you have any problems with that. (Using "upquotes=true" fixes the most common problem)
If you don't want to use the accsupp package, this also works somewhat:
If you set showspaces=true, it turns all spaces into a visible space character, including leading spaces. This doesn't look very pretty, but they are indeed copyable.
The listings package allows you to set visible tab characters yourself, but unfortunately not visible space characters. Perhaps someone with more LaTeX knowledge can offer a suggestion on how to replace the visible space character with some form of invisible space character. I assume the copy-paste-ability would be maintained.
This also doesn't solve the blank newlines problem.
Proof of concept:
\documentclass{article}
\usepackage{listings}
\lstset{basicstyle=\ttfamily, columns=flexible, showspaces=true}
\begin{document}
Some text.
\begin{lstlisting}
def f(x):
return x+1
Previous line intentionally left blank.
\end{lstlisting}
\end{document}
\lstinputlisting) and embed the source files in the PDF itself, using theembedfilepackage. My readers can then just extract them from the PDF they're reading. No copy/paste required! – jub0bs Nov 06 '13 at 17:01Are you really sure this depends on the PDF viewer? I will admit I only tried adobe reader and sumatraPDF, but the way the selection displays suggests there aren't actually any spaces it could copy there.
Embedded files are interesting. I wasn't aware that this was a possibility. Then again, taking myself as an example, the users of the document may not be aware either, whereas copy-paste is a much more natural action. Thanks for the suggestion though! If copy-paste doesn't work out, I'll consider embedding files.
– Mqrius Nov 07 '13 at 10:13accsupppackage, which provides commands to differentiate the text that is copied from the typesetted content. One "just" has to hack this intolistingsinternal line scanner and printer, so that the actually read verbatim text line (sansgobble=value) is used as the copy text for each formatted listings line. This would also make it possible to use line numbers in the PDF that are not copied. Any takers? – Daniel Nov 07 '13 at 11:45