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?

1 Answer 1

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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .