6
  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!

1 Answer 1

5

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .