Graphics, Figures & TablesIncluding matlab code as a listing in an appendix

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
gokotai
Posts: 5
Joined: Thu Aug 11, 2022 12:35 pm

Including matlab code as a listing in an appendix

Post by gokotai »

Hi folks,

I'm using overleaf to write my phd thesis.

I have some matlab code which I'd like to include in my appendix section, so I've uploaded the ".mlx" file to a folder called "programmes" and have called the listings package in the preamble.

I have used the following lines to call the listing:

Code: Select all

\lstset{style=mystyle}
\lstinputlisting[language=Matlab, caption=xyz label=appendix:abc]{Programmes/myMatlab.mlx}
but the document no longer compiles! I used the same method to include some VBA code, so not sure where I've gone wrong. May I please ask for advice on this?

Recommended reading 2024:

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

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

kaiserkarl13
Posts: 707
Joined: Tue Mar 25, 2008 5:02 pm

Including matlab code as a listing in an appendix

Post by kaiserkarl13 »

This worked for me without the \lstset line with a short .m file that I included using the report class.

It may be something you changed in your "mystyle" info (whatever that is) or it may be something in your documentclass, neither of which you posted so we can't be sure.

I would say a Infominimal working example is in order.
gokotai
Posts: 5
Joined: Thu Aug 11, 2022 12:35 pm

Including matlab code as a listing in an appendix

Post by gokotai »

kaiserkarl13 wrote:This worked for me without the \lstset line with a short .m file that I included using the report class.

It may be something you changed in your "mystyle" info (whatever that is) or it may be something in your documentclass, neither of which you posted so we can't be sure.

I would say a Infominimal working example is in order.
Thank you! I've attached the relevant "mystyle" info and everything else in my preamble related to listings here:

Code: Select all

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\lstdefinestyle{mystyle}{
    backgroundcolor=\color{backcolour},   
    commentstyle=\color{codegreen},
    keywordstyle=\color{magenta},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{codepurple},
    basicstyle=\ttfamily\footnotesize,
    breakatwhitespace=false,         
    breaklines=true,                 
    captionpos=b,                    
    keepspaces=true,                 
    numbers=left,                    
    numbersep=5pt,                  
    showspaces=false,                
    showstringspaces=false,
    showtabs=false,                  
    tabsize=2
}

\renewcommand{\lstlistingname}{Programme}
\renewcommand{\lstlistlistingname}{Programmes}

\lstloadlanguages{Matlab}
\lstset{language=Matlab, 
        frame=single,    
        basicstyle=\small\ttfamily,
        keywordstyle=[1]\color{Blue}\bfseries,   
        keywordstyle=[2]\color{Purple},      
        keywordstyle=[3]\color{Blue}\underbar, 
        identifierstyle=,  
        commentstyle=\usefont{T1}{pcr}{m}{sl}\color{MyDarkGreen}\small,
        stringstyle=\color{Purple},  
        showstringspaces=false,     
        tabsize=5,    
        morekeywords={xlim,ylim,var,alpha,factorial,poissrnd,normpdf,normcdf},
        morekeywords=[2]{on, off, interp},
        morekeywords=[3]{binMask, RGBMask},
        morecomment=[l][\color{Blue}]{...},  
        numbers=left,          
        firstnumber=1,              
        numberstyle=\tiny\color{Blue},      
        stepnumber=5        
        }
\newcommand{\matlabscript}[2]
gokotai
Posts: 5
Joined: Thu Aug 11, 2022 12:35 pm

Including matlab code as a listing in an appendix

Post by gokotai »

So it turns out the error was because the files are .mlx format. Converting them to .m format fixed the issue!
Post Reply