1

I am trying to make a simple document, showing MATLAB code, and some images. This is my template that I am using.

\documentclass{article}
\usepackage{graphicx}
\usepackage{float}
\usepackage{amsmath}
\usepackage{amsfonts}

% Correct bad hyphenation here
\hyphenation{}

\begin{document}

I want to put MATLAB code here.

\begin{figure}[H]
\begin{center}
\includegraphics[width=6in]{case2Rect.png}
\caption{Blah}
\label{fig1}
\end{center}
\end{figure}


\end{document}

The problem is that MATLAB code is full of comments that use '%', and so when I try to compile using pdfLaTeX, it crashes. I am using TexMaker btw. Anyway, I was wondering what technique might be used to go around this problem. I simply need to show MATLAB code, along with some images. I am relatively new to LaTeX.

Thanks.

Spacey
  • 1,159
  • There have been a few similar questions, see if any of these help: http://tex.stackexchange.com/questions/75116/inserting-matlab-code-in-the-appendix , http://tex.stackexchange.com/questions/84777/how-to-get-matlab-code-into-a-latex-document http://tex.stackexchange.com/questions/69382/inline-matlab-code – Torbjørn T. Sep 30 '13 at 18:30
  • @TorbjørnT. Thanks, I now get this error "! LaTeX Error: File `mcode.sty' not found. Type X to quit or to proceed, or enter new name. (Default extension: sty) Enter file name: ! Emergency stop." when I just simply try to add the mcode package. I thought I had this setup to update packages automatically... – Spacey Sep 30 '13 at 18:37
  • mcode isn't part of either MikTeX or TeX Live, so either use plain listings or download and install mcode: http://www.mathworks.com/matlabcentral/fileexchange/8015-m-code-latex-package – Torbjørn T. Sep 30 '13 at 18:39
  • @TorbjørnT. Ok... I guess my question then is how do I set up TexMaker to automatically download packages... :-/ I honestly do not know how to install packages. I tried once and it was a complete and utter nightmare. – Spacey Sep 30 '13 at 18:49
  • How about substitute every % in your code with \%? – Francis Sep 30 '13 at 18:52
  • Texmaker doesn't really have anything to do with package installing. If you use MikTeX then you can have MikTeX install missing packages automatically, but not mcode. mcode is not on CTAN, and not in the databases of MikTeX, so it wouldn't work for that anyway. A one-time solution is to just place mcode.sty in the same folder as your .tex document. – Torbjørn T. Sep 30 '13 at 18:52
  • Or not use mcode at all, just plain listings as in http://tex.stackexchange.com/a/75124/586 – Torbjørn T. Sep 30 '13 at 18:53
  • You might be interested in the matlab-prettifier package; see this answer. – jub0bs Apr 28 '14 at 16:37

1 Answers1

2

You can use the Listings package to include all kinds of code into Latex. The listings package supports syntax highlighting of a number of languages. You can also define own styles.

A good place to start

Dohn Joe
  • 1,912