I want to implement a function name color highlighting and I use the listings
package. My 'regex' for this is def %s(
where def
(including space) is the first delimiter %s
is the function name and (
is the second delimtier. Now my aim is to color only %s
in purple.
So basically I want to only touch the part between the delimiters and the delimiters should stay untouched.
Here is what I tried so far:
\documentclass{article}
\usepackage{xcolor,listings}
\usepackage[ngerman]{babel}
\usepackage{fontspec}
\setmonofont{Courier New}
\begin{document}
\lstset{language=Python,
escapebegin=\color{orange},
keywordstyle=\color{blue}\bfseries,
commentstyle=\color{orange!80!red},
stringstyle=\ttfamily\color{gray},
%moredelim = [is][\color{blue}\bfseries def \mdseries\color{purple}]{def\ }{(},
moredelim = [s][\color{purple}]{def\ }{(},
showstringspaces=false}
\lstset{literate=%
*{0}{{{\color{red}0}}}1
{1}{{{\color{red}1}}}1
{2}{{{\color{red}2}}}1
{3}{{{\color{red}3}}}1
{4}{{{\color{red}4}}}1
{5}{{{\color{red}5}}}1
{6}{{{\color{red}6}}}1
{7}{{{\color{red}7}}}1
{8}{{{\color{red}8}}}1
{9}{{{\color{red}9}}}1
}
\begin{lstlisting}[basicstyle=\ttfamily,mathescape]
def fancy_function_name(page_url = 'tex.stackexchange.com'):
return page_url.split('.')[0]
\end{lstlisting}
\end{document}
You can see that it matches the right place but is too greedy and colors the delimiters too.
So I thought about the uncommented line:
%moredelim = [is][\color{blue}\bfseries def \mdseries\color{purple}]{def\ }{(},
If I use this one and uncomment the line below I get this result:
It's nearly what I wan't but I cannot add the last, now missing (
in black color.
Does anyone has a solution for this without pretouching the code and add all the @@
symbols?