You can use any (All)TeX macro you wish within listings by enclosing it within escape brackets,
(*@ \textcolor{blue}{code} @*)
The funny eyes are the escapes you define them as follows:
\lstset{escapeinside={(*@}{@*)}}
For highlighting the soul
package provides a nice highlight macro \hl
, try it in the following minimal
\documentclass{article}
\usepackage{soul,listings,xcolor}
\lstnewenvironment{teX}[1][]
{\lstset{language=[LaTeX]TeX}\lstset{escapeinside={(*@}{@*)},
numbers=left,numberstyle=\normalsize,stepnumber=1,numbersep=5pt,
breaklines=true,
%firstnumber=last,
%frame=tblr,
framesep=5pt,
basicstyle=\normalsize\ttfamily,
showstringspaces=false,
keywordstyle=\itshape\color{blue},
%identifierstyle=\ttfamily,
stringstyle=\color{maroon},
commentstyle=\color{black},
rulecolor=\color{black},
xleftmargin=0pt,
xrightmargin=0pt,
aboveskip=\medskipamount,
belowskip=\medskipamount,
backgroundcolor=\color{white}, #1
}}
{}
\begin{document}
\begin{teX}
\test{this is some code}
(*@ \textcolor{blue}{code} @*)
(*@ \hl{yellow code} @*)
\end{teX}
\end{document}
I normally use an environment for "listings" settings, but you can use any other code style as well.
Another useful trick is to use the "listings" setting for emphasis in macros, like this:
\gdef\emphasis#1{\lstset{emph={begin,end,#1},
emphstyle={\itshape\ttfamily\textcolor{blue}}}}
\gdef\hlemphasis#1{\lstset{emph={begin,end,#1},
emphstyle={\hl{blue}}}}
This way you can emphasize keywords.
Consider using lstset
from the listings
package. Note that any option you don't want globally (or technically, global to the current group) can be set as part of the optional argument to \begin{lstlisting}
.
\documentclass{article}
\usepackage{xcolor} % for more colors
\usepackage{listings}
\lstset{
basicstyle=\small\ttfamily,
stringstyle=\color{black!40!white}\ttfamily,
keywordstyle=\color{blue},
commentstyle=\color{green!50!black},
showstringspaces=false,
backgroundcolor=\color{black!5!white}
}
\usepackage{framed} % for contrast
\begin{document}
% Example source from http://php.net/manual/en/function.readdir.php
\begin{framed}
\begin{lstlisting}[language=php]
<?php
if ($handle = opendir('/path/to/files')) {
echo "Directory handle: $handle\n";
echo "Entries:\n";
/* This is the correct way to
loop over the directory. */
while (false !== ($entry = readdir($handle))) {
echo "$entry\n";
}
/* This is the WRONG way to
loop over the directory. */
while ($entry = readdir($handle)) {
echo "$entry\n";
}
closedir($handle);
}
?>
\end{lstlisting}
\end{framed}
\end{document}
Best Answer
The code seems to be highlighted using the package
minted
.Typeset either with
pdflatex --shell-escape
or witharara
.