Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
1  
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
    
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. –  Jubobs Mar 1 at 19:02
add comment

3 Answers

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.

share|improve this answer
add comment

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,
        :
       }
share|improve this answer
add comment

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

share|improve this answer
    
It has also other actions, look at the documentation. –  egreg Apr 11 '12 at 21:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.