TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
  1. I have some Matlab code which I would like to include in my LaTeX document.

    I use fancyvrb package, based on which I define a new environment called code. An complete and minimum example is:


    However, in the created pdf file, if I copy the code and paste it in Matlab command window, Matlab will complaint about the single quote is not the one used in Matlab. I was wondering why and how to solve the problem?

  2. To generalize, I also hope to know if there may possibly be other characters besides single quote that can cause such error? Not only in Matlab but also other programming languages?
  3. Also, I was wondering if my way of including code in LaTeX by using fancyvrb is the best? Do you have other nice ways to recommend?

Thanks and regards!

share|improve this question
up vote 5 down vote accepted

A few years ago I wrote a book about the use Matlab, and I used the listings package for all included code. This allows lots of options: escape to (La)TeX, coloured backgrounds, frames etc.

I set up my listings with


and then called it with

function sc(n)

for i = 1:n
    disp([i, i^2, i^3])

I have also been using it for student notes.

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.