# Package “listings” command “morekeywords” doesn't work

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}

\lstset{
morekeywords={classdef}
}

\lstset{
language=Matlab,
keywordstyle=\color{javaLila},
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.

• You have to put that last as it just gets overridden! Or better even, put it in the other lstset group. – Johannes_B Sep 6 '14 at 15:30
• A part of your gloabl options only work with KOMA-classes and even worse, they are outdated. – Johannes_B Sep 6 '14 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 Sep 6 '14 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 Sep 7 '14 at 8:56

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}

\lstdefinestyle{myMatlab}{
language=Matlab,
keywordstyle=\color{javaLila},
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}

• Defining my own style worked as intended :) Thanks! Which of my document options are now obsolete? – Atlan Sep 6 '14 at 15:45
• I just added a remark above. Please be aware of this. – Johannes_B Sep 6 '14 at 15:45