# How to remove indent characters when copying from PDF document using listings package

I am coding inside lstlisting environment using SPACEBAR for separate words and TAB for indent code. Check this MWE:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}
\usepackage[spanish]{babel}

\newcommand\opstyle{\bfseries\color{red}}
\makeatletter
\usepackage{xcolor,textcomp}
\usepackage{enumitem}

\usepackage{textcomp}
\usepackage{listings}
\usepackage{accsupp}
\usepackage{lmodern}

\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstset { %
language=C++,
alsoletter=0123456789,
upquote=true,
columns=fullflexible,
keepspaces=true,
backgroundcolor=\color{backcolour},
breakatwhitespace=false,
breaklines=true,
captionpos=b,
keepspaces=true,
showspaces=false,
showstringspaces=false,
showtabs=false,
tabsize=2
}

\newcommand\ProcessOther@silmeth
{%
\ifnum\lst@mode=\lst@Pmode%     % If we're in Processing' mode...
\def\lst@thestyle{\opstyle}%  % ... redefine the style locally
\fi%
}

\makeatother

%========================== Taken from https://tex.stackexchange.com/a/442600/152550
\newcommand{\noncopynumber}[1]{%
\BeginAccSupp{method=escape,ActualText={}}%
#1%
\EndAccSupp{}%
}

\makeatletter   %================= Taken from https://tex.stackexchange.com/a/19978/152550
\def\lst@outputspace{{\ifx\lst@bkgcolor\empty\color{white}\else\lst@bkgcolor\fi\lst@visiblespace}}
\makeatother

\begin{document}

\begin{lstlisting}
Example with no tabulation

With one tabulation

With one space
\end{lstlisting}

\end{document}


The output with selected text:

When I paste it anywhere, it turns on the spaces:

This way a compiler could never compile correctly.

Is there an easy way to keep spacing but no with this symbol: ␣?

Thanks!

P.D: I also take a look at the manual, section 2.5 Special characters - Visible tabulators and spaces but it does not say anything about this problem. Also I take a look at section 7, How tos - How to gobble characters.

• Your commented link explicitly said “[t]his hack is not perfect, however, as the typesetted character is really a visible space, not a space (so searching the pdf for char line will not work) and some PDF readers (like Mac's preview) will copy a visible space. It works under Acrobat Reader and it's extremely pleasant to be able to quickly copy/paste code without problem”. Also, on another note, please use basicstyle=\ttfamily` to display source code. – Ruixi Zhang Jul 22 '18 at 1:59
• Thank you for answering. I follow the suggestion of koleygr comment in the same link and it works partially; although it eliminates that character, the indentation does not hold. Do you want to post an answer referring to him to solve partially this question? – manooooh Jul 22 '18 at 2:49
• I’m happy to help! I merely quoted from @PhilippeGoutet. If you think you’ve found a satisfactory solution, you’re more than welcome to answer yourself. In fact, it is explicitly encouraged to answer your own question. :) – Ruixi Zhang Jul 22 '18 at 3:19