To Solve this problem a guy named Florian Knorn at the MATLAB central created a style file which lets you do this very task. He calls it the MCODE. You can download the mcode by clicking this link which will direct you to MATLAB Central.
How to use MCODE:
- To use mcode, first you have to download it and put it in the same directory as your .tex file which you're trying to compile.
- Then, include mcode as a package in your .tex file:
- Some options can be added between the square brackets:
- bw : Black and White
- numbered : Number the lines of your code
- framed : Draw a frame around your code
- Now to include MATLAB code one of two ways can be used.
\usepackage[]{mcode}
- Copying and Pasting the MATLAB code:
- Using the Path of the M-File:
\begin{lstlisting} clear; clc; len=1000; L2=zeros(1,len); index=1; \end{lstlisting}
\lstinputlisting{/path/mfile_name.m}
Complete Example:
\documentclass[12pt]{article} \usepackage[numbered, framed]{mcode} \begin{document} \title{MATLAB Code in LATEX} \author{Saliya} \date{} \maketitle Copy and Paste Method: \begin{lstlisting} clear; clc; len=1000; L2=zeros(1,len); index=1; \end{lstlisting} Using the path of the M-File: \lstinputlisting{mfile.m} \end{document}
No comments:
Post a Comment