Using this solution Only Select Code without Line Numbers I allow the listings to be copyable from PDF w/o the line numbers and many extra spaces.
But that also removes the leading spaces used for indentation!
\documentclass[11pt,letterpaper]{memoir}
\usepackage{listings}
\lstloadlanguages{SQL}
\usepackage{accsupp}
\lstset{
frameround=fttt
,language=SQL
,numbers=left
,breaklines=true
,showstringspaces=false
,basicstyle=\small
,numberstyle=\noncopynumber
,columns=flexible
}
\newcommand{\noncopynumber}[1]{
\BeginAccSupp{method=escape,ActualText={}}
#1
\EndAccSupp{}
}
\begin{document}
\begin{lstlisting}
declare @a int
if @a = null
select 'null' as outcome
else
select 'not null' as outcome
\end{lstlisting}
\end{document}
This is what I get when pasting code from PDF:
declare @a int
if @a = null
select ' null ' as outcome
else
select 'not null ' as outcome
if null = null
select 'equal' as outcome
else
select 'not equal' as outcome
There are extra spaces around null on line #3 and one on line #5 but all leading spaces are gone.
columns=fullflexible. There are no spaces in PDFs so there's nothing to copy -- see How to copy/paste multiple spaces from lstlistings?. There's a workaround involving replacing spaces with other characters (which can be later replaced into spaces after pasting) -- see How to make listings code indentation remain unchanged when copied from PDF?. – alwaysask Jul 28 '16 at 14:27