# LaTeX

Learning latex is like learning to ride a bike. Once you do it, you look down upon those who haven't learned it yet and run them over.

## Times New Roman

To make the generic font times new roman, add:

\renewcommand\rmdefault{ptm}


Also, one could use the following (we are quoting from http://www.iac.ethz.ch/en/various/Mittelbau/disslatex_en.html:

\fontfamily{abr}\selectfont


if the new font should be used for the whole text following or

{\fontfamily{abr}\selectfont Some Text}


just for the text in the braces. abr is the abbreviation of the font family. The most often used are ptm (Times), phv (Helvetica), pcr (Courier), pbk (Bookman), pag (Avant Garde), ppl (Palatino), bch (Charter), pnc (New Century Schoolbook), pzc (Zapf Chancery), put (Utopia).

## General Template

\documentclass[12pt]{article}
\usepackage{amsfonts,graphicx,epsf,amsmath,amsbsy}
\title{Nonlinear Dynamics Homework One}
\author{T Jones}

\begin{document}
\maketitle
\section*{Problem 1.1}
\subsection*{Introduction}
Blah blah blah

\end{document}


The \documentclass line, which should always come first, defines the document class, which loads special functions and formating appropriate to the type which appears in curly braces, in this case 'article'. Article is appropriate for journal articles, homeworks, or other reports. Document class files have the extension .cls and can be found in subfolders of '/usr/share/texmf/tex/latex/'. The default classes for latex (including article and book) are found in the 'base' subfolder.

The brackets [pre,aps,12pt] define options for the indicated class file. Options include paper and font sizes, sides, columns, etc. You can see the available options in the appropriate .cls file. They are specified at the beginning with '\DeclareOption'.

The \usepackage line indicates optional packages to load. These packages are 'style files' and have a .sty extension. These load support for graphics, additional mathematical symbols and fonts, or new command and fomatting functionality, like double-spacing and the like. These are usually found in subfolders of '/usr/share/texmf/tex/latex/' as well.

The function of the \title and \author commands are self-evident and are provided by the article style file, as is the \maketitle command, which puts the information contained in \title and \author into a title header.

Finally, note the use of the '*' on the sectioning commands. Normally sections are automatically numbered by TeX, but the use of a '*' suppresses that numbering.

## Math

Example of LaTeX math

$$f(k;\lambda)=\frac{e^{-\lambda} \lambda^k}{k!},\,\!$$


$f(k;\lambda)=\frac{e^{-\lambda} \lambda^k}{k!},\,\!$

This will create an equation on its own line, centered on the page. To create math within a sentence, surround the code with a single $on each side, for example: the answer is$x=\epsilon^2$. For more information, see the LaTeX math guide. ## Equation Numbering If you use then your equations will be numbered. If you do not want the current equation to be numbered, use {displaymath} instead of {equation}, or you can use the shortcut$\$ instead of \begin{displaymath} to cut back on the typing. One nice trick is if you want equations with lettered sublabels - 1a, 1b, etc. First you must include the amsmath package, and the enclose the relevant equations in the 'subequations' environment like this:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
...
\begin{subequations}
\begin{eqnarray}
x &=& y^2 \\
&=& (a-b)^2 \nonumber \\
&=& c
\end{eqnarray}
\end{subequations}
...
\end{document}


where the \nonumber command suppresses numbering of that line. You can use any number of ordinary equations or equation arrays inside the subequations environment.

## Referencing

You often want to reference an equation or section by number in a document ("see equation 4"). The simplest way is to write equation numbers in by hand, but this is error prone. Fortunately, LaTeX provides labeling which avoids this problem. Inside of an equation, section, figure caption,... you wish to reference, add the

\label{label_name}


command. When you wish to cite that equation, use the

\ref{label_name}


command. The \ref command will return the number of the equation/section/... assigned by LaTeX when the document is compiled. Note that you will have to TeX the document twice for the refernces to be made (it generates the reference list on the first pass, then assigns them on the second). The labeling can also be used on individual lines of an equation array, inside of the subequations environment, subfigures, etc.

When you get tired of writing

see equation \ref{f_eq_ma}.


in your text, take a look at the cleveref package, which adds the name and the number automatically

see \cref{f_eq_ma}.


## Including Graphics

One often needs to insert various graphics into a document. The functionality to do this is not supported by latex itself, but one most load a graphics package for this. The most common is probably graphicx. So you must include this in your preamble

\usepackage{graphicx}
...
\begin{document}


The recommended file formats are postscript (ps) and encapsulated postscript (eps). You would include a graphic within a figure environment with an \includegraphics command, like this

\begin{figure}
\centering
\includegraphics[scale=2,angle=90]{myfig.ps}
\caption{My fig.\label{my_fig}}
\end{figure}


The square brackets take optional arguments seperated by commas. Scale=number is a magnification. This only works for ps and eps files which have a built in sense of scale. Angle=number rotates the figure ccw in degrees. There are also width=number and height=number options. The number can be given in whatever unit, like width=5cm. There are other options as well.

One often wants to include raster drawings like jpg or png. To do this we need to invoke pdftex in graphicx. This will run pdftex on our file so the output will be a pdf file and not a dvi file. We do this as

usepackage[pdftex]{graphicx}
\begin{document}
...
\begin{figure}
\includegraphics[width=5cm]{myfig.png}
\end{figure}


See this discussion of the various formats supported by pdflatex and latex. Note that you can include raster graphics in the standard, dvi-producing latex by converting them to (large) encapsulated postscript, for example

convert fig.png fig.eps


although in my experience eps-conversion programs are generally finicky.

There is an excellent introduction to LaTeX figures here.

## Making Graphics

Listed roughly in order of increasing coolness:

## Tips and Tricks

Chapter 0?

I personally always love a book that has a chapter 0 for introductory or background material. This can be accomplished in LaTeX by knowing how the internal counters work. Simply, they start at zero and increment by one every time the appropriate command is issued, and then it returns the appropriate number. Since the increment occurs first we need to set our counter to -1 to get a chapter 0:

\begin{document}
\setcounter{chapter}{-1}
\chapter{Background Material}


This can be done for sections, subsections, whatever, if the proper counter is set.

Computer Code in LaTeX

LaTeX provides a verbatim environment which typesets things exactly as they're input, including all spaces, etc... which is ideal for things like computer code. You can enter verbatim text as an environment by using \begin{verbatim} and \end{verbatim}. But you can also enter text inline as well with the \verb command.

The \verb syntax is as follows:

\verb#verbatim text goes in here#


where # is used as a delimiter. But the delimiter can be almost any character, so that you can avoid having to use a character appearing in your verbatim text (exceptions are a space a * or any letter).

The verbatim commands also have starred versions which print spaces visibly with a underscore-like symbol, which is nice if you need to make the number of space present explicit.

If you want to get really fancy, there is a whole verbatim package, with all sorts of cute tricks...

## Graphical Front-ends

There are a couple of front-ends to LaTeX which can help one learn the ropes. Both of these programs are available on the cluster.

• LyX is a What-You-See-is-What-You-Mean graphical front end, which provides real-time display of the rough layout. LyX does not require any pre-existing knowledge of LaTeX, but allows raw LaTeX to be entered if you desire. The table and equation editors are particularly useful. The LyX Wiki has many useful examples and suggestions.
• Kile is raw LaTeX environment for KDE with extra features to help organize the code.
• AucTeX is an editor plugin that adds extended LaTeX functionality. The preview-latex subpackage adds in-buffer previewing of equations and other environments. You can edit rendered environments by simply mousing over the rendered image.

## Latex Internals

See LaTeX Internals for examples and a more detailed look at how latex works on the inside.