LaTeX forum ⇒ Graphics, Figures & TablesIncluding matlab code as a listing in an appendix Topic is solved

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

Including matlab code as a listing in an appendix

Postby gokotai » Thu Aug 11, 2022 12:42 pm

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:
\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 2021:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics
kaiserkarl13
Posts: 706
Joined: Tue Mar 25, 2008 5:02 pm

Including matlab code as a listing in an appendix

Postby kaiserkarl13 » Thu Aug 11, 2022 4:39 pm

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

Postby gokotai » Thu Aug 11, 2022 7:51 pm

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:

\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  Topic is solved

Postby gokotai » Fri Aug 12, 2022 10:31 am

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


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 7 guests