How can I attach MATLAB code in LaTeX without it looking like a report?

Click For Summary

Discussion Overview

The discussion revolves around how to effectively include MATLAB code in a LaTeX document, specifically in the appendix of a technical report, while maintaining the appearance of the code rather than that of a standard report. Participants explore various methods and packages to achieve this formatting.

Discussion Character

  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant suggests using the "verbatim" environment to include MATLAB code.
  • Another participant recommends the listings package for a more sophisticated presentation of MATLAB code.
  • A later reply provides detailed settings for the listings package, including color coding for different elements of the code and options for line numbering.
  • Another participant mentions using a specific style file from MATLAB Central that worked well for their needs.

Areas of Agreement / Disagreement

Participants present multiple approaches to including MATLAB code, with no consensus on a single best method. Various opinions on the effectiveness of different packages and styles are expressed.

Contextual Notes

Some suggestions depend on specific LaTeX packages and configurations, which may not be universally applicable. The effectiveness of each method may vary based on individual requirements and document settings.

ladil123
Messages
42
Reaction score
0
Hello!

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 won't 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 ?

Danne
 
Physics news on Phys.org
Enter the MATLAB code in a "verbatim" environment. (I.e. \begin{verbatim}..code..\end{verbatim})
 
I second the use of the http://www.ctan.org/tex-archive/help/Catalogue/entries/listings.html" package. I put the following in my document preamble whenever I need to include MATLAB code in a LaTeX document:
Code:
\usepackage{listings}
\usepackage[usenames,dvipsnames]{color}
% This is the color used for MATLAB comments below
\definecolor{MyDarkGreen}{rgb}{0.0,0.4,0.0}

% For faster processing, load Matlab syntax for listings
\lstloadlanguages{Matlab}%
\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
        commentstyle=\usefont{T1}{pcr}{m}{sl}\color{MyDarkGreen}\small,
        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
        morekeywords={xlim,ylim,var,alpha,factorial,poissrnd,normpdf,normcdf},
        %
        %%% 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.
\newcommand{\matlabscript}[2]
  {\begin{itemize}\item[]\lstinputlisting[caption=#2,label=#1]{#1.m}\end{itemize}}
These settings are based on those used in a http://links.tedpavlic.com/ascii/homework_new_tex.ascii" .
 
Last edited by a moderator:
I used http://www.mathworks.com/matlabcentral/files/8015/mcode.sty" for this, and it did a fine job for me.
 
Last edited by a moderator:

Similar threads

  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 9 ·
Replies
9
Views
6K