# Extend a language with additional keywords?

I'm using listings and I want to extend a language with additional keywords for highlightning. I know there is an option morekeywords available, but that only works for a particular lstset declaration and I don't want to have to copy-paste all keywords into all listings. I've tried declaring the keywords as part of a command but that doesn't work (probably because the command is not expanded and I don't know how to fix that).

So is there any way of declaring a new language by copying an existing one and just adding more keywords?

-

You can define the keywords in a separate file and just include it in your preamble. Here is an example of adding for and downto as new keywords:

\documentclass{article}
\usepackage{listings}%
\usepackage{xcolor}

\lstset{%
backgroundcolor=\color{yellow!20},%
basicstyle=\small\ttfamily,%
numbers=left, numberstyle=\tiny, stepnumber=2, numbersep=5pt,%
}%

% Add your keywords here, and have this in a separate file
% and include it in your preamble
\lstset{emph={%
downto, for%
},emphstyle={\color{red}\bfseries\underbar}%
}%

\begin{document}
\begin{lstlisting}
y = 0
for i = n downto 0
y = a_i + x * y
\end{lstlisting}
\end{document}

-
I never thought about using a separate \lstset to define the keywords! This solved my problem! –  gablin Sep 13 '11 at 6:51

It seems that morekeywords also works for extending a language. For instance the Python keyword super isn't in the list of default keywords. In my preamble, I can set it as a keyword by doing:

\lstset{language=Python}
\lstset{
morekeywords={super}
}


Of course, it would be ideal to contribute those missing keywords to the listings package.

-
Works like a charm... –  mtsz Feb 28 '13 at 17:27

(Too long for a comment, hence posted as an "answer".)

I can't make that work. For some reason, \maketitle isn't in the default list of keywords for LaTeX, so I wanted to add it:

\documentclass{article}
\usepackage{listings}
\usepackage[svgnames]{xcolor}
\lstset{basicstyle=\small\color{Black}\ttfamily,
keywordstyle=\color{DarkGreen}\bfseries,
showstringspaces=false,
columns=fullflexible,
keepspaces=true}
\lstset{language={[LaTeX]TeX},morekeywords={maketitle}}
\begin{document}
Like this
\begin{lstlisting}[language={[LaTeX]TeX}]
\title{foo}
\author{bar}
\maketitle
\end{lstlisting}
\end{document}


But this does not render \maketitle like the other keywords. What have I misunderstood?

-
Your question won't be seen by many people here, so it would be best to repost it as a fresh question. Follow-up questions like this are more than welcome! Please use the "Ask Question" link for your new question; there you can link to this question to provide the background. –  Jubobs 19 hours ago
maketitle is indeed not defined as a "keyword" by listings. However, you can use a key called moretexcs to add it; don't use morekeywords for TeX languages. Also, invoking language={[LaTeX]TeX} in your listing won't use any of your settings. You should instead define a style saving all your settings and invoke that style in your listings (or possibly set that style globally). For instance: \lstdefinestyle{LaTeX-Flynn}{ language=[LaTeX]TeX, moretexcs={maketitle,foo}, showstringspaces=false, columns=fullflexible, keepspaces=true, texcsstyle=\color{DarkGreen}\bfseries, } –  Jubobs 19 hours ago
I've taken the liberty of inserting a header line that explains the posting of this material as a new answer rather than as a comment. –  Mico 4 hours ago