[Tex/LaTex] Only Select Code without Line Numbers

line-numberinglistings

I'm writing a LaTeX document and want to include several listings for source code examples.

Is there a way to disable the selection of line-numbers when selecting the source code?

My prof just wants to copy/paste the source code from the final pdf-file without manually removing all the line numbers afterwards. Does anyone know how to fix this problem?

Best Answer

Unfortunately I really need the line numbers. I want to reference several lines within my text.

After I kept searching, I found a solution for this problem using the following snippet:

\usepackage{accsupp}    
\lstset {
    numberstyle=\tiny\noncopynumber,
    columns=flexible,
}

\newcommand{\noncopynumber}[1]{
    \BeginAccSupp{method=escape,ActualText={}}
    #1
    \EndAccSupp{}
}

That way the line numbers won't be selected anymore. In addition using colums=flexible I get rid of the whitespaces when copy/pasting the source code out of the pdf-file.