Thursday, August 30, 2007

Display C++ Codes with syntax highlight

source: http://www.ntu.edu.sg/home5/pg04878518/LatexTips.html#q1
googled by "latex C++ syntax highlight listings"

1. Want to include Java/C++/Pascal source code. How can I get syntax highlighting?

There is a greate package which supports syntax highlighting for a huge amount of programming languages (Fortran, C, C++, csh, HTML, Java, Matlab, Mathematica, Pascal, Perl, SQL, XML, Delphi, PHP, VBScript, SAS and even Latex itself - and many more). The usage is simple:

  • Load the package: \usepackage{listings}
  • Set the language: \lstset{language=TeX}
  • Open a lstlisting environment: \begin{lstlisting}
  • Include all your programming code
  • Close the lstlisting environment: \end{lstlisting}

If this does not seem to work for your language, even if the language is listed above, try upgrading to the lastest MiKTeX version. The listings package is being updated continuously.

Here's a more advanced example on the source code highlight:

 \usepackage{color}
\usepackage{listings}
\definecolor{Brown}{cmyk}{0,0.81,1,0.60}
\definecolor{OliveGreen}{cmyk}{0.64,0,0.95,0.40}
\definecolor{CadetBlue}{cmyk}{0.62,0.57,0.23,0}
\begin{document}

\lstset{language=R,frame=ltrb,framesep=5pt,basicstyle=\normalsize,
keywordstyle=\ttfamily\color{OliveGreen},
identifierstyle=\ttfamily\color{CadetBlue}\bfseries,
commentstyle=\color{Brown},
stringstyle=\ttfamily,
showstringspaces=ture}

\begin{lstlisting}
...
\end{lstlisting}

2. DVIPS can't find

No comments: