Math 300: LaTeX Programming (back to Math 300 notes)

LaTeX is a programming language. While this sometimes causes it to be more difficult to use than simple word processors, it also makes it powerful. We can exploit that programming capability in a variety of ways.

Create Commands

We can create our own commands in LaTeX, using the \newcommand and \renewcommand macros. The format of \newcommand is

      \newcommand{\nameofcommand}[optional number of arguments]{text to appear}
	  
For example, we could create a command to type "Washington State University" for us:
	  \newcommand{\WSU}{Washington State University}
	  This software, which was developed at \WSU, is provided "as is" ...
	  
We could create a command to insert a small icon in our text, e.g. for each item in a list:
	  \newcommand{\icon}{\includegraphics{wsuicon.eps}}
	  \begin{description}
	  \item[\icon \WSU:] Is a multicultural extravaganza.
	  \item[\icon \WSU:] Is ``world class - face to face''.
	  ...
	  
We may use other homemade commands in our new commands, and may feed arguments to the commands:
	  \newcommand{\wsuitem}[1]{\item[\icon \WSU] #1}
	  \begin{description}
	  \wsuitem{Is a multicultural extravaganza.}
	  \wsuitem{Is ``world class - face to face''}
	  ...

Floating Objects

LaTeX can "float" objects such as tables and figures to the place in the document where they fit best. All this really means is that floating objects constitute blocks of material that stay together, and are inserted into a place where they fit typographically, regardless of whether they fit well there logically. For example, a floating figure may not stay very close to the reference to it in the text, but it will fit well into the typography, when LaTeX is done with it. An example of a figure appears below.

\begin{figure}[hb] The figure environment floats. The optional hb argument tells TeX to try to keep the figure close to the place it appears in the markup, and to try to put it at the bottom of a page.
\includegraphics[width=5cm]{mypic.eps} Included graphics must be in encapsulated postscript format. They may be resized at will.
\caption{My picture} The \caption command is only valid in figure and table environments. It numbers the figure or table automatically.
\end{figure}  

Note that in order to use the includegraphics command you must load the graphicx package using usepackage{graphicx} somewhere in the header.

Citations and References

LaTeX counts and tracks equation numbers, sections, figures, and tables for us. We can maintain the logical integrity of our document by referring to those items by name, so that we do not have to make extensive changes when we insert new material. All we have to do is to assign names to those objects using the \label command: e.g.

\caption{\label{fig:pic} My picture}
applies the name "ref:pic" to the figure. Once that is done, we can refer to the figure using the \ref command:
As is seen in Figure \ref{fig:pic}, I have a big nose.

In the typeset document, the number of the figure appears in place of the \ref. Likewise, we can name a section number using the \label command anywhere in the section:

	  \section{What elephants remember}
	  \label{sec:elephants}This is Section \ref{sec:elephants}.

applies the name sec:elephants to the number of the current section. The \ref command is replaced by the section number in the typeset document.

We can name equations as well: e.g.

	  \begin{eqnarray}
	  f(x) &=& \frac{x^2-1}{x+1}\label{eq:fdef}\\
	     &=& x+1\quad {\rm for}\ x\ne -1\label{eq:fredef}
	  \end{eqnarray}
	  
captures the numbers of each of the two equations, and applies the names given to them.

It is customary, but not required, to apply names that help to identify the item named; as in sec:name or eq:name.

Another situation in which we allow LaTeX to count for us, and just apply names to the numbers, is in bibliographies and reference lists. The \bibitem command, for use in the "thebibliography" environment, takes a single argument: the name to be applied to the reference number. We refer to the number using the \cite command. Consider an example.

		In the text, we refer to the book by Knuth \cite{knuth84} by number.  
		We do not need to know the number - \LaTeX\ takes care of that.
		\begin{thebibilography}{X}
		\bibitem{calvin} Watterson, B., {\bf Homicidal Psycho Jungle Cat,}
		Andrews McMeel, New Jersey, 1994.
		\bibitem{knuth84} Knuth, D., {\bf The \TeX book,} Addison-Wesley, 
		Reading, 1984.
		\end{thebibliography}