11

I'm currently using the listings package to display my matlab code in Latex.

However, when trying to add new keywords to the package, nothing happens. Here's a MWE:

\documentclass[a4paper,12pt,parskip,bibtotoc,liststotoc]{article}

\usepackage[usenames,dvipsnames]{color}
\usepackage{listings}

\definecolor{javaBlue}{RGB}{42,0.0,255}
\definecolor{javaGreen}{RGB}{63,127,95}
\definecolor{javaLila}{RGB}{127,0,85}

\lstloadlanguages{Matlab} % load java, needed because of option 'savemem'

\lstset{
 morekeywords={classdef}
}

\lstset{
language=Matlab,
keywordstyle=\color{javaLila},
commentstyle=\color{javaGreen},
stringstyle=\color{javaGreen},
numbers=left,
stepnumber=1,
numbersep=5pt,
numberstyle=\tiny,
breaklines=true,
breakautoindent=true,
breakatwhitespace=false,
postbreak=\space,
tabsize=2,
basicstyle=\ttfamily\scriptsize,
showspaces=false,
showstringspaces=false,
extendedchars=true,
backgroundcolor=\color{white}}

\begin{document}
\lstinputlisting[language=Matlab]{"E:/Program Files/MATLAB/projects/master/classes /Customer.m"}
\end{document}

I have no idea why it doesn't work, as all respective threads in this forum just tell me to use "morekeywords". Seems like no one has my problem.

4
  • 1
    You have to put that last as it just gets overridden! Or better even, put it in the other lstset group.
    – Johannes_B
    Commented Sep 6, 2014 at 15:30
  • A part of your gloabl options only work with KOMA-classes and even worse, they are outdated.
    – Johannes_B
    Commented Sep 6, 2014 at 15:32
  • I tried putting morekeywords at the beginning, at the end, and in a seperate lstset-command as depicted in my MWE. None of these three approaches did work :|
    – Atlan
    Commented Sep 6, 2014 at 15:33
  • @Atlan For information, if you use the matlab-prettifier package, there is no need to add classdef to the list of MATLAB keywords, because the package already does that for you.
    – jub0bs
    Commented Sep 7, 2014 at 8:56

1 Answer 1

10

When loading a language specifically as the optional argument of a listings environment, the language defaults are being used. Define your own style. Right now, everything you set using lstset i completely ignored. You can even define a new environment saving you some typing.

\documentclass[a4paper,12pt,parskip,bibtotoc,liststotoc]{article}
%Only works with KOMA, and by the way some are obsolet by now

\usepackage[usenames,dvipsnames]{color}
\usepackage{listings}

\definecolor{javaBlue}{RGB}{42,0.0,255}
\definecolor{javaGreen}{RGB}{63,127,95}
\definecolor{javaLila}{RGB}{127,0,85}

\lstloadlanguages{Matlab} % load java, needed because of option 'savemem'

\lstdefinestyle{myMatlab}{
language=Matlab,
keywordstyle=\color{javaLila},
commentstyle=\color{javaGreen},
stringstyle=\color{javaGreen},
numbers=left,
stepnumber=1,
numbersep=5pt,
numberstyle=\tiny,
breaklines=true,
breakautoindent=true,
breakatwhitespace=false,
postbreak=\space,
tabsize=2,
basicstyle=\ttfamily\scriptsize,
showspaces=false,
showstringspaces=false,
extendedchars=true,
backgroundcolor=\color{white},
 morekeywords={classdef}%copy and paste
 }


\begin{document}
\begin{lstlisting}[style=myMatlab]
classdef warning
\end{lstlisting}
\end{document}
2
  • Defining my own style worked as intended :) Thanks! Which of my document options are now obsolete?
    – Atlan
    Commented Sep 6, 2014 at 15:45
  • I just added a remark above. Please be aware of this.
    – Johannes_B
    Commented Sep 6, 2014 at 15:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .