我想将所有方法名称的颜色改为蓝色。方法由前面的一个点和后面的一个左括号定义。示例:
.setLanguage("de");
在这里我只想要设置语言换成其他颜色。我该怎么做?我目前只能改变设置语言(
\documentclass{article}
\usepackage{graphicx,color}
\usepackage{booktabs}
\usepackage{listings}
\lstdefinelanguage{Java}{
sensitive = true,
keywords={},
morecomment=[l]{//},
morecomment=[s]{/*}{*/},
commentstyle=\color{darkgray}\ttfamily,
% This is where i try it
morecomment=[s][\color{blue}\bfseries]{.}{(},
aboveskip=2em,
belowskip=2em,
sensitive=true
}
\lstset{
backgroundcolor=\color{lightgray},
extendedchars=true,
basicstyle=\footnotesize\ttfamily,
showstringspaces=false,
showspaces=false,
numbers=left,
numberstyle=\footnotesize,
numbersep=7pt,
tabsize=1,
breaklines=true,
showtabs=true,
captionpos=b,
}
\begin{document}
\begin{figure}[htb]
\begin{small}
\lstinputlisting[language=Java]{test.java}
\end{small}
\caption{Service 1}
\end{figure}
\end{document}
答案1
虽然不是一个优雅的答案,但它确实有效。只需为和之间的内容定义另一组注释(
,而)
无需任何格式。
\documentclass{article}
\usepackage{graphicx,xcolor}
\usepackage{booktabs}
\usepackage{listings}
\lstdefinelanguage{Java}{
sensitive = true,
keywords={},
morecomment=[l]{//},
morecomment=[s]{/*}{*/},
commentstyle=\color{darkgray}\ttfamily,
% This is where i try it
moredelim=*[s][\color{blue}\bfseries]{.}{(},
moredelim=*[s][]{(}{)}, % <-- additional setting
aboveskip=2em,
belowskip=2em,
sensitive=true
}
\lstset{
backgroundcolor=\color{lightgray},
extendedchars=true,
basicstyle=\footnotesize\ttfamily,
showstringspaces=false,
showspaces=false,
numbers=left,
numberstyle=\footnotesize,
numbersep=7pt,
tabsize=1,
breaklines=true,
showtabs=true,
captionpos=b,
}
\begin{filecontents*}{test.java}
a.setLanguage("de");
b.hello("world");
\end{filecontents*}
\begin{document}
\begin{figure}[htb]
\begin{small}
\lstinputlisting[language=Java]{test.java}
\end{small}
\caption{Service 1}
\end{figure}
\end{document}