Sign up ×
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:


\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

% 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.

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

Any ideas?

share|improve this question
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

4 Answers 4

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

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


then the substitution will not be performed anyway.

share|improve this answer
If the literate={-}{-}1 addition leaves you with minus signs that look like unary minuses (the unary minus is closer to the right symbol than to the left symbol), you should also add keepsaces=true to the lstset command. – Martin J.H. Sep 9 at 16:01

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

Then use it in the listing setup

        basicstyle   = \color{black}\raggedright\verbfont\footnotesize\selectfont,
share|improve this answer

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

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.

suwind< $DATA_FOLDER/ \
key=cdp min=1 max=1 >
suwind< $DATA_FOLDER/ \
key=cdp min=2 max=2 >
suwind< $DATA_FOLDER/ \
key=cdp min=3 max=3 >
suwind< $DATA_FOLDER/ \
key=cdp min=4 max=4 >
suwind< $DATA_FOLDER/ \
key=cdp min=5 max=5 >

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:

suwind < $DATA_FOLDER/ \
key=cdp min=1 max=1 >
suwind< $DATA_FOLDER/ \
key=cdp min=2 max=2 >
suwind< $DATA_FOLDER/ \
key=cdp min=3 max=3 >
suwind< $DATA_FOLDER/ \
key=cdp min=4 max=4 >
suwind< $DATA_FOLDER/ \
key=cdp min=5 max=5 >

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

By the way I use this definition:

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
upquote=true,% ensure that backtick displays correctly
commentstyle=\color{dark-gray}, %blue
keywordstyle=\color{dark-gray}  %red
share|improve this answer

Your Answer


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.