This probably have an answer on here somewhere…
Here is an MWE:
\documentclass[a4paper]{memoir}
\usepackage{listings}
\usepackage[svgnames,dvipsnames]{xcolor}
\colorlet{level1}{red}
\lstset{
basicstyle=\small\ttfamily,
keywordstyle=\bfseries\color{level1},
alsoletter={\\},
}
\begin{document}
\lstset{
morekeywords={\\bigl,\\bigr}
}
\begin{lstlisting}
$\bigl(\sqrt{x^2}\bigr)$
\end{lstlisting}
\begin{lstlisting}
$ \bigl(\sqrt{x^2}\bigr)$
\end{lstlisting}
\end{document}
Why do I need a space in order to get a red \bigl
?
Best Answer
The math shift is also a letter, so listings see the "keyword"
$\bigl
. You could addalsoother={$}
.Edit
Probably a better way is to define an own language: