# Putting in MATLAB codes in a box as figures? [duplicate]

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!

-

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

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:

%%
%Initialization
clear all
clc
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(1,:,1,1,1)=0+rand(1,s)*(1-(0))';
P(2,:,1,1,1)=0+rand(1,s)*(1-(0))';
%P(3,:,1,1,1)= .2*rand(s,1)';
end


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:

\documentclass{article}
\usepackage[framed,numbered,autolinebreaks,useliterate]{mcode}
\begin{document}
\begin{figure}
\caption{My program}
\lstinputlisting{ff16.m}
\end{figure}
\end{document}


The result will be:

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:

\documentclass{article}
\usepackage[framed,numbered,autolinebreaks,useliterate]{mcode}
\begin{document}
%\begin{figure}
%\caption{My program}
\lstinputlisting[caption=My code here]{ff16.m} % note the caption here.
%\end{figure}
\end{document}


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

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

-
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

\documentclass{article}
\usepackage{listings}

\begin{document}

\lstset{language=matlab,frame=single}

\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;
end
\end{lstlisting}

\end{document}

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

\documentclass{article}

\usepackage{lipsum}
\usepackage[T1]{fontenc}

\usepackage{filecontents}
\begin{filecontents*}{sample.m}
% create a file for output
!touch testFile.txt
fid = fopen('testFile.text', 'w')
for i=1:10
fprintf(fid,'%6.2f \n', i);
end
\end{filecontents*}

\usepackage[framed,numbered]{matlab-prettifier}
\let\ph\snippetPlaceholder
\lstset
{
style = Matlab-editor,
escapechar      = ",
}

\begin{document}

\lstlistoflistings

\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