
%---------------------------------------------------------
% Chapter: A chapter about algorithms
%----------------------------------------------------------

\chapter{Algorithms and Listings}

\section{Algorithms}

Algorithms can be typeset in \LaTeX\ using the \verb|algorithm|
and \verb|algorithmic| packages. Once they have been included
typesetting an algorithm is a snap. For example, the code:

\begin{verbatim}
\begin{algorithmic}[1]        
	\FOR{$i \leftarrow \left[0, 10 \right]$}
	   \PRINT $i$ to the console.
	\ENDFOR
\end{algorithmic}
\end{verbatim}

\noindent produces the output:

\begin{algorithmic}[1]        
	\FOR{$i \leftarrow \left[0, 10 \right]$}
	   \PRINT $i$ to the console.
	\ENDFOR
\end{algorithmic}

In this example the \verb|[1]| argument to the algorithmic 
environment tells the package to place Arabic numbers at
the beginning of each line.

Like figures and tables, algorithms can be labelled and
captioned.  For example, the code:

% Start a new page if there is less than one inch remaining here:
\need 1 in

\verbatiminput{./algorithms/examplealg}
	
\noindent produces the following typeset algorithm, which can 
be referenced as Algorithm~\ref{alg:EXAMPLE_ALG} by typing
\verb|Algorithm~\ref{alg:EXAMPLE_ALG}|:
	
\input{./algorithms/examplealg}

\section{Listings}

Listings in \LaTeX\ can be typeset using the \verb|listings|
package.  We can include code within the text using:

\begin{verbatim}
\begin{centering}
\lstinputlisting[float=h,language=FILE_LANGUAGE,
                 caption=A CAPTION,label=THE_LABEL]{FILE_TO_LIST}
\end{centering}
\end{verbatim}

For example, to include a centered listing of the contents of the 
Java file located at \verb|./examples/example.java|, with a 
caption ``Example Java File.'' and a label \verb|lst:JAVA|, 
we would write:

\begin{verbatim}
\begin{centering}
    \lstinputlisting[float=h,language=Java,caption=Example Java File.,
                     label=lst:JAVA]{./examples/example.java}
\end{centering}
\end{verbatim}

\noindent which would produce Listing~\ref{lst:JAVA}.

\begin{centering}
    \lstinputlisting[float=h,language=Java,caption=Example Java File.,
                     label=lst:JAVA]{./examples/example.java}
\end{centering}

\noindent
But what if we only want to show a few of the lines of the Java program?

In this case we can tell the package to only include certain lines, for
example, lines $10 \rightarrow 11$ in file \verb|./examples/example.java|
would be written

\begin{verbatim}
\begin{centering}
    \lstinputlisting[float=h,firstline=10,lastline=11,language=Java,
        caption=Partial Java Listing.,
        label=lst:SOMEJAVA]{./examples/example.java} 
\end{centering}
\end{verbatim}

\noindent and would produce Listing~\ref{lst:SOMEJAVA}.

\begin{centering}
    \lstinputlisting[float=h,firstline=10,lastline=11,language=Java,
	caption=Partial Java Listing.,
	label=lst:SOMEJAVA]{./examples/example.java} 
\end{centering}

The \verb|listings| package is capable to working with a large number
of languages; you can find the documentation at: 
	\url{http://www.ctan.org/tex-archive/macros/latex/contrib/listings/listings-1.3.pdf}.


For example, to typeset an XML file using a ``footnote-sized'' font we would
use the code:
\begin{verbatim}
\begin{centering}
    \lstinputlisting[float=h,basicstyle=\footnotesize,language=XML,
        caption=Example XML File.,
        label=lst:XML]{./examples/simple_example_xml.xml}
\end{centering}
\end{verbatim}

\noindent which would produce Listing~\ref{lst:XML}.

\begin{centering}
    \lstinputlisting[float=h,basicstyle=\footnotesize,language=XML,
        caption=Example XML File.,
        label=lst:XML]{./examples/simple_example_xml.xml}
\end{centering}

% Defunct link... what did Brian mean by ``sizes'' anyway?
%For more information on sizes see:
%\url{http://www.eng.cam.ac.uk/help/tpl/textprocessing/teTeX/latex/latex2e-html/ltx-178.html}.


