我想@Override
在 Java 文件中添加颜色定义。我该怎么做?
到目前为止,我有以下颜色定义:
\documentclass[a4paper,11pt]{article}
\usepackage{color}
\usepackage{courier}
\usepackage{listings} % for code snippets
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\lstset{frame=tb,
language=Java,
aboveskip=3mm,
belowskip=3mm,
showstringspaces=false,
columns=flexible,
basicstyle={\scriptsize\ttfamily},
numbers=left,
numbersep=4pt,
numberstyle=\scriptsize\color{gray},
keywordstyle=\color{blue},
commentstyle=\color{dkgreen},
stringstyle=\color{mauve},
breaklines=true,
breakatwhitespace=true,
tabsize=2
}
\begin{document}
\lstinputlisting[label={lst:sourcefile}]{codefile.java}
\end{document}
答案1
@
要突出显示以空格或换行符开头的所有单词,您可以moredelim
像这样使用键:
moredelim=[s][⟨style⟩]{@}{\ },
其中⟨style⟩
可以是字体系列、大小、形状、系列、颜色规范等的宏的任意组合。
\documentclass[a4paper,11pt]{article}
\usepackage{color}
\usepackage{courier}
\usepackage{listings} % for code snippets
\usepackage{filecontents}
% http://stackoverflow.com/questions/11873633/should-i-comment-override-method-in-java
\begin{filecontents*}{codefile.java}
/** Interface */
public Interface IFoo {
/** method */
public String getFoo();
}
/** Class implementing interface */
public Class Foo implements IFoo {
/** Should be here documentary comment for overrided method getFoo() ???*/
@Override
public String getFoo() {
return "Foo";
}
}
\end{filecontents*}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\lstset{frame=tb,
language=Java,
aboveskip=3mm,
belowskip=3mm,
showstringspaces=false,
columns=flexible,
basicstyle={\scriptsize\ttfamily},
numbers=left,
numbersep=4pt,
numberstyle=\scriptsize\color{gray},
keywordstyle=\color{blue},
commentstyle=\color{dkgreen},
stringstyle=\color{mauve},
moredelim=[s][\color{red}]{@}{\ },
breaklines=true,
breakatwhitespace=true,
tabsize=2
}
\begin{document}
\lstinputlisting[label={lst:sourcefile}]{codefile.java}
\end{document}