1

I want to highlight function name in code, which is a word that ends with left parenthesis, like sqrt(), but don't want to highlight the parenthesis.

Here is what I got so far, I learnt this from Listings - Only highlight between delimiters . It works fine until I indent the statement. And even there are no indent problem, it cannot work on C/C++ or Java where the type before function name is unfixed.

I found a way that can highlight words start with an uppercase here: Can the listings package highlight by regexp?

But I don't really understand the code, not to mention to change it the way I want.

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}

\newcommand{\jsfunction}[1]{\textcolor{blue}{function} \textcolor{purple}{#1}(}
\lstdefinestyle{js}{
    basicstyle = \ttfamily,
    keywordstyle = \color{blue},
    keywords = {function, return},
    moredelim = [is][\jsfunction]{function\ }{(}
}

\begin{document}

\begin{lstlisting}[style = js]
function foo() {
    function bar(){

    }
}
\end{lstlisting}

\end{document}

enter image description here

  • Should this only work for identifiers that are immediately followed by (), or also for function calls with arguments like foo(x, y, 123)? – siracusa yesterday

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.