Problems with copy and paste from PDF using lstlisting

I'm using lstlisting to create course notes from which I am hoping that code can be copy&paste'd I've already implemented the suggestions in Phantom spaces in listings (pdf) to make sure that there are no spurious spaces, but now the problem is that minus signs (-) are copied as non-ascii, and so when I paste the code into Matlab (in this case) I get an error. I'm assuming that this will happen with other characters, but at this point I have only trouble with the minus sign. I doubt it matters, but I've been using:

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

\lstset{ %
language=Matlab,                % the language of the code
basicstyle=\footnotesize,           % the size of the fonts that are used for the code
numbers=left,                   % where to put the line-numbers
numberstyle=\footnotesize,          % the size of the fonts that are used for the line-numbers
stepnumber=2,                   % the step between two line-numbers. If it's 1, each line
% will be numbered
numbersep=5pt,                  % how far the line-numbers are from the code
backgroundcolor=\color{white},      % choose the background color. You must add \usepackage{color}
showspaces=false,               % show spaces adding particular underscores
showstringspaces=false,         % underline spaces within strings
showtabs=false,                 % show tabs within strings adding particular underscores
frame=single,                   % adds a frame around the code
tabsize=2,                      % sets default tabsize to 2 spaces
captionpos=b,                   % sets the caption-position to bottom
breaklines=true,                % sets automatic line breaking
breakatwhitespace=false,        % sets if automatic breaks should only happen at whitespace
title=\lstname,                   % show the filename of files included with \lstinputlisting;
% also try caption instead of title
numberstyle=\tiny\color{gray},        % line number style
keywordstyle=\color{blue},          % keyword style
stringstyle=\color{mauve},         % string literal style
escapeinside={\%*}{*)},            % if you want to add a comment within your code
morekeywords={*,...}               % if you want to add more keywords to the set
basicstyle=\ttfamily,
columns=flexible
}

\begin{lstlisting}
% notice how after a % one can write comments which can be used to
% explain the code inline.
% it will actually not bother Matlab if you copy the comments
% together with the code
x=2; % This is our first guess
for k=1:30 % we will iterate 30 times
x=x-(tan(x)-x/3)/(sec(x)-1/3) %notice that we are not using k. That is OK.
end
\end{lstlisting}


I cannot manage to lift the example and drop it into Matlab with it complaining about the (-) signs.

Any ideas?

-
Try adding the option literate=*{-}{-}1 as part of your \lstset. Does that help? I was able to copy/paste an ASCII - using this. –  Werner Apr 11 '12 at 21:21
You might want to use the matlab-prettifier package; see this answer. –  Jubobs Apr 28 '14 at 15:28

There are a couple of errors in your input. You specify twice basicstyle, but the second time without terminating properly the preceding option. Therefore the listing doesn't appear neither in footnote size nor in typewriter type.

I'll repeat only the final lines:

  escapeinside={\%*}{*)},  % if you want to add a comment within your code
% morekeywords={*,...},      % if you want to add more keywords to the set
basicstyle=\footnotesize\ttfamily,
columns=flexible,
}


Remove the basicstyle=\footnotesize at the beginning. I've also commented the morekeywords option as it doesn't appear to have a good value.

When the font is not monospaced, listings changes the hyphen into a minus sign, which doesn't happen with monospaced font.

literate={-}{-}1,


then the substitution will not be performed anyway.

-

If you look at the verbatim package you can use the code of the \verbatim@font command. This will ensure that the minus sign is used and not a rule as is the case with some fonts. It will also disable all ligatures. Define your own \verbfont command

% Ensure that the minus sign is the "-" character in
% listings environments for cut and paste operations from pdf docs
\makeatletter
\newcommand*\verbfont{%
\normalfont\ttfamily\hyphenchar\font\m@ne\@noligs}
\makeatother


Then use it in the listing setup

\lstset{%
:
basicstyle   = \color{black}\raggedright\verbfont\footnotesize\selectfont,
:
}

-

I discovered an option extendedchars=false that seems to work.

-
It has also other actions, look at the documentation. –  egreg Apr 11 '12 at 21:48

I want to draw your attention to a little trick!

Even if you have setup everything perfectly, you still can get problems with the spacings when copying text from PDF.

For example you use this latex lines and export to PDF.

\begin{lstlisting}[language=bash]
suwind< $DATA_FOLDER/cmps.azim.su \ key=cdp min=1 max=1 > tmp1.su suwind<$DATA_FOLDER/cmps.azim.su \
key=cdp min=2 max=2 > tmp2.su
suwind< $DATA_FOLDER/cmps.azim.su \ key=cdp min=3 max=3 > tmp3.su suwind<$DATA_FOLDER/cmps.azim.su \
key=cdp min=4 max=4 > tmp4.su
suwind< $DATA_FOLDER/cmps.azim.su \ key=cdp min=5 max=5 > tmp5.su \end{lstlisting}  If I select this in a pdf only blocks will be selected and spaces ignored. !!! IF YOU DO THIS LITTLE TRICK !!! Just break the symmetry of this by adding a space after the first suwind, thus: \begin{lstlisting}[language=bash] suwind <$DATA_FOLDER/cmps.azim.su \
key=cdp min=1 max=1 > tmp1.su
suwind< $DATA_FOLDER/cmps.azim.su \ key=cdp min=2 max=2 > tmp2.su suwind<$DATA_FOLDER/cmps.azim.su \
key=cdp min=3 max=3 > tmp3.su
suwind< $DATA_FOLDER/cmps.azim.su \ key=cdp min=4 max=4 > tmp4.su suwind<$DATA_FOLDER/cmps.azim.su \
key=cdp min=5 max=5 > tmp5.su
\end{lstlisting}


You can select the full text properly. Don't ask me why but it works.

By the way I use this definition:

\lstset{
basicstyle=\ttfamily\scriptsize\color{dark-gray},% dont use footnotesize. it will generate empty spaces when copy paste
showstringspaces=false,         % show spaces adding particular underscores
%frame=single,                  %gives black frame around code
columns=fullflexible,           %very important: otherwise when copy paste text, empty spaces are inserted, also DONT use: footnotesize
extendedchars=true,
upquote=true,% ensure that backtick displays correctly