Search code examples
colorslatex

LaTeX make only some listing use colour and not all (listings package)


I added colour to the listings I create in latex using the following,

\usepackage{listings}% http://ctan.org/pkg/listings
\lstset{
  basicstyle=\ttfamily,
  mathescape
}
\usepackage{color}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\usepackage[colorinlistoftodos]{todonotes}

\lstset{frame=tb,
  language=Python,
  aboveskip=3mm,
  belowskip=3mm,
  showstringspaces=false,
  columns=flexible,
  basicstyle={\small\ttfamily},
  numbers=none,
  numberstyle=\tiny\color{gray},
  keywordstyle=\color{blue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  breaklines=true,
  breakatwhitespace=true,
  tabsize=3
}

This applies the colouring to ALL listing that I create, I would like to create a new listing that does NOT use any colour, while keeping the colour for other listings.

Could someone tell me how to do that, or if it is even possible ?

Thank you


Solution

  • All the commannds that are given to a \lstset can be given as an optional parameter to a specific listing.

    So you can do

    \begin{lstlisting}[keywordstyle=\color{black},
      commentstyle=\color{black},
      stringstyle=\color{black},
    ]
    < your program >
    

    All the other parameters remain unchanged, but you can change specific values as required.

    Another way, is to define styles.

    \lstdefinestyle{nocoloring}{
        keywordstyle=\color{black},
        commentstyle=\color{black},
        stringstyle=\color{black}
    }
    
    \begin{lstlisting}{style=nocoloring}
    < your program >