TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

enter image description here

share|improve this question
Please reformulate the title of your question. I think the listings package is what you're looking for. – Stephan Lehmke Nov 29 '12 at 12:16
There's also mcode, which uses listings. – Torbjørn T. Nov 29 '12 at 12:25
There's also pythontex and minted, which use the Python syntax highlighting library Pygments. These can highlight Matlab code and Matlab interactive sessions. – G. Poore Nov 29 '12 at 13:10
Whatever solution you choose, please make sure that the resulting code listing is copy-and-pasteable. Your 25 readers will hate it otherwise. – Federico Poloni Nov 29 '12 at 13:13
The matlab-prettifier package is your friend, here; see this answer. – Jubobs Feb 13 '14 at 23:15
up vote 3 down vote accepted

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.


enter image description here






% 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);

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:');


You'll have to call pdflatex with --shell-escape though and you will have to install a package that provides pygmentize command.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.