[Tex/LaTex] How to use different lstset settings for listings

listings

I would like to specify two different lstset settings in one latex file: one for java code, one for html. Is this somehow possible?

The solution of https://stackoverflow.com/questions/2654810/how-to-reset-lstset-settings-for-listings does not seem to be appropriate for my document because I have more than just one html listing.

Best Answer

Every lstlistings block can have its own formatting commands. So:

\documentclass{article}
\usepackage{listings}
\lstset{basicstyle=\ttfamily}
\begin{document}
\begin{lstlisting}
  Foo
\end{lstlisting}

\begin{lstlisting}[basicstyle=\sffamily]
  Bar
\end{lstlisting}

\lstset{basicstyle=\rmfamily}

\begin{lstlisting}
  Baz
\end{lstlisting}

\end{document}

If you want to use two distinct listings and resetting the listings like this manually is a hassle, just create some new environments with lstnewenvironment for example:

\lstnewenvironment{sflisting}{\lstset{basicstyle=\sffamily}}{}
\lstnewenvironment{ttlisting}{\lstset{basicstyle=\ttfamily}}{}

page 42 of the listings manual has the details