我正在尝试创建一个lstdefinestyle
类似于下图的图像,但由于某种原因,设置不起作用。
我的结果lstdefinestyle
:
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\definecolor{myblue}{RGB}{20,105,176}
\definecolor{constants}{RGB}{127,39,84}
\definecolor{character}{RGB}{100,169,57}
\lstdefinestyle{deltaj}{
belowcaptionskip=1\baselineskip,
breaklines=true,
columns=fullflexible,
frame=single,
xleftmargin=\parindent,
language=Java,
escapeinside={(*}{*)},
numbers=left,
stepnumber=1,
numberblanklines=false,
basicstyle=\footnotesize\ttfamily,
keywordstyle=\bfseries\color{green!40!black},
commentstyle=\itshape\color{purple!40!black},
identifierstyle=\color{blue},
stringstyle=\color{orange},
literate=
{\{}{{{\color{character}{\{}}}}{1}
{\}}{{{\color{character}{\}}}}}{1}
{SPL }{{{\color{constants}{SPL }}}}{1}
{Features }{{{\color{constants}{Features }}}}{1}
{Deltas }{{{\color{constants}{Deltas }}}}{1}
{Constraints }{{{\color{constants}{Constraints }}}}{1}
{Partitions }{{{\color{constants}{Partitions }}}}{1}
{Products }{{{\color{constants}{Products }}}}{1}
{when }{{{\color{constants}{when }}}}{1}
{\& }{{{\color{constants}{\& }}}}{1}
{| }{{{\color{constants}{| }}}}{1}
}
\begin{document}
\begin{lstlisting}[style=deltaj]
SPL ReminderPL {
Features = {Reminder, GUI, ManageReminder}
Deltas = {dBase}
Constraints {
Reminder & GUI & ManageReminder;
}
Partitions {
{dBase} when (Reminder & GUI & ManageReminder);
}
Products {
Example = {Reminder, ManageReminder, GUI};
}
}
\end{lstlisting}
\end{document}
答案1
literate
由于注释太长:不建议使用该选项突出显示关键字或某些标识符。listings
提供额外的选项keywords
并keywordstyle
分别定义不同的关键字集并应用样式。对于您的代码,定义可能看起来像
keywords={SPL,Features,Deltas,Constraints,Partitions,Products,when},
keywordstyle=\color{constants}
您的左括号和右括号定义也不正确。命令\{
和\}
排版数学模式括号分隔符,这可能不是您想要的列表。而是使用
literate={\{}{{{\color{character}{\char`\{}}}}{1}
{\}}{{{\color{character}{\char`\}}}}}{1}