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.

This question already has an answer here:

In my paper I want to include some source codes with colored sytax. For syntax highlighting I use the listings package:

\lstset{frame=tb,
  language={[Visual]Basic},
  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
}

And then in the document:

\begin{lstlisting}
'test
\end{lstlisting}

This works for one language (Visual Basic in the example above). But now I also want to include some C code. How can I achieve this?

share|improve this question
4  
See if defining lstset parameters for multiple languages helps. –  Mike Renfro Apr 2 '13 at 13:41
    
Welcome to TeX.sx! –  hpesoj626 Apr 2 '13 at 13:48
    
Thanks Mike, that was just what I needed. –  alxppp Apr 2 '13 at 13:56
add comment

marked as duplicate by diabonas, Werner, mafp, Claudio Fiandrino, Torbjørn T. Apr 2 '13 at 14:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 2 down vote accepted

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}

enter image description here

share|improve this answer
add comment

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