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.

A good place to start

Share:
3,375

Related videos on Youtube

Spacey
Author by

Spacey

Updated on October 01, 2020

Comments

  • Spacey
    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.
      Torbjørn T. about 10 years
    • Spacey
      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.
      Torbjørn T. about 10 years
      mcode isn't part of either MikTeX or TeX Live, so either use plain listings or download and install mcode: mathworks.com/matlabcentral/fileexchange/…
    • Spacey
      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
      Francis about 10 years
      How about substitute every % in your code with \%?
    • Torbjørn T.
      Torbjørn T. about 10 years
      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.
      Torbjørn T. about 10 years
      Or not use mcode at all, just plain listings as in tex.stackexchange.com/a/75124/586
    • jub0bs
      jub0bs over 9 years
      You might be interested in the matlab-prettifier package; see this answer.
  • karlkoeller
    karlkoeller about 10 years
    I think what you wrote in your answer has been extensively said in the above comments...