0% found this document useful (0 votes)
112 views2 pages

Highlighting MATLAB Code in LaTeX With Mcode

This document demonstrates three ways to include MATLAB code in LaTeX documents using the mcode.sty package: 1) Using the \mcode{} command for inline code snippets. 2) Using the lstlisting environment for code blocks with options like numbering, line breaks, and literate programming replacements. 3) Directly including external .m files using \lstinputlisting. It also notes that mcode.sty configures the listings package and to refer to its documentation for customization or troubleshooting.

Uploaded by

mojonchico
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views2 pages

Highlighting MATLAB Code in LaTeX With Mcode

This document demonstrates three ways to include MATLAB code in LaTeX documents using the mcode.sty package: 1) Using the \mcode{} command for inline code snippets. 2) Using the lstlisting environment for code blocks with options like numbering, line breaks, and literate programming replacements. 3) Directly including external .m files using \lstinputlisting. It also notes that mcode.sty configures the listings package and to refer to its documentation for customization or troubleshooting.

Uploaded by

mojonchico
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

mcode.

sty Demo

Florian Knorn, [email protected]

April 30, 2019

Usage — 3 ways

1) This inline demo for i=1:3, disp('cool'); end; uses the \mcode{} com-
mand.1

2) The following is a block using the lstlisting environment.

1 for i = 1:3
2 if i ≥ 5 % literate programming replacement
3 disp('cool'); % comment with some LATEXin it: πx2
4 end
5 [¬,ind] = max(vec);
6 x last = x(1,end);
7 v(end);
8 really really long really really long really really long ...
really really long really really long line % blaaaaaaaa
9 end

Note: Here, the package was loaded with the framed, numbered, autolinebreaks
and useliterate options. Please see the top of mcode.sty for a detailed
explanation of these options.

3) Finally, you can also directly include an external m-file from somewhere on
your hard drive (the very code you use in Matlab, if you want) using the
\lstinputlisting{/SOME/PATH/FILENAME.M} command. If you only want to
1 Works also in footnotes: for i=1:3, disp('cool'); end;

1
include certain lines from that file (for instance to skip a header), you can use
\lstinputlisting[firstline=6, lastline=15]{/SOME/PATH/FILENAME.M}.

Florian ([email protected])

NOTE — BEFORE YOU START


All that this package does is to configure the listings
package for you. If anything is not working the way you
want it, refer to the listings documentation first and /
or take a look at the mcode.sty file itself, which is well
documented internally.
The listings documentation can be accessed either by typ-
ing texdoc listings into a command prompt on your sys-
tem, or online:
http://mirrors.ctan.org/macros/latex/contrib/listings/listings.pdf

You might also like