使用清单对 @param 和 @return 进行着色

使用清单对 @param 和 @return 进行着色

以下是我所拥有的 MWE,以及屏幕截图

\documentclass[paper=a4, fontsize=11pt]{scrartcl}
\usepackage{color,soul}                                % For text coloring and highlighting
\usepackage{listings}

\definecolor{dkgreen}{rgb}{0,0.6,0} %for listings
\definecolor{gray}{rgb}{0.5,0.5,0.5} %for listings
\definecolor{mauve}{rgb}{0.58,0,0.82} %for listings

\lstset{frame=tb,
   language=php,
   aboveskip=3mm,
   belowskip=3mm,
   showstringspaces=false,
   columns=flexible,
   basicstyle={\small\ttfamily},
   numbers=none,
   numberstyle=\tiny\color{gray},
   keywordstyle=\color{blue},
   commentstyle=\color{dkgreen},
   morecomment=[l][\color{magenta}]{\#},
   stringstyle=\color{mauve},
   breaklines=true,
   breakatwhitespace=true,
   tabsize=3
}

\begin{document}

\begin{lstlisting}
/*
 * If the target is a valid duck, connects this bread with duck's mouth.
 * @param target the target we are trying to feed
 * @pre if target is an ancestor of class Duck, calls feedDuck()
 * @pre if target is an ancestor of MyDad, calls goHomeDadUrDrunk()
 * @return value of feedDuck() or goHomeDadUrDrunk() upon success, return value
 * of FALSE otherwise.
*/
function feed (target){\ldots}
\end{lstlisting}
\end{document}

在此处输入图片描述

有没有办法通过改变颜色使关键字@param@pre@post和更加突出?(请忽略,应该是)@return\ldots...

答案1

我找到了一个解决方案。只需将其添加为关键字,方法是将以下行添加otherkeywords={@param,@pre,@post,@return}lstset。见下文

\documentclass[paper=a4, fontsize=11pt]{scrartcl}
\usepackage{color,soul}                                % For text coloring and highlighting
\usepackage{listings}

\definecolor{dkgreen}{rgb}{0,0.6,0} %for listings
\definecolor{gray}{rgb}{0.5,0.5,0.5} %for listings
\definecolor{mauve}{rgb}{0.58,0,0.82} %for listings

\lstset{frame=tb,
   language=php,
   aboveskip=3mm,
   belowskip=3mm,
   showstringspaces=false,
   columns=flexible,
   basicstyle={\small\ttfamily},
   numbers=none,
   numberstyle=\tiny\color{gray},
   keywordstyle=\color{blue},
   otherkeywords={@param,@pre,@post,@return},
   commentstyle=\color{dkgreen},
   morecomment=[l][\color{magenta}]{\#},
   stringstyle=\color{mauve},
   breaklines=true,
   breakatwhitespace=true,
   tabsize=3
}

\begin{document}

\begin{lstlisting}
/*
 * If the target is a valid duck, connects this bread with duck's mouth.
 * @param target the target we are trying to feed
 * @pre if target is an ancestor of class Duck, calls feedDuck()
 * @pre if target is an ancestor of MyDad, calls goHomeDadUrDrunk()
 * @return value of feedDuck() or goHomeDadUrDrunk() upon success, return value
 * of FALSE otherwise.
*/
function feed (target){\ldots}
\end{lstlisting}
\end{document}

相关内容