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

In summary, attaching MATLAB code in LaTeX can be done by using the "listings" package, which allows for formatting of code snippets within a LaTeX document. This can be done by specifying the programming language, adjusting the style and layout, and including line numbers if desired. Additionally, using the "matlab-prettifier" package can further enhance the appearance of the code. It is important to properly format and label the code in order to avoid it looking like a report and maintain readability.
  • #1
ladil123
45
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
  • #2
Enter the MATLAB code in a "verbatim" environment. (I.e. \begin{verbatim}..code..\end{verbatim})
 
  • #4
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:
  • #5
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:

1. What is the purpose of attaching matlab code in latex?

Attaching matlab code in latex allows for a seamless integration of code and mathematical equations in a document. It also enhances the readability and reproducibility of the code.

2. How do I attach matlab code in latex?

There are a few steps to follow in order to attach matlab code in latex. First, you need to include the package "listings" in the preamble of your document. Then, use the "lstlisting" environment to insert the code in your document. You can also customize the appearance of the code using various options provided by the "listings" package.

3. Can I include only parts of my matlab code in latex?

Yes, you can choose to include specific parts of your matlab code in latex by using the "lstinputlisting" command. This allows you to include certain sections or functions from your code without having to include the entire file.

4. How do I format my matlab code in latex?

The "listings" package offers various formatting options for your matlab code in latex. You can choose the programming language, adjust the indentation, change the font size and style, and add line numbers for better readability. You can also customize the appearance of comments, keywords, and operators in your code.

5. Can I add comments or explanations to my matlab code in latex?

Yes, you can add comments or explanations to your matlab code in latex by using the "lstlisting" environment. You can also add comments within the code using the "%" symbol, which will not be displayed in the final document. This allows you to provide context and explanations for your code within the latex document.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
908
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
745
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
Back
Top