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

    \documentclass[12pt,letterpaper]{article}
    \usepackage{fancyvrb}
    \DefineVerbatimEnvironment{code}{Verbatim}{fontsize=\small}
    \DefineVerbatimEnvironment{example}{Verbatim}{fontsize=\small}
    
    \begin{document}
    \begin{code}
    addpath('~/plot'); 
    \end{code}
    \end{document}
    

    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

1 Answer 1

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

\lstset{language={},
  keepspaces=true,
  xleftmargin=\parindent,
  xrightmargin=\parindent,
  basicstyle=\ttfamily,
  frame=single,
  framesep=2mm,
  framerule=0pt,
  columns=flexible,
  backgroundcolor=\color[gray]{0.9}}

and then called it with

\begin{lstlisting}
function sc(n)

for i = 1:n
    disp([i, i^2, i^3])
end  
\end{lstlisting}

I have also been using it for student notes.

share|improve this answer

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.