Syntax highlighting in LaTeX using the listings
package is very powerful. Below, I will provide a few examples for your convenience.
First, let me briefly summarize the usage of the listings package.
%Preamble \usepackage{listings} \usepackage{color} \definecolor{name}{rgb}{0.5,0.5,0.5} \lstset{key1=value1,key2=value2} % Define listings style %Document body \begin{lstlisting} % Add code here \end{lstlisting} % Or load from file \lstinputlisting{filename.java}
So below you’ll find a few complete code examples to reuse and modify for your purposes:
“The Pseudo Code Box”
\definecolor{lightpurple}{rgb}{0.8,0.8,1} \lstset{ numbers=left, stepnumber=1, numbersep=5pt, numberstyle=\small\color{black}, basicstyle=\ttfamily, keywordstyle=\color{black}, commentstyle=\color{black}, stringstyle=\color{black}, frame=single, tabsize=2, backgroundcolor=\color{lightpurple}, caption=Fibonacci numbers pseudo code}
This code was taken from here.
“Java/Eclipse style”
\definecolor{javared}{rgb}{0.6,0,0} % for strings \definecolor{javagreen}{rgb}{0.25,0.5,0.35} % comments \definecolor{javapurple}{rgb}{0.5,0,0.35} % keywords \definecolor{javadocblue}{rgb}{0.25,0.35,0.75} % javadoc \lstset{language=Java, basicstyle=\ttfamily, keywordstyle=\color{javapurple}\bfseries, stringstyle=\color{javared}, commentstyle=\color{javagreen}, morecomment=[s][\color{javadocblue}]{/**}{*/}, numbers=left, numberstyle=\tiny\color{black}, stepnumber=2, numbersep=10pt, tabsize=4, showspaces=false, showstringspaces=false}
\definecolor{lightgrey}{rgb}{0.9,0.9,0.9} \definecolor{darkgreen}{rgb}{0,0.6,0} \lstset{language=[LaTeX]TeX, texcsstyle=*\bf\color{blue}, numbers=none, breaklines=true, keywordstyle=\color{darkgreen}, commentstyle=\color{red}, otherkeywords={$, \{, \}, \[, \]}, frame=none, tabsize=2, backgroundcolor=\color{lightgrey}, caption=LaTeX example}
The LaTeX code was copied from here.