I am using listings for code formatting and colouring in a document I'm working on, including a lot of Python code. I would like for decorators to be displayed in a specific colour but for the @ symbol to still be displayed normally when used as a matrix operator.
I found a hack to handle the decorators on this website (unfortunately a long time ago and I can't seem to find the original post) using a delimiter with @ as left delimiter and \^^M as right delimiter. The following code then succeeds at showing the decorator in orange, but also shows the matmul operator in orange.
\documentclass{article}
\usepackage{listings}
\usepackage{tcolorbox}
\definecolor{orange}{rgb}{.9,.5,.1}
\lstset{frame=tb,
language=python,
moredelim=[s][\color{orange}]{@}{^^M}, % show decorators in orange
showstringspaces=false,
basicstyle={\ttfamily},
tabsize=4
}
\begin{document}
\begin{lstlisting}
@decorator
def function():
A = B = np.eye(5)
A = A @ B
A @= B
\end{lstlisting}
\end{document}
How could one keep the specific colouring for the decorators but leave the operators (i.e. either @ or @= ) in black? I tried to understand how this proposition by jub0bs works to adapt it but my knowledge of listings (and LaTeX for that matter) is far from enough for that.
pitonpackage if you are usinglualatex, it has options for@decorator's, the author of the package is active on this site too which is nice. Could uselistingsfor your non-Python andpitonfor Python. If you want to stick withlistings, hope someone can answer! – JamesT Jun 25 '23 at 14:50listings-based solution to not enforcelualatexon the other people working on this document (pdflatexis the standard compiler here), but I'll trypiton, thanks for the recommendation! – Bermudes Jun 25 '23 at 16:52