How can I include MATLAB code in my LaTeX document?
The matlab-prettifier
package can be used to typeset MATLAB code and was written in response to a question on tex.stackexchange.
The lstlisting
environment
After including the matlab-prettifier
package in your document via
\usepackage{matlab-prettifier}
you can use the lstlisting
environment to typeset MATLAB code:
\begin{lstlisting}[<options>]
MATLAB code...
\end{lstlisting}
where <options>
is a comma-separated list of parameters whose values are set using key–value pairs (i.e., parameter=value
).
- Note: The
matlab-prettifier
package is built around thelistings
package, enabling use of features provided bylistings
, including parameters within<options>
of thelstlisting
environment. However, somelistings
parameters should not be used, and readers are advised to consult thematlab-prettifier
documentation for further details.
The visual appearance of typeset MATLAB code can be changed using the style
parameter, which supports three values:
Matlab-editor
Matlab-bw
Matlab-Pyglike
The following examples demonstrate how to use those styles.
Examples
Here is an example, using MATLAB code from matlab-prettifier
’s documentation, which applies the Matlab-editor
style:
\documentclass[a4paper]{article}
\usepackage{matlab-prettifier}
\begin{document}
\begin{lstlisting}[style=Matlab-editor]
%% Sample Matlab code
!mv test.txt test2.txt
A = [1, 2, 3;... foo
4, 5, 6];
s = 'abcd';
for k = 1:4
Disp(s(k)) % bar
end
%{
create row vector x, then reverse it
%}
x = linspace(0,1,101);
y = x(end:-1:1);
\end{lstlisting}
\end{document}
This example produces the following output:
The next example set values for the frame
and
numbers
parameters provided by the listings
package:
\documentclass[a4paper]{article}
\usepackage{matlab-prettifier}
\begin{document}
\begin{lstlisting}[
frame=single,
numbers=left,
style=Matlab-Pyglike]
%% Sample Matlab code
!mv test.txt test2.txt
A = [1, 2, 3;... foo
4, 5, 6];
s = 'abcd';
for k = 1:4
Disp(s(k)) % bar
end
%{
create row vector x, then reverse it
%}
x = linspace(0,1,101);
y = x(end:-1:1);
\end{lstlisting}
\end{document}
This example produces the following output:
Using MATLAB code stored in a file
Instead of including MATLAB code directly within your LaTeX document, use the \lstinputlisting
command to import and typeset code contained in an external file:
\lstinputlisting[<options>]{<filename>}
where
<options>
is a comma-separated list of parameters provided as key–value pairs (see above);<filename>
is the path to a file containing MATLAB code.
For example, to import the file sample.m
and typeset it in the Matlab-bw
style you would write
\lstinputlisting[style=Matlab-bw]{sample.m}
The next example places a frame around the code, numbers the lines of code (on the left), and uses the Matlab-bw
style:
\lstinputlisting[
frame=single,
numbers=left,
style=Matlab-editor
]{sample.m}
Select the Open this example in Overleaf
link (see below) to automatically create an Overleaf project which includes the sample.m
MATLAB code file.
main.tex
\documentclass[a4paper]{article}
\usepackage{matlab-prettifier}
\begin{document}
\title{Importing and typesetting MATLAB code contained in a file}
\lstinputlisting[
frame=single,
numbers=left,
style=Matlab-bw
]{sample.m}
\end{document}
sample.m
%% Sample Matlab code
!mv test.txt test2.txt
A = [1, 2, 3;... foo
4, 5, 6];
s = 'abcd';
for k = 1:4
Disp(s(k)) % bar
end
%{
create row vector x, then reverse it
%}
x = linspace(0,1,101);
y = x(end:-1:1);
Open this example in Overleaf (it creates the sample.m
file).
This example produces the following output: