How to get Matlab code into a LaTeX document?
16,934
I personally prefer the minted
package. It's a little trouble to set up by the output is very neat and tidy -- and it has syntax highlighting.
Output:
Code:
\documentclass{article}
\usepackage{minted}
\begin{document}
\definecolor{bg}{rgb}{0.95,0.95,0.95}
\begin{minted}[linenos=true,bgcolor=bg]{matlab}
% Gradient descent algo for linear regression
% author: Nauman (recluze)
%set the data
X=[1 1 1 1 1 1 1; 22 49 80 26 40 54 91];
Y=[20 24 42 22 23 26 55];
hold on;
plot(X(2,:),Y, 'x');
% set the actual values of W
W = [5.775 0.474]';
YAct = (W' * X);
% GRADIENT DESCENT
W = zeros(2,1); % initialize W to all zeros
m = size(X,2); % number of instances
n = size(W,1); % number of parameters
alpha = 0.000025; % gradient descent step size
disp('Starting Weights:');
\end{minted}
\end{document}
You'll have to call pdflatex
with --shell-escape
though and you will have to install a package that provides pygmentize
command.
Related videos on Youtube
Author by
Mazzy
Updated on August 01, 2022Comments
-
Mazzy over 1 year
I'm trying to put in my thesis matlab code but the results are not very good. I would get something like that but I don't know what packages using and how to create this result:
-
Stephan Lehmke almost 11 yearsPlease reformulate the title of your question. I think the
listings
package is what you're looking for. -
Torbjørn T. almost 11 yearsThere's also
mcode
, which useslistings
. -
G. Poore almost 11 yearsThere's also
pythontex
andminted
, which use the Python syntax highlighting library Pygments. These can highlight Matlab code and Matlab interactive sessions. -
Federico Poloni almost 11 yearsWhatever solution you choose, please make sure that the resulting code listing is copy-and-pasteable. Your 25 readers will hate it otherwise.
-
jub0bs over 9 yearsThe
matlab-prettifier
package is your friend, here; see this answer.
-