# Is it possible to not copy numeration using lstlisting?

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}
\usepackage{color}
\usepackage{enumitem}

\usepackage{listings}

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstset {%
backgroundcolor=\color{backcolour},
keywordstyle=\color{magenta},
stringstyle=\color{codepurple},
basicstyle=\scriptsize,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
keepspaces=true,
numbers=left,
numbersep=5pt,
showspaces=false,
showstringspaces=false,
showtabs=false,
tabsize=2,
%columns=fullflexible,
keepspaces=true,
language=C++,
columns=flexible,
%numberstyle=\noncopynumber                             % 1. If this line is only uncommented it produces 6 errors
%numberstyle=\tiny\color{codegray}                      % 2. If this line is only uncommented it compiles
%numberstyle=\tiny\color{codegray}\noncopynumber        % 3. If this line is only uncommented it produces 6 errors
}

%\BeginAccSupp{method=escape,ActualText={}}%
%#1%
%\EndAccSupp{}%
%}

%\def\lst@outputspace{{\ifx\lst@bkgcolor\empty\color{white}\else\lst@bkgcolor\fi\lst@visiblespace}}
%\makeatother

\begin{document}

\begin{lstlisting}
Example code line 1
*
... and 3
\end{lstlisting}

\end{document}


When I compile it using LaTeX->PS->PDF the output is very fancy:

However, when I am going to copy the text and paste it into a compiler (or anywhere else):

I tried the Martin Scharrer solution with differents options:

1. (Martin Scharrer solution)

As you can see the problem is that the reference numbers (1, 2 and 3) are copied from PDF to any text editor even with the proposed solution.

I also take a look here, here and here but it does not work for me too (by the way please check that solutions).

I have code around all the full document.

Is there any easy solution to this problem?

Thanks!

EDIT: TeXnician's solution works for me as long as I keep the PDF file on my computer without sharing it via the Internet. However, when I try to upload it to, for example, Outlook his solution is without effect. You can see the reproduction in this gif (use fullscreen mode to see better the selected text in both PDF).

EDIT 2: also tested the Werner solution in Is there a LaTeX trick to prevent a PDF viewer from copying the line number? (adding \protect command inside the new command of TeXnician and/or renewing the \thelstnumber command) with the same results.

• Off-topic: you really ought not use vmargin. – cfr Jul 21 '18 at 2:53
• Obviously, you have to use geometry's facilities. I would add a4paper as a class option and then use \geometry{margin=2.2cm,...} with whatever other options you need. (I don't know what all those arguments are for. texdoc geometry for a full list of options. – cfr Jul 21 '18 at 3:31
• I think that's a ridiculous claim. First, I clearly don't have to do it. Second, I don't think I'm under any particular obligation to do it. If your claim were true, it would mean that every time somebody points out that the template somebody is using is crap, they're obligated to provide alternative code for the person's entire thesis format. The point is, I'd have to learn vmargin to give you equivalent code. That's a waste of my time because I shouldn't use the package. Whereas your learning geometry isn't a waste of your time, because you should use that package. – cfr Jul 21 '18 at 3:42
• May I add that your MWE is not minimal? You do not even need the page setup stuff here. – TeXnician Jul 21 '18 at 9:10
• It's not necessary to use geometry. @cfr pointed out a recommendation. You can follow this advice or not. If there's a wasp nest, some people would recommend you to leave it alone. You can do with your code whatever you want (as long as no one cooperates, at least). You can rape it, make it entirely unreadable, format in-line... But I'd rather not recommend this. You shouldn't request something, nor get as cocky as to tell advisers they have to implement stuff in your code. We're in userland, users help users, you don't employ the users. See meta.stackexchange.com/q/17447 – nox Jul 21 '18 at 9:16

You have one obvious problem in your code: You set numberstyle to make it non-copyable and then you reset it with fancy font stuff. Any option is overwritten at a later use. The effects do not add up. So you need that one line: numberstyle=\tiny\color{codegray}\noncopynumber.

A true MWE showing how to apply your macro would be

\documentclass{article}

\usepackage{enumitem}

\usepackage{listings}

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

\lstset {%
numbers = left,%
numberstyle=\tiny\noncopynumber
}

\begin{document}

\begin{lstlisting}
Example code line 1
*
... and 3
\end{lstlisting}

\end{document}


Concerning your problem with the asterisk I don't know what you literate, but using the proper unicode code point with {*}{{\char"002A}}1 (or alternatively \**) gives the correct char which can be copied as you would expect it to be copied.

• Your first solution does not to work for me. I have upgrade my question for more readable, and I have removed the question about bad asterisks because I have found an answer before. – manooooh Jul 21 '18 at 18:32
• @manooooh Why don't you include the accsupp package which is obviously required? – TeXnician Jul 21 '18 at 19:52
• @manooooh See my updated answer. – TeXnician Jul 21 '18 at 19:54
• @manooooh I'm on Linux and for me it works even with Spanish babel. I use a fully updated TL2018. Unfortunately I can't reproduce your problem, so I can't adapt my answer. – TeXnician Jul 22 '18 at 7:52
• @manooooh That has nothing to do with email, but rather with the used viewer. I don't quite get the mechanics of PDF, but probably the viewer (is it pdf.js?) doesn't correctly interpret what is in the pdf stream. – TeXnician Jul 22 '18 at 19:50