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

This question already has an answer here:

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!

share|improve this question

marked as duplicate by Werner, Jesse, ChrisS, Jubobs, Speravir Feb 8 '14 at 1:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – Jubobs Dec 6 '14 at 20:10

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).

share|improve this answer
Thanks for your help! – Joe Apr 29 '12 at 21:37
@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. – Harish Kumar 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;

share|improve this answer
Thanks for your help! – Joe Apr 29 '12 at 21:36
but we cannot call an mfile in listings environment, is there a way to solve this ? – Negin May 4 '12 at 10:56
@Negin how about \lstinputlisting?... – cmhughes May 4 '12 at 11:04
thanks it worked – Negin May 4 '12 at 20:29

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}"

share|improve this answer
Ah if combined with this .... tex.stackexchange.com/questions/115132/matlab-like-command-box :P – percusse Feb 8 '14 at 1:37
@percusse You don't get the same kind of highlighting when you call the help from the command line, actually. But I like Alan's answer :) – Jubobs Feb 8 '14 at 1:57

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