我有一个pdflatex
项目需要 Python 和 SQL listings
。该listings
包运行良好,我能够使用以下代码添加和重新格式化 Python 的额外关键字:
\lstset{
language=python,
basicstyle=\small,
identifierstyle=\ttfamily,
keywordstyle=\bfseries\ttfamily\color[rgb]{0,0,1},
stringstyle=\ttfamily\color[rgb]{0.627,0.126,0.941},
morekeywords={cls,self,abstractmethod,classmethod,
__call__,__import__,__init__, __nonzero__,None}
}
但是,我还需要添加在 Python 中无效的额外 SQL 关键字,例如REFERENCES
。如何添加不同语言的关键字?
谢谢。
答案1
您可以定义两种不同的风格,甚至两种不同的环境。
\documentclass[]{article}
\usepackage{xcolor}
\usepackage{listings}
\lstdefinestyle{Python}
{
language=python,
basicstyle=\small,
identifierstyle=\ttfamily,
keywordstyle=\bfseries\ttfamily\color[rgb]{0,0,1},
stringstyle=\ttfamily\color[rgb]{0.627,0.126,0.941},
morekeywords={cls,self,abstractmethod,classmethod,
__call__,__import__,__init__, __nonzero__,None}
}
\lstdefinestyle{Sql}
{
language=SQL,
basicstyle=\small,
identifierstyle=\ttfamily,
keywordstyle=\bfseries\ttfamily\color[rgb]{0,0,1},
stringstyle=\ttfamily\color[rgb]{0.627,0.126,0.941},
morekeywords={REFERENCES}
}
\lstnewenvironment{sql}
{\lstset{style=Sql}}
{}
\lstnewenvironment{python}
{\lstset{style=Python}}
{}
\begin{document}
\begin{python}
parents, babies = (1, 1)
while babies < 100:
print 'This generation has {0} babies'.format(babies)
parents, babies = (babies, parents + babies)
\end{python}
\begin{sql}
CREATE TABLE STATION
(ID INTEGER PRIMARY KEY,
CITY CHAR(20),
STATE CHAR(2),
LAT_N REAL,
LONG_W REAL);
REFERENCES
\end{sql}
\end{document}