以下代码无法高亮作为关键字。我做错了什么?不应该作为大胆一点进口?
\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}
答案1
morekeyword=as
如果您声明该选项,则该选项有效后告知listings
要使用的语言是 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}
为 Python 定义一个自己的环境可能更好:
\lstnewenvironment{Python}[1][]
{\lstset{language=Python,
morekeywords=as,
#1}%
}
{}
这样就可以写
\begin{Python}
<Python code>
\end{Python}
可能会提供其他本地选择,比如
\begin{Python}[basicfamily=\ttfamily]
<Python code>
\end{Python}