Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Attach matlab code in latex

  1. Jun 7, 2009 #1

    Im writing a technical report with "article" as documentclass in Latex.

    I want to attach my matlab code in the appendix.
    If I copy and paste the text it wont look like the matlab code but it will look like a report..

    What command should I use so the text just looks like it is ?

  2. jcsd
  3. Jun 7, 2009 #2


    User Avatar
    Staff Emeritus
    Science Advisor

    Enter the matlab code in a "verbatim" environment. (I.e. \begin{verbatim}..code..\end{verbatim})
  4. Jun 7, 2009 #3


    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

  5. Jun 17, 2009 #4
    I second the use of the listings package. I put the following in my document preamble whenever I need to include MATLAB code in a LaTeX document:
    Code (Text):
    % This is the color used for MATLAB comments below

    % For faster processing, load Matlab syntax for listings
    \lstset{language=Matlab,                        % Use MATLAB
            frame=single,                           % Single frame around code
            basicstyle=\small\ttfamily,             % Use small true type font
            keywordstyle=[1]\color{Blue}\bf,        % MATLAB functions bold and blue
            keywordstyle=[2]\color{Purple},         % MATLAB function arguments purple
            keywordstyle=[3]\color{Blue}\underbar,  % User functions underlined and blue
            identifierstyle=,                       % Nothing special about identifiers
                                                    % Comments small dark green courier
            stringstyle=\color{Purple},             % Strings are purple
            showstringspaces=false,                 % Don't put marks in string spaces
            tabsize=5,                              % 5 spaces per tab
            %%% Put standard MATLAB functions not included in the default
            %%% language here
            %%% Put MATLAB function parameters here
            morekeywords=[2]{on, off, interp},
            %%% Put user defined functions here
            morekeywords=[3]{FindESS, homework_example},
            morecomment=[l][\color{Blue}]{...},     % Line continuation (...) like blue comment
            numbers=left,                           % Line numbers on left
            firstnumber=1,                          % Line numbers start with line 1
            numberstyle=\tiny\color{Blue},          % Line numbers are blue
            stepnumber=5                            % Line numbers go in steps of 5

    % Includes a MATLAB script.
    % The first parameter is the label, which also is the name of the script
    %   without the .m.
    % The second parameter is the optional caption.
    These settings are based on those used in a LaTeX homework template by Ted Pavlic.
  6. Jun 24, 2009 #5
    I used mcode.sty for this, and it did a fine job for me.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Attach matlab code in latex
  1. Matlab codes (Replies: 2)

  2. Matlab code (Replies: 0)

  3. Latex Code (Replies: 4)