我想用 latex 中的 lstdefinelanguage 将一些符号变成绿色,但似乎无法识别符号。
我需要着色的符号是 =、> 和 <。这可能吗?我该怎么做?
主文件:
\documentclass{article}
\usepackage{graphicx,color}
\usepackage{booktabs}
\usepackage{listings}
\lstdefinelanguage{NeoIDL}{
sensitive = true,
keywords = [1]{module, resource, enum, annotation, for, import, entity, path,
@get, @post, @put, @delete, require, ensure, otherwise, call},
%ndkeywords={int, \char{} },
%ndkeywordstyle=\color{blue}\bfseries,
morekeywords=[2]{>, <, ==},
keywordstyle=[2]\color{green},
numbers=left,
numberstyle=\scriptsize,
stepnumber=1,
numbersep=8pt,
showstringspaces=false,
breaklines=true,
frame=top,
comment=[l]{//},
morecomment=[s]{/*}{*/},
commentstyle=\color{purple}\ttfamily,
stringstyle=\color{red}\ttfamily,
morestring=[b]',
morestring=[b]"
}
\begin{document}
\begin{figure}[htb]
\begin{small}
\lstinputlisting[language=NeoIDL,firstnumber=1]{store_pos_servico.neo}
\end{small}
\caption{Service 1}
\end{figure}
\end{document}
需要格式化的文件:
module store {
(...)
resource order {
path = "/order/{id}";
@post int postOrder (Order order)
require (call store.getOrder(id) == "NotFound"),
otherwise "InvalidPrecondition"
};
}
答案1
我以前也遇到过类似的情况,并且能够通过以下方式解决问题:
\documentclass{article}
\usepackage{graphicx,color}
\usepackage{booktabs}
\usepackage{listings}
\lstdefinelanguage{NeoIDL}{
sensitive = true,
keywords={},
otherkeywords={% Operators
>, <, ==
},
keywords = [2]{module, resource, enum, annotation, for, import, entity, path,
@get, @post, @put, @delete, require, ensure, otherwise, call},
keywordstyle=\color{green},
keywordstyle=[2]\color{blue},% for example
numbers=left,
numberstyle=\scriptsize,
stepnumber=1,
numbersep=8pt,
showstringspaces=false,
breaklines=true,
frame=top,
comment=[l]{//},
morecomment=[s]{/*}{*/},
commentstyle=\color{purple}\ttfamily,
stringstyle=\color{red}\ttfamily,
morestring=[b]',
morestring=[b]"
}
\begin{document}
\begin{figure}[htb]
\begin{small}
\lstinputlisting[language=NeoIDL,firstnumber=1]{store_pos_servico.neo}
\end{small}
\caption{Service 1}
\end{figure}
\end{document}
即定义一个空的 1 类关键词集,将符号添加为属于该类的“其他关键词”,并将所有其他类型的关键词移至高阶类。但不要问为什么这样做有效,因为我也不知道!
这是我得到的输出: