3

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?

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.

2

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

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