2

Here is my code:

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

% Example 1
\lstset{
    basicstyle=\color[HTML]{0000ff}\ttfamily, % blue
    moredelim=[s][\itshape\color{red}]{[}{]}, % red
}
\begin{lstlisting}
foo [bar] baz
\end{lstlisting}

% Example 2
\lstset{
    basicstyle=\color[HTML]{008000}\ttfamily, % green
    moredelim=[s][\itshape\color[HTML]{808080}]{[}{]}, % gray
}
\begin{lstlisting}
foo [bar] baz
\end{lstlisting}

\end{document}

Here is the output:

enter image description here

I was expecting the second "[bar]" to appear in gray color. But it appeared in red color. Why is it so?

3

You are using an optional argument within an optional argument, so you have to group the argument.

correct color

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

% Example 1
\lstset{
    basicstyle=\color[HTML]{0000ff}\ttfamily, % blue
    moredelim=[s][\itshape\color{red}]{[}{]}, % red
}
\begin{lstlisting}
foo [bar] baz
\end{lstlisting}

% Example 2
\lstset{
    basicstyle=\color[HTML]{008000}\ttfamily, % green
    moredelim=[s][{\itshape\color[HTML]{808080}}]{[}{]}, % gray
}
\begin{lstlisting}
foo [bar] baz
\end{lstlisting}

\end{document}

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.