How to include MATLAB code in LaTeX documents

Many packages exist that allow you to include source code of different programming languages into LaTeX document. The most popular once are probably listings and fancyvrb. However, due to their aim for being as general as possible, including MATLAB source code with the right color and markup can be tedious task. Fortunately, Florian Knorn over at MATLAB Central developed a simple style file that does this for you: meet mcode.

Set up

Download mcode.sty by clicking this link to the MATLAB Central File Exchange. If that link every breaks down, I’ve uploaded the file to the howtoTeX.com server as well: here.

Now, put the mcode.sty file in the same folder as the (master) .tex file you’re compiling. Also include the following line of code in your preamble:

\usepackage[]{mcode}

Some options could be added between the square brackets:

  • bw: For black and white printing (highlighting will be done based on grayscale)
  • numbered: For numbered lines
  • framed: Puts a frame around each piece of code
  • final: This one is for the case where the draft option is enable in the documentclass. The package will then output no code at all, unless the final option is included.

Note that the mcode package is based on the listings package (it just includes some predefined setup such that the markup and color is according to the MALTAB standard).

Usage

Including MATLAB code can be done in two ways. First, just including the code by copy and pasting:

\begin{lstlisting}
a = 1;    % MATLAB comment
b = 2;
c = a^2 + b^2;
\end{lstlisting}

The second option is to include the source code right away from the .m file:

\lstinputlisting{/path_to_mfile/my_mfile.m}

That’s all about the mark up of MATLAB source code in LaTeX documents.

33 Comments

  1. Excellent blog here! Also your website loads up fast!
    What host are you using? Can I get your affiliate link to your host?
    I wish my web site loaded up as fast as yours lol

  2. each time i used to read smaller content that
    as well clear their motive, and that is also happening with this post which I am reading at
    this place.

  3. Wilburn says:

    you are in reality a excellent webmaster. The web
    site loading velocity is incredible. It seems that you are
    doing any unique trick. Furthermore, The contents are masterpiece.
    you’ve done a excellent task on this topic!

  4. having sex says:

    Greetings! I know this is kinda off topic but I was wondering which blog platform are you using for this site?
    I’m getting sick and tired of WordPress because I’ve had problems with hackers and I’m looking at options for another platform.
    I would be fantastic if you could point me in the direction of
    a good platform.

  5. Together with everything which seems to be building throughout this specific area,
    a significant percentage of points of view are quite stimulating.
    However, I am sorry, but I do not give credence to your entire suggestion,
    all be it radical none the less. It would
    seem to everybody that your comments are actually not entirely rationalized and in simple fact you
    are generally your self not even entirely certain of the point.
    In any event I did take pleasure in examining it.

    I do enjoy the way you have framed this particular

  6. Sondra says:

    I’m extremely inspired along with your writing skills as smartly
    as with the structure to your weblog. Is this a paid subject
    matter or did you modify it yourself? Either way keep up the excellent high quality writing, it is uncommon to peer a great weblog like this one nowadays..

  7. Organo Gold says:

    You can definitely see your skills in the article you write.
    The world hopes for even more passionate
    writers like you who aren’t afraid to say how they believe.
    All the time go after your heart.

  8. Hello there! Do you know if they make any plugins to safeguard against hackers?
    I’m kinda paranoid about losing everything I’ve worked hard on.

    Any suggestions?

  9. Any employer thaat can latch onto a manger with both wisdom and industry has won a
    victory. 2 District oof tthe Federal Reserve System, and
    the first Bolshevik “ambassador” to the United States and head of the Soviet Bureau,
    Ludwig Martens, the vice president of Weinberg and Posner.

    “My door is always open, Spencer,” she replied, “And I’ll be praying for your success.

  10. Bridgett says:

    I want to to thank you for this wonderful read!! I definitely enjoyed every bit of it.
    I’ve got you book-marked to look at new
    things you post…

  11. I believe what you said made a lot of sense. But, think on this, suppose you added a little information?
    I mean, I don’t want to tell you how to run your website, however
    suppose you added something to maybe grab people’s attention?
    I mean How to include MATLAB code in LaTeX documents | howtoTeX.com – Your LaTeX
    resource site is kinda plain. You should peek at Yahoo’s home page and note
    how they create post titles to grab viewers to click.
    You might try adding a video or a picture or two to grab people interested about everything’ve written.
    Just my opinion, it might bring your posts a little livelier.

  12. funciona says:

    Hi there I am so excited I found your blog page, I really found you
    by mistake, while I was researching on Bing for something else, Nonetheless I am
    here now and would just like to say kudos for a
    tremendous post and a all round entertaining blog (I also love
    the theme/design), I don’t have time to look over it all at the
    moment but I have saved it and also added your RSS feeds, so when I
    have time I will be back to read a lot more, Please do
    keep up the superb b.

  13. videosexe says:

    Hi there! I know this is kind of off topic but I was wondering if you knew
    where I could locate a captcha plugin for my comment form?

    I’m using the same blog platform as yours and I’m having trouble finding one?
    Thanks a lot!

  14. Janet says:

    Appreciation to my father who shared with me concerning this website,
    this weblog is in fact remarkable.

  15. Heya i am for the first time here. I found this board and I to find It truly helpful & it helped me out a lot.
    I hope to offer one thing again and aid others like you
    aided me.

  16. Hi, Neat post. There’s an issue with your website in internet explorer, would test this?
    IE nonetheless is the marketplace chief and a large component of folks will pass over your
    magnificent writing because of this problem.

  17. Salomon Lab says:

    Incredible! This blog looks exactly like my old one!
    It’s on a completely different subject but it has pretty
    much the same layout and design. Superb choice of colors!

  18. Katia says:

    I think the admin of this web site is in fact working
    hard in favor of his website, because here every material is quality based data.

  19. Heya i am for the first time here. I found this board and I to find It really
    helpful & it helped me out much. I’m hoping to provide one thing back and
    aid others such as you aided me.

  20. Conceptualized by Nintendo visionary Shigeru Miyamoto, Mario has enjoyed
    over 30 years of video game superiority, with titles such as Donkey
    Kong, Super Mario 64, and Paper Mario and many more ranking among the top must-buy games of
    generations of gamers. He is seen pulling himself on to a legde, ala Sonic Adventure 2.
    However, competition is keen so the amount is usually very reasonable.

  21. Aw, this was an incredibly nice post. Taking the time and actual effort to produce a very
    good article… but what can I say… I put things
    off a whole lot and never manage to get nearly anything done.

  22. Together with every little thing which appears to be
    developing within this subject material, many of your points of viw tend to be soomewhat radical.
    On the otther hand, I appologize, because I can not subscribe to your entire idea, all be it stimulating
    none the less. It seems to me that your comments are generally not
    completely rationalized and iin actuality you are generally your self
    not really thoroughly confident of the point. In
    aany event I did appreciate examining it.
    I doo likle the way you have frameed this particular

  23. I’ll immediately snatch your rss as I can not in finding your e-mail subscription hyperlink or e-newsletter service.

    Do you have any? Kindly permit me recognise so that I may just subscribe.

    Thanks.

  24. Highly energetic blog, I loved that a lot. Will there be a part
    2?

  25. Really no matter if someone doesn’t understand afterward its up to other visitors that they will help, so here it
    happens.

  26. Rocky says:

    Your style is very unique in comparison to other people I’ve read stuff from.
    I appreciate you for posting when you have the
    opportunity, Guess I’ll just book mark this site.

  27. Whats up are using WordPress for your blog platform?
    I’m new to the blog world but I’m trying to get started and set
    up my own. Do you require any html coding expertise to make your own blog?

    Any help would be really appreciated!

  28. Franklyn says:

    This seems to have issues with the single quote transpose operator. any fix for that ?

  29. Velibor says:

    I followed the “cookbook” step by step but for me it doesn’t work, in the place of the text where the script should be I have blank paper or some dashes… weird. Anyone know whats going on?

  30. If it just contains settings for Matlab, best would be to submit an official patch to the maintainer.

    As far as I can tell the current solution is quite sub-optimal if one has listings from different languages. Besides that it would be a good idea to keep it in one package.

    • Frits says:

      I agree. However, this is a way to go when you just want to display some MATLAB source code in the correct markup without setting up the style yourself. It could be useful for (lab) reports and homework assignments.

  31. Ben Abbott says:

    Will the mcode.sty file be added to CTAN?

Leave a Reply




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>