2

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.

Bermudes
  • 121
  • You could look at the piton package if you are using lualatex, it has options for @decorator's, the author of the package is active on this site too which is nice. Could use listings for your non-Python and piton for Python. If you want to stick with listings, hope someone can answer! – JamesT Jun 25 '23 at 14:50
  • 1
    If possible, I'd like to keep a listings-based solution to not enforce lualatex on the other people working on this document (pdflatex is the standard compiler here), but I'll try piton, thanks for the recommendation! – Bermudes Jun 25 '23 at 16:52

0 Answers0