[Tex/LaTex] Problems with copy and paste from PDF using lstlisting

copy/pastelistingspdf

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
  commentstyle=\color{dkgreen},       % comment 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?

Best Answer

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.

However, if you add also

literate={-}{-}1,

then the substitution will not be performed anyway.

Related Question