6

Please consider the following MWE:

\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},
    commentstyle=\color{codegreen},
    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
}

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

%\makeatletter                                              % Answer
%\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:

Original output

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

Bad copy

I tried the Martin Scharrer solution with differents options:

  1. (Martin Scharrer solution) Example 1

  2. (added by me) Example 2

  3. (suggested by TeXnician answer) Example 3

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.

manooooh
  • 3,203
  • 2
  • 21
  • 46
  • 1
    Off-topic: you really ought not use vmargin. – cfr Jul 21 '18 at 02:53
  • "Use geometry instead" :). I remember that @cfr, but if I change it to geometry then the \setpapersize{A4},\setmargins{2.2cm},... code produces 3 errors. How would you fix that? – manooooh Jul 21 '18 at 03:18
  • 3
    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 03:31
  • @cfr I will try. Btw I think if you propose that change you have to give me (or to anyone) the correct code for testing and then adopt the new package.. – manooooh Jul 21 '18 at 03:37
  • 3
    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 03:42
  • For the purposes of the question this is off-topic. Then, I accept your suggestion in order to see some changes... As I don't see changes why I would change my structure of code if, for my purposes, using vmargin or geometry is exactly the same? I really appreciate your suggestion, really, I trust you, but I am here not to fix that issue, but to fix these two problems that I posted with your help. When I find the solution that you gave me I will take a look at geometry. – manooooh Jul 21 '18 at 03:51
  • Do you see my point of view, @cfr? Please, tell me if using geometry is really necessary and I will take a look now. I do not like to disobey you because I know that you all know much more than me... – manooooh Jul 21 '18 at 03:54
  • 2
    May I add that your MWE is not minimal? You do not even need the page setup stuff here. – TeXnician Jul 21 '18 at 09:10
  • 2
    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 https://meta.stackexchange.com/q/17447 – nox Jul 21 '18 at 09:16
  • @cfr I removed the vmargin package ;). Please see my updated question. – manooooh Jul 21 '18 at 18:36

1 Answers1

9

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.

TeXnician
  • 33,589
  • 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
  • 1
    @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
  • I regret having canceled your answer but if I compile your code (adding \usepackage[spanish]{babel}) and open the PDF Outlook then numbers are copied too (but not in Acrobat Reader for Windows 10). Please see this image. – manooooh Jul 22 '18 at 01:28
  • 1
    @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 07:52
  • Oh :(. Did you try sending the file via email? I could reproduce it in this gif. Please take a look and try to tell how to fix that when uploading the PDF file to Internet with this example. – manooooh Jul 22 '18 at 17:33
  • 2
    @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
  • I know, I just make an example. I use the free Acrobat Reader DC for Windows 10. I do not know which viewer has the email. However, it still does not work. If you need more information please let me know. – manooooh Jul 22 '18 at 20:13