Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 14 down vote accepted

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.

enter image description here

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

% Copied from the listings documentation
\lstdefinestyle{numbers} {numbers=left, stepnumber=1, numberstyle=\tiny, numbersep=10pt}
\lstdefinestyle{MyFrame}{backgroundcolor=\color{yellow},frame=shadowbox}

\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}
share|improve this answer

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}
share|improve this answer
1  
Can this be modified to accept something similar to \lstinputlisting{somefile.cpp} for larger code pieces that would make a solution like this a little less user friendly? I've got files in several syntaxes (particularly c++ and MATLAB) that I would like to be able to typeset in the same document. If there's somewhere in a manual that I've missed, I'd really appreciate a nice point in the right direction. –  Dave Doyle Apr 3 at 2:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.