how do I put MATLAB codes in a box or deal with them as if they are figures? Is there a way to do it within LaTeX itself? I am currently using the verbatim environment to present my codes and to be honest, I don't think they are very well presented!


I would use mcode from Matlabcentral for this purpose since it is customized for matlab.

Say, the contents of ff16.m is like this:

clear all   
for f=1:20
p=2;                         % dimension of search space 
s=26;                        % The number of bacteria 
Nc=36;                       % Number of chemotactic steps 
Ns=20;                        % Limits the length of a swim 
Nre=4;                       % The number of reproduction steps 
Ned=2;                       % The number of elimination-dispersal events 
Sr=s/2;                      % The number of bacteria reproductions (splits) per generation 
Ped=0.25;                    % The probabilty that each bacteria will be eliminated/dispersed 
c(:,1)=0.005*ones(s,1);       % the run length  
for m=1:s                    % the initital posistions 
   %P(3,:,1,1,1)= .2*rand(s,1)';

Now I want to insert this code in my tex file and I do not want to type or copy/paste it again. This is how it can be done provided ff16.m, my main.tex file and mcode.sty are all in the same folder:

\caption{My program}

The result will be:

enter image description here

Without figure environment, the code will have a box. Since you wanted to float this code like a figure, I have just used figure environment and every thing (that should work for a figure like a caption) should work as expected.

Better way to present a code would be:

%\caption{My program}
\lstinputlisting[caption=My code here]{ff16.m} % note the caption here.

Advantage here is the caption will read Listing 1: My code here instead of Figure 1:.... (You can change Listing 1: by \renewcommand{\lstlistingname}{My program} to get My program 1:)

enter image description here

Also, note that the long comments are wrapped automatically at the end of the line.

For details, refer to the mcode.sty file itself, from the above link (matlab central).

  • @Joe: If the answers are useful to you, you may consider accepting it (by clicking the green check mark near the left of the answer). so that it will be useful for others who search for similar things. Also you may consider upvoting it.
    – user11232
    Apr 29 '12 at 23:46

You can use the listings package for code snippets

texdoc listings

A simple MWE follows

enter image description here




\begin{lstlisting}[float,caption=My caption here]
function [] = wordsearch( words, N )

if nargin<1
    words{1} = 'monkey';
    words{2} = 'hippo';
    words{3} = 'girraffe';
    words{4} = 'crocodile';
    words{5} = 'kangaroo';

    N = 20;

  • but we cannot call an mfile in listings environment, is there a way to solve this ?
    – Prelude
    May 4 '12 at 10:56
  • 1
    @Negin how about \lstinputlisting?...
    – cmhughes
    May 4 '12 at 11:04

A more recent alternative to mcode is the matlab-prettifier package, which is built on top of listings and more closely mimicks the syntax highlighting offered by the Matlab editor.

If your readers are students of the language, matlab-prettifier also allows you to typeset placeholders in Matlab code snippets very easily (see the second listing below).

Because the package is also compatible with XeLaTeX and LuaLaTeX, you can use more exotic fonts than typewriter Computer Modern if you so wish.

enter image description here



% create a file for output
!touch testFile.txt
fid = fopen('testFile.text', 'w')
for i=1:10
  fprintf(fid,'%6.2f \n', i);

  style = Matlab-editor,
  escapechar      = ",



\lstinputlisting[caption=Sample code from Matlab]{sample.m}

\begin{lstlisting}[caption = {Simple syntax example}]
% syntax of the while loop using a placeholder
while "\ph{condition}"
  % do something useful
  if "\ph{something\_bad}"


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