\documentclass{article}
\usepackage{makeidx}
\makeindex
\usepackage{listings}
\usepackage[dvipsnames]{color}
\begin{document}
\lstset{numbers=left}
\section{Introduction}
This document contains the following listings:
\lstlistoflistings
\section{Inline Listings}
Various delimiters: \lstinline{a_word},
\lstinline!a_word!, \lstinline Aa_wordA,
\lstinline&a_word& and even \lstinline^a_word^ done.
\def\justcopy#1{#1}
Indirectly: \justcopy{\lstinline|a_word|};
and with messed up braces \lstinline{foo { bar }.% }
{
\lstset{
mathescape=true,
}
Careful with spacing/math/macros: \lstinline!foo($\langle X \rangle$)!
}
\subsection{Shorthands}
Normal: |@| and $x^y$
\lstMakeShortInline[language=perl,basicstyle=\ttfamily]|
\lstMakeShortInline[language=perl,basicstyle=\ttfamily]@
\lstMakeShortInline[language=perl,basicstyle=\ttfamily]^
Listing1:
|$foo->baz(/^\s*/)";|
Listing2:
@$foo->baz(/^\s*/)";@
Listing3:
^xy^
\lstDeleteShortInline|
\lstDeleteShortInline@
\lstDeleteShortInline^
Normal again: |@| and $x^y$
\section{An untyped Listing}
No options, language, etc
\begin{lstlisting}
stuff1
stuff2
stuff3
\end{lstlisting}
\section{Some C}
\begin{lstlisting}[language=C,identifierstyle=\slshape,directivestyle=\ttfamily]
#define EXAMPLE whichwhat
x = "foo";
break;
\end{lstlisting}
\section{A Pascal Listing}
A listing portion:
\begin{lstlisting}[language=Pascal,firstline=2,lastline=5,caption={}]
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write('case insensitive');
Write('long '' string');
WritE('Pascal keywords.');
\end{lstlisting}
A numbered listing:
\begin{lstlisting}[language=Pascal,numbers=left, numberstyle=\tiny, stepnumber=2,stringstyle=\color{red}\ttfamily,showspaces,tabsize=4]
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write('case insensitive');
Write('long '' string');
WritE('Pascal keywords.');
\end{lstlisting}
A Titled listing:
\begin{lstlisting}[language=Pascal,title={A bit of Pascal}]
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write('case insensitive');
\end{lstlisting}
A Captioned listing (known as Listing \ref{pascallisting}) :
\begin{lstlisting}[language=Pascal,caption=Another bit of Pascal, label=pascallisting,firstnumber=100,numbers=left]
for i:=maxint to 0 do
begin
{ do nothing }
end;
\end{lstlisting}
\section{An Environment}
\begin{lstlisting}[language=Pascal]
for i:=maxint to 0 do
begin
{ do nothing }
end;
\end{lstlisting}
\lstnewenvironment{colored}[1]{\lstset{language=Pascal,numbers=right,numberstyle=\color{#1}}}{}
\begin{colored}{red}
for i:=maxint to 0 do
begin
{ do nothing }
end;
\end{colored}
\begin{colored}{blue}
for i:=maxint to 0 do
begin
{ do nothing }
end;
\end{colored}
\section{Framing and such}
\lstset{backgroundcolor=\color[named]{CarnationPink}}
\begin{lstlisting}[language=Pascal,frame=single,rulecolor=\color{red}]
for i:=maxint to 0 do
begin
{ do nothing }
end;
\end{lstlisting}
\begin{lstlisting}[language=Pascal,frameround=tttt,backgroundcolor=\color{yellow}]
for i:=maxint to 0 do
begin
{ do nothing }
end;
\end{lstlisting}
\lstset{backgroundcolor=}
\begin{lstlisting}[language=Pascal,frame=single]
for i:=maxint to 0 do
begin
{ do nothing }
end;
\end{lstlisting}
\begin{lstlisting}[language=Pascal,frame=lines]
for i:=maxint to 0 do
begin
{ do nothing }
end;
\end{lstlisting}
\begin{lstlisting}[language=C,identifierstyle=\slshape,directivestyle=\ttfamily,
caption=A C language listing, frame=lines,backgroundcolor={\color[cmyk]{0,0,0,0.1}}]
#define EXAMPLE whichwhat
x = "foo";
break;
\end{lstlisting}
\section{Listing with Math}
\begin{lstlisting}[language=c,texcl,commentstyle=\color{green}]
// \upshape calculate $a_{ij}$
a[i][j] = a[j][j]/a[i][j];
\end{lstlisting}
\begin{lstlisting}[texcl,language=c]
// \upshape calculate $a_{ij}$
a[i][j] = a[j][j]/a[i][j];
\end{lstlisting}
\begin{lstlisting}[language=c,mathescape,numbers=left,commentstyle=\color{green}]
// calculate $a_{ij}$
$a_{ij}
= a_{jj}/a{ij}$;
// calculate $a_{ij} =
\sin x$
a[i,j]=sin(x)
foo="a word";
foo="a $x^2$ math";
\end{lstlisting}
\begin{lstlisting}[language=c,escapechar=\%,escapebegin=\textless,escapeend=\textgreater,numbers=left]
// calculate %$a_{ij}$%
a_{ij}
= a_{jj}/a{ij};
\end{lstlisting}
\begin{lstlisting}[language=c,numbers=left,stringstyle=\ttfamily]
// calculate $a_{ij}$
$a_{ij}
= a_{jj}/a{ij}$;
// calculate $a_{ij} =
\sin x$
a[i,j]=sin(x)
foo="a word";
foo="a \"string";
foo="a $x^2$ math";
\end{lstlisting}
\section{A Perl Listing}
\lstinputlisting[language=perl]{any.sty.ltxml}
\section{A Recursive \TeX\ listing}
\lstinputlisting[language={[LaTeX]TeX}]{listing.tex}
A shorter listing, with colored cs that include the slash
\begin{lstlisting}[language={[LaTeX]TeX},texcsstyle=*{\color{blue}\bfseries}]
\iftrue something \fi
\end{lstlisting}
\section{Testing Tag}
% AHA, tagstyle only is in effect with XML (?)
\begin{lstlisting}[language=XML,tagstyle=\bf]
<element attr='value'>content</element>
\end{lstlisting}
\begin{lstlisting}[language=XML,tagstyle=\bf,usekeywordsintag=false]
<element attr='value'>content</element>
\end{lstlisting}
\begin{lstlisting}[language=XML,tagstyle=\bf,markfirstintag]
<element attr='value'>content</element>
\end{lstlisting}
\section{Literate Programming}
\begin{lstlisting}[language=C,escapechar=@,literate=*{:=}{{$\gets$}}1 {<=}{{$\leq$}}1 {>=}{{$\geq$}}1 {<>}{{$\neq$}}1]
var i:integer;
if (i<=0) i := 1;@\label{lit:a}@
if (i>=0) i := 0;
if (i<>0) i := 0;@\label{lit:b}@
/* However not := here */
\end{lstlisting}
where we draw your attention to lines \ref{lit:a} and \ref{lit:b}.
\section{Screwiness}
\lstdefinelanguage{bingo}{morekeywords={foo,bar},morekeywords=[2]{bing,bar}}
%,
% AHA, words can only be in one class (1st one declared?)
% BUT, index is separate, and classname is without the "style" !!
\begin{lstlisting}[language=bingo,keywordstyle=\bfseries,keywordstyle={[2]\itshape},index={[1][keywords2]{bar,baz}}]
foo bar baz bing booboo
\end{lstlisting}
{\bfseries\itshape bfit}
{\itshape\bfseries itbf}
\printindex
\end{document}