# Python “as” keyword ignored in Listings

Following code cant highlight as keyword. What am I doing wrong? Shouldn't as be bold like import?

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}

\lstset{morekeywords={as}}

\title{Python listings}
\author{me}

\begin{document}

\maketitle

\section{Introduction}
Python example

\begin{lstlisting}[language=Python]
#!/usr/bin/env python
"""
An animated image
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
\end{lstlisting}

done with the example.
\end{document}

-
Using \lstset{otherkeywords={as}} seems to work. From the documentation it appears that otherkeywords is intended to be used for keywords that may contain other characters or begin with numbers, so perhaps it is a bug that morekeywords did not work. – Peter Grill Sep 24 '12 at 16:52
I can confirm that otherkeywords works. I didn't find a valid reason (in listings documentation) to morekeywords ignore 'as', thus it seems it's a bug. Where can I report it? e-mail to the maintainer? – sivic Sep 24 '12 at 18:25
For bugs it is best to email the package maintainer, but I'd wait some time to see if someone provides an explanation here. But still good that you posted the question in case someone else encounters a similar issue. – Peter Grill Sep 24 '12 at 18:55

The morekeyword=as option works if you declare it after telling listings that the language to be used is Python:

\documentclass{article}
\usepackage{listings}

\begin{document}
\begin{lstlisting}[language=Python,morekeywords=as]
#!/usr/bin/env python
"""
An animated image
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
\end{lstlisting}
\end{document}


It's perhaps better to define an own environment for Python:

\lstnewenvironment{Python}[1][]
{\lstset{language=Python,
morekeywords=as,
#1}%
}
{}


so that it will be possible to write

\begin{Python}
<Python code>
\end{Python}


possibly giving other local options, say

\begin{Python}[basicfamily=\ttfamily]
<Python code>
\end{Python}

-
Thanks! It's helpful, but it's still a bug in listings? How to implement morekeywords to use it with \lstinputlisting[language=Python]{python_code.py}? – sivic Sep 25 '12 at 9:01
@sivic Why should be this considered a bug? The morekeywords are set on a per language basis. You may want to add as in Python, but not in C++ (just a random language). – egreg Sep 25 '12 at 9:05
@sivic The only way I see to add something to a predefined language is to define a new one: \lstdefinelanguage{sPython}[]{Python}{morekeywords=as}. – egreg Sep 25 '12 at 9:14
Yes! Defining a "new" (copy) language is nice way to solve this. Now I can put all stuff defined in \lstset in \lstdefinelanguage? – sivic Sep 25 '12 at 9:25