%
% to "compile" (old style -- produces DVI files):
%   latex sampler (first pass, also generates .aux file for bibtex)
%   bibtex sampler (generates .bbl file with bibliography)
%   latex sampler (second pass, incorporates bibliography)
%   latex sampler (do this if you still get messages about labels
%      having changed or missing references)
%   dvips -o sampler.ps sampler 
% or to generate PDF from DVI:
%   dvipdf sampler
%
% to "compile" directly to PDF, replace "latex" with "pdflatex"
%   in instructions for compiling
%   
% or use "latexmk" (discussed in class)
%
\documentclass[12pt]{article}

%-----------------------------------------------------------------------
%
% "preamble" with \usepackage commands, macro definitions, etc.
%

% make sure of okay hyphenation
% (this may no longer be needed, but at one time it fixed a bug)
\usepackage[english]{babel}
\selectlanguage{english}

% some useful packages
\usepackage{graphicx}	% has \includegraphics
\usepackage{moreverb}	% has \verbatiminput and others
\usepackage{fancybox}	% has \BVerbatim and others
\usepackage{amsmath}	% has considerable extra math stuff
\usepackage{url}	% has \url for formatting URLs

% allow more figures on a page
\setcounter{topnumber}{10}
\setcounter{bottomnumber}{10}
\setcounter{totalnumber}{10}
\setcounter{dbltopnumber}{10}
\def\topfraction{1.0}
\def\bottomfraction{1.0}
\def\textfraction{0.0}
\def\dbltopfraction{1.0}
% adjust spacing for boxed code examples
%\fboxrule=2pt % change width of box rule
\fboxsep=10pt % change space between box and enclosed text
% adjust interrow spacing for tables
\renewcommand{\arraystretch}{1.2}

%
% macros, etc.
%
% (if you have a lot of these you will use in many documents, put them 
% in a separate file foo.tex and use \input{foo} to include them)
%
% macros to reference sections, equations, consistently
%   the "~" disallows line breaks between Section, etc., and number
\newcommand{\SectionRef}[1]{Section~\ref{#1}}
\newcommand{\EqnRef}[1]{Equation~\ref{#1}}
\newcommand{\FigRef}[1]{Figure~\ref{#1}}
\newcommand{\TableRef}[1]{Table~\ref{#1}}
\newcommand{\PageRef}[1]{page~\pageref{#1}}
% macro to start an unindented paragraph with space before
\newcommand{\SpacedPar}{\vspace*{\baselineskip}\noindent}
%
\newenvironment{TinyQuote}
{
	\begin{quote}
	\begin{tiny}
}
{
	\end{tiny}
	\end{quote}
}

% hack to "properly" typeset BibTeX 
% also an example of \input, similar to C #include
\input{typeset-bibtex}

% title, etc.
\title{A Sampler of \LaTeX\ Features}
\author{Berna Massingill}
% don't include date with title
\date{}
% uncomment to include date
%\date{March 30, 2004}

%-----------------------------------------------------------------------
%
% body of document
%

%-----------------------------------------------------------------------

\begin{document}

\maketitle

\tableofcontents

%-----------------------------------------------------------------------

\section{Text and sectioning}
\label{first-section}

\subsection{Text}
\label{text-subsection}

% "\" after macro reference (\LaTeX) makes sure there's space after it.
% (yet another "gotcha", alas.)
\LaTeX\ formats lines into paragraphs automagically.
For purposes of illustration, I'll put in some extra text here:
lots and lots and lots and lots and lots and lots and
lots and lots and lots and lots and lots and lots and
lots and lots and lots and lots and lots and lots and
lots and lots and lots and lots and lots and lots and
lots and lots and lots and lots and lots and lots 
of words.

A blank line in the source starts a new paragraph, like this one.
For purposes of illustration, I'll put in some extra text here:
lots and lots and lots and lots and lots and lots and
lots and lots and lots and lots and lots and lots 
of words.

And here's yet another paragraph with 
lots and lots and lots and lots and lots and lots and
lots and lots and lots and lots and lots and lots 
of words.

% SpacedPar is a macro I defined earlier
\SpacedPar
Here is yet another paragraph, this one preceded by some space and
not indented.  Look at the source for how that was accomplished.

One thing you do have to be careful about is making sure \TeX\
doesn't interpret periods as ``end of sentence'' (which would result
in its adding a bit of extra space).
Example:
Dr.~Smith-Jones has a hyphenated last name.
% the ~ means non-breaking space.
% or you could put {} after the period, but that allows a line break.

\subsection{Text styles}

You \emph{emphasize text} with the \verb+\emph+ macro.
There are also macros for applying various different styles
(italics, etc.) to text.
%
\begin{itemize}
\item \verb+\textsf+ (\textsf{looks like this})
\item \verb+\texttt+ (\texttt{looks like this})
\item \verb+\textbf+ (\textbf{looks like this})
\item \verb+\textit+ (\textit{looks like this})
\item \verb+\textsl+ (\textsl{looks like this})
\item \verb+\textsc+ (\textsc{looks like this})
\end{itemize}
%
I like to use typewriter font (\verb+\texttt+) for program variables
and command names, as well as for URLs, but for the former it
may be better to use \verb+verb+,
which allows including characters that normally would need to
be ``escaped'', such as some of the characters in \verb+!@#$%^&*()_<>+,
and for the latter it may be better to use \verb+\url+
(e.g., \url{http://www.cs.trinity.edu/~bmassing}),
which breaks lines where needed and does something reasonable with
the tilde character (\texttt{\textasciitilde}).

%-----------------------------------------------------------------------

\section{Lists and other environments}

% the blank comment lines within the lists are to provide visual 
% separation in the source but avoid starting new "paragraphs" -- 
% with possibly unwanted extra vertical space.

% indentation within lists is meant to make the source more 
% human-readable.

\subsection{Itemized lists}

Here is an itemized list.
%
\begin{itemize}
\item An item.
	(For purposes of illustration, part of the list entry
	is this sentence with
	lots and lots and lots and lots and
	lots and lots and lots and lots and
	of words.)
\item Another item.
	(For purposes of illustration, part of the list entry
	is this sentence with
	lots and lots and lots and lots and
	lots and lots and lots and lots and
	of words.)

	This one has subitems.
	\begin{itemize}
	\item An item.
	(For purposes of illustration, part of the list entry
	is this sentence with
	lots and lots and lots and lots and
	lots and lots and lots and lots and
	of words.)
	\item Another item.
	(For purposes of illustration, part of the list entry
	is this sentence with
	lots and lots and lots and lots and
	lots and lots and lots and lots and
	of words.)
	\end{itemize}
\end{itemize}
% 
It has some text after it.
(Note that text after a list is only set flush left if it
doesn't start a new paragraph.)

\subsection{Enumerated lists}

Here is an enumerated list.  
%
\begin{enumerate}
\item An item.
	(For purposes of illustration, part of the list entry
	is this sentence with
	lots and lots and lots and lots and
	lots and lots and lots and lots and
	of words.)
\item Another item.
	(For purposes of illustration, part of the list entry
	is this sentence with
	lots and lots and lots and lots and
	lots and lots and lots and lots and
	of words.)
	This one has subitems.
	\begin{enumerate}
	\item An item.
	(For purposes of illustration, part of the list entry
	is this sentence with
	lots and lots and lots and lots and
	lots and lots and lots and lots and
	of words.)
	\item Another item.
	(For purposes of illustration, part of the list entry
	is this sentence with
	lots and lots and lots and lots and
	lots and lots and lots and lots and
	of words.)
	\end{enumerate}
\end{enumerate}

\subsection{``Description'' lists}

\SpacedPar
Here is a ``description list''.
%
\begin{description}
\item[Something.]  An item.
	(For purposes of illustration, part of the list entry
	is this sentence with
	lots and lots and lots and lots and
	lots and lots and lots and lots and
	of words.)
\item[Another thing.]  Another item.
	(For purposes of illustration, part of the list entry
	is this sentence with
	lots and lots and lots and lots and
	lots and lots and lots and lots and
	of words.)
\end{description}

\subsection{Centered text and block quotations}

Here is some centered text, with explicit line breaks.
%
\begin{center}
A first line \\
A second line with more text \\
Another line \\
And one more 
\end{center}
%
and here is some text set off as one would a long quotation,
also set in a much smaller type size.
%
\begin{quote}
\begin{tiny}
	(For purposes of illustration, 
	part of the list entry
	is this sentence with
	lots and lots and lots and lots and
	lots and lots and lots and lots and
	of words.)
\end{tiny}
\end{quote}
%
Here is that example again, using a user-defined environment.
%
\begin{TinyQuote}
	(For purposes of illustration, 
	part of the list entry
	is this sentence with
	lots and lots and lots and lots and
	lots and lots and lots and lots and
	of words.)
\end{TinyQuote}

%-----------------------------------------------------------------------

\section{Math}

% the mbox prevents splitting the equation across lines
Here is some math inline:  \mbox{$c = \sqrt{a^2 + b^2}$}.
And here is the same math ``displayed'' (on a separate line
and centered).
\[
c = \sqrt{a^2 + b^2}
\]
And here it is again with an equation number:
\begin{equation}
c = \sqrt{a^2 + b^2} \label{eqn}
\end{equation}
As another more complicated example, taken from one of my programming
assignments, the following is a recursive version of 
Euclid's algorithm for finding the greatest common divisor of 
non-negative integers $a$ and $b$:
\begin{equation*}
\gcd(a, b) = 
\begin{cases}
% \; here adds a small amount of space, which I thought was needed
% to make this look good
\; a & \text{if } b = 0  \\
\; \gcd(b, a\bmod b) & \text{otherwise}
\end{cases}
\end{equation*}
(\mbox{$a\bmod b$} 
is the remainder when $a$ is divided by $b$.)

%-----------------------------------------------------------------------

\section{References, etc.}
\label{refs-section}

\LaTeX\ includes a lot of features useful in writing scholarly
papers (which after all is what it was designed for).

\subsection{Cross-references}

You can label things and reference them later,
with or without the pages on which they appear.
For example,
% EqnRef, etc., are my macros defined earlier
\EqnRef{eqn} appears on \PageRef{eqn},
\SectionRef{first-section} appears on \PageRef{first-section},
and \SectionRef{text-subsection} appears on \PageRef{text-subsection}.
There are more uses of references in the section on
figures and tables (\SectionRef{floats-section}).

\subsection{Footnotes}

% notice that \footnote appears just after the word or sentence to
% which it applies, without a space or linebreak.
% this is to avoid introducing unwanted space in the output.
You can also have footnotes.\footnote{This is a footnote.
It can include more than one sentence, and in fact could be quite long.
}
Here\footnote{%
Another footnote.
}
is another example.

\subsection{Bibliographies}

There is also nice support for bibliographies,
particularly in combination with companion program \BibTeX.
% notice use of "~" for nonbreaking space
Here I'm going to shamelessly plug two of my own (well-aged) 
papers~\cite{plop-1999,sct00-paper} and also cite a book~\cite{gof-book}
and a Web site~\cite{MPI2}.

%-----------------------------------------------------------------------

\section{Figures and tables}
\label{floats-section}

Figures and tables ``float'' (might appear somewhere other than
where they are in the input source).  
You can include with each figure or table a caption
(positioned above or below) and of course a label you can
(and usually should) use to reference it in the text.
In the examples,
captions appear after the figure or table,
though you could choose to put them above.
(Notice that the captions include some explanatory text.
When to put explanatory text in a figure's caption and when
to put it in the main text --- well, it probably merits some thought.)

For example, this section includes \TableRef{table-tbl},
which is defined after this sentence but which might appear
(almost) anywhere.
%
\begin{table}[htbp]
\begin{center}
\begin{tabular}{|l|c|r|}
\hline
This & that & a line \\
\hline
Whatever & whichever & another line \\
\hline
\end{tabular}
\end{center}
\caption{This is a simple table.  Default spacing is, in my opinion,
	not very pretty, so I changed it in the preamble.}
\label{table-tbl}
\end{table}
%
(To make this a slightly more realistic example I'll add some more
text here discussing the table:
This is a sentence with
lots and lots and lots and lots and lots and lots and
lots and lots and lots and lots and lots and lots 
of words.)

As another example, 
\FigRef{xor-fig} shows how to include an image or diagram from a file
created by some other program.
%
\begin{figure}[htbp]
\begin{center}
% ".eps" extension automatically added when processed with "latex"
\includegraphics[width=0.5\textwidth]{xor}
\end{center}
\caption{This is a picture of an XOR circuit I drew for my
	CSCI~2321 class years ago. 
	It's scaled here to half the text width.
	I drew this with \texttt{xfig}.}
\label{xor-fig}
\end{figure}
%
Again I'll put in some more explanatory text so maybe it's clearer
how this works.

As yet another example, particularly relevant in our field,
\FigRef{code-fig} and
\FigRef{morecode-fig} show two simple ways of including code,
while
\FigRef{boxed-code-fig} and
\FigRef{boxed-morecode-fig} show two not-so-simple ways of including
code with an enclosing ``box''.

\begin{figure}[htbp]
\begin{quote}
\begin{verbatim}
#include <iostream>
int main(void) {
    cout << "Hello, world!\n";
    return 0;
}
\end{verbatim}
\end{quote}
\caption{This is a figure that consists of code.  Making it a figure
	keeps it all on one page and gives it a number we can
	reference.  
	I used the \texttt{quoted} environment to indent it.}
\label{code-fig}
\end{figure}

\begin{figure}[htbp]
\begin{quote}
\verbatimtabinput{hello.c}
\end{quote}
\caption{This is another figure with the same code
	as \FigRef{code-fig}, but included from a file.}
\label{morecode-fig}
\end{figure}

\begin{figure}[htbp]
% change fboxrule, fboxsep in preamble to change line width of box, etc.
% the following uses stuff from the fancybox package and is somewhat
% obscure, but seems to work.
\begin{Sbox}
\begin{BVerbatim}
#include <iostream>
int main(void) {
    cout << "Hello, world!\n";
    return 0;
}
\end{BVerbatim}
\end{Sbox} 
% regular verbatim isn't smart enough to center or indent, but this is
\begin{center}
\fbox{\TheSbox} % use "saved box" generated by SBox environment
\end{center}
\caption{This is the code from \FigRef{code-fig} again,
	this time boxed.  This was trickier to do than I thought
	it would be!}
\label{boxed-code-fig}
\end{figure}

\begin{figure}[htbp]
\begin{center}
\fbox{\BVerbatimInput{hello.c}}
\end{center}
\caption{This is the code from \FigRef{code-fig} yet again, 
	included from a file and boxed.  
	This also was trickier to do than I thought
	it would be!}
\label{boxed-morecode-fig}
\end{figure}

%-----------------------------------------------------------------------
%
% bibliography
%

% replace "plain" with "alpha" to get the author/year style 
\bibliographystyle{alpha}
\bibliography{mybiblio}

\end{document}
