Text FormattingImport of Matlab Code

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
rizias
Posts: 11
Joined: Mon Dec 12, 2011 4:50 am

Import of Matlab Code

Post by rizias »

Hello everyone,

Does anyone know an efficient way of importing matlab code in a latex document? I mean that this should be a report and the comments need to be visible (i cannot make them visible with listings) and moreover i cannot find any simple way to use m2tex.

Thank you in advance for your reply.


Regards
George

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Frits
Posts: 169
Joined: Wed Feb 02, 2011 6:02 pm

Import of Matlab Code

Post by Frits »

I always display my Matlab codes with the fancyvrb package:

Code: Select all

\usepackage{fancyvrb}
...
\begin{document}
\begin{Verbatim}
... Matlab code here ...
\end{Verbatim}
\end{document}
howtoTeX.com - Your LaTeX resource site (Tips, Tricks, Templates and more!)
Follow howtoTeX on twitter
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Import of Matlab Code

Post by localghost »

I'd be interested in the difficulties with the listings package that you mentioned. You could show your efforts in form of a minimal example along with a sample of Matlab code.


Thorsten
rizias
Posts: 11
Joined: Mon Dec 12, 2011 4:50 am

Re: Import of Matlab Code

Post by rizias »

Hello again,
I think verbatim is fine!
Thank you very much!
Regards
George
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Import of Matlab Code

Post by localghost »

Still can't comprehend the problems with the listings package.

Code: Select all

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{geometry}
\usepackage{xcolor}
\usepackage{listings}

\lstset{
  language=Matlab,
  basicstyle=\ttfamily,
  backgroundcolor=\color{lightgray!25},
  breaklines=true,
  columns=fullflexible,
  frame=single,
  commentstyle=\color{teal},
  identifierstyle=\textbf,
  keywordstyle=\color{magenta},
  numbers=left,
  numberstyle=\footnotesize\sffamily,
  showstringspaces=false
}

\begin{document}
  \begin{lstlisting}[gobble=4]
    % simultaneousEquations.m
    % (c) 2001 g.m.dick
    %
    % solution of Ax = b using matrix methods
    %
    clear

    A = [-1 1 2 ; 3 -1 1 ; -1 3 4]; % coefficients
    b = [2 6 4]';                   % constants - COLUMN vector!!!

    % if singular system - bail out now!
    if det(A) == 0
        error('determinate of A = 0 - no solution exists')
    end

    % the solution
    x = A\b;    

    % print results
    format compact
    fprintf('\n\nThe coefficient matrix\n\n');
    A
    fprintf('\nThe constants\n\n');
    b
    fprintf('\nThe solution\n');
    x

    % =====================================================
    % alternate solution
    fprintf('\nThe alternate solution\n');
    xAlt = inv(A)*b
  \end{lstlisting}
\end{document}
Post Reply