3

Code:

\documentclass{article}
\usepackage{parskip}
\usepackage{listings}
\usepackage{textcomp}
\usepackage{xcolor}
\lstset{
    basicstyle=\ttfamily,
    columns=fullflexible,
    keepspaces=true,
    upquote=true,
    numbers=left,
    numberstyle=\ttfamily\color{gray},
    language=c,
    moredelim=*[is][\itshape]{((:}{:))},
    showstringspaces=false,
    commentstyle=\color{olive},
    keywordstyle=\color{blue},
    identifierstyle=\color{violet},
    stringstyle=\color{purple},
    directivestyle=\color{teal},
}
\begin{document}
Here is the skeleton of the proposed solution:

\begin{lstlisting}
int f(int x)
{
    if (x <= 0) {
        return 0;
    }

    if ((x | CONNECT_FLAG) != 0) {
        ((:statements:))
    } else if ((x | DROP_FlAG) != 0) {
        ((:statements:))
    } ((:if else if ladder continues:))

    return result;

}
\end{lstlisting}
\end{document}

Output:

enter image description here

Problem:

In line 11 of the source code output, if and else are syntax highlighted as keywords. I was expecting them to be highlighted with the moredelim style.

How can I prevent them from being syntax highlighted as keywords and be highlighted with the moredelim style instead?

4

Change the line

moredelim=*[is][\itshape]{((:}{:))},

to

moredelim=[is][\itshape]{((:}{:))},

A single star denotes nested syntax highlighting, where the style of detected keywords etc. will be applied instead of the delimiter style. Remove the star to prevent listings from looking for other syntax elements within the delimiter. (Additionally, two stars will apply both styles cummulatively.)

Result:

enter image description here

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.