Need to show MATLAB code in TeX document, using pdfLaTeX
3,375
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.
Related videos on Youtube
Author by
Spacey
Updated on October 01, 2020Comments
-
Spacey about 3 years
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.
-
Torbjørn T. about 10 yearsThere have been a few similar questions, see if any of these help: tex.stackexchange.com/questions/75116/… , tex.stackexchange.com/questions/84777/… tex.stackexchange.com/questions/69382/inline-matlab-code
-
Spacey about 10 years@TorbjørnT. Thanks, I now get this error "! LaTeX Error: File `mcode.sty' not found. Type X to quit or <RETURN> 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...
-
Torbjørn T. about 10 years
mcode
isn't part of either MikTeX or TeX Live, so either use plainlistings
or download and installmcode
: mathworks.com/matlabcentral/fileexchange/… -
Spacey about 10 years@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.
-
Francis about 10 yearsHow about substitute every
%
in your code with\%
? -
Torbjørn T. about 10 yearsTexmaker 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 placemcode.sty
in the same folder as your.tex
document. -
Torbjørn T. about 10 years
-
jub0bs over 9 yearsYou might be interested in the
matlab-prettifier
package; see this answer.
-
-
karlkoeller about 10 yearsI think what you wrote in your answer has been extensively said in the above comments...