# Defining lstset parameters for multiple languages

I am using the listings package, and have defined lstset parameter sets for multiple programming languages. I would like to select a parameter set when beginning a lstlisting or lstinputlisting by selecting the language corresponding to the parameter set definitions. I appreciate, however, that language selection at this stage only affects formatting of language-specific syntax and is not connected to the preamble parameter set definitions. Rather, enumerating multiple such definitions is fruitless as the last set of definitions overrides all the former ones (or so it appears). Therefore, I would like to know how to define and subsequently invoke multiple lstset parameter sets.

As a very rudimentary example,

\documentclass{article}
\usepackage{listings}% http://www.ctan.org/pkg/listings
\lstset{language=C,frame=lines}
\lstset{language=C++,frame=none}
\begin{document}
\begin{lstlisting}[language=C]
#include<stdio.h>
main() {
printf("Hello World");
}
\end{lstlisting}
\begin{lstlisting}[language=C++]
#include <iostream.h>
main() {
cout << "Hello World!";
return 0;
}
\end{lstlisting}
\end{document}


As one can see, both languages are displayed using the second defined parameter set (frame=none). I would like to use a key-value pair in the \begin{lstlisting} macro to invoke the appropriate language-specific parameter set defined in the preamble (as suggested by my use of the language= key-value pair). Is this possible?

-

You can use multiple \lstdefinestyle to define your own styles, and then combine them as desired. With this approach you can incorporate previous styles and apply tweaks to them as well.

Below I have defined the styles numbers which specifies the settings to be used for the line numbers, and a style MyFrame which defines a shadowbox for a frame. These two styles are used to define a MyCStyle and a MyC++Style style. Note that in the MyCStyle I override the frame settings, and in the MyC++Style, I override the background color setting.

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

% Copied from the listings documentation
\lstdefinestyle{numbers} {numbers=left, stepnumber=1, numberstyle=\tiny, numbersep=10pt}

\lstdefinestyle{MyCStyle} {language=C,style=numbers,style=MyFrame,frame=lines}
\lstdefinestyle{MyC++Style} {language=C++,style=numbers,style=MyFrame,frame=none,backgroundcolor={}}

\lstset{language=C,frame=lines}
\lstset{language=C++,frame=none}

\begin{document}
\begin{lstlisting}[style=MyCStyle]
#include<stdio.h>
main() {
printf("Hello World");
}
\end{lstlisting}
\begin{lstlisting}[style=MyC++Style]
#include <iostream.h>
main() {
cout << "Hello World!";
return 0;
}
\end{lstlisting}
\end{document}

-

You can also define your own language dependent environments:

\documentclass{article}
\usepackage{listings}
\lstnewenvironment{C}
{\lstset{language=C,frame=lines}}
{}
\lstnewenvironment{CPP}
{\lstset{language=C++,basicstyle=\ttfamily\small,frame=none}}
{}
\begin{document}

\begin{C}
#include<stdio.h>
main() {
printf("Hello World");
}
\end{C}

\begin{CPP}
#include <iostream.h>
main() {
cout << "Hello World!";
return 0;
}
\end{CPP}
\end{document}

-