2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.