Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

This question already has an answer here:

How do I copy code in to latex to get a result which looks like this:

enter image description here

...Without the red bars of course. I don't know how to get the numbering on the left side when I compile LaTeX. Using \verbatim only gives me the code.

Update: Having the code colored as it would appear in Matlab is necessary.

share|improve this question

marked as duplicate by Jubobs, Guido, Werner, cmhughes, Adam Liter Apr 28 at 16:31

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.

2  
\usepackage{listings} and then \begin{lstlisting}your code here \end{lstlisting}. texdoc listings for more info –  cmhughes Apr 3 '13 at 21:33
    
@cmhughes Do I need to use verbatim when I paste my code? –  AlanH Apr 3 '13 at 21:37
    
nope, shouldn't do- have a look at the documentation for more examples :) –  cmhughes Apr 3 '13 at 21:38
    
I feel the urge to comment on using nested loops in matlab :P –  percusse Apr 3 '13 at 22:02
    
@percusse It's an example of non-optimized code. Worse case scenario. –  AlanH Apr 3 '13 at 23:46

2 Answers 2

up vote 12 down vote accepted

For my Master thesis I was writing a lot of code in MatLab. In order to have it "pretty printed" I used the same package as @cmhughes mentioned. It looks like this (at the preamble):

\usepackage{listings} %For code in appendix
\lstset
{ %Formatting for code in appendix
    language=Matlab,
    basicstyle=\footnotesize,
    numbers=left,
    stepnumber=1,
    showstringspaces=false,
    tabsize=1,
    breaklines=true,
    breakatwhitespace=false,
}

Then, when you want to include your code file, just use \lstinputlisting. Continuing with the example:

\lstinputlisting[language=Matlab]{./Code/calc_error.m}

I particularly loved this because it allowed me to change my code in MatLab in the folder itself and I didn't have to update it in LaTeX, since I was pointing directly to the file and not the code per se.

The result will be something like this:

Code Example using <code>\usepackage{listings}</code> and setting it up with <code>\lstset</code>

Of course, you can always play with \lstset in order to get what you want, as explained in the documentation

share|improve this answer
2  
You can write \lstinputlisting{./Code/calc_error.m} since you have already specified language=matlab in your \lstset. –  hpesoj626 Apr 4 '13 at 1:13
    
For matlab mcode does pretty well. –  Harish Kumar Apr 5 '13 at 1:22

If you don't need code highlighting then the fancyvrb package will do. I don't use matlab, but for purposes of demonstration, suppose you have a file named yourfile.m, then the following code will give you left-side numbering. VerbatimInput{yourfile.m} inputs your file.

\documentclass[10pt]{article}

\usepackage{fancyvrb}
\fvset{%
fontsize=\small,
numbers=left
}

\begin{document}
\VerbatimInput{yourfile.m}
\end{document}

Another option is the minted. It internally uses fancyvrb and uses the Pygments library of python for syntax highlighting. Try the following example, replacing yourfile.m with your actual file. You have to compile with -shell-escape option enabled. Read the manual regarding the installation of the Pygments library.

\documentclass[10pt]{article}

\usepackage{minted}
\newmintedfile{matlab}{
linenos=TRUE,
fontsize=\small,
}
\usemintedstyle{trac}

\begin{document}
\matlabfile{yourfile.m}
\end{document}
share|improve this answer

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