# Physics 6720: Brief Introduction to LaTeX

by Benjamin Bromley (1998) with some modifications by Carleton DeTar (the latest, October 28, 2012)

examples | hints

LaTeX is a powerful document preparation system which is in widespread use in science and mathematics communities. It is a "mark-up language" and hence you control all document formatting and other properties by writing explicit commands right in the file with the text that is to be published.

Its nicer features include very polished output, reference and cross-reference schemes, and sophisticated equation layouts. It can be cumbersome to do simple things with it, on the other hand, it can be relatively easy to do quite complicated things (especially related to mathematical typesetting). Above all, it is nearly ubiquitous among scientific journals as the preferred submission format (at least in mathematics and physics).

If you are wondering, the "TeX" in "LaTeX" is pronounced "teck". As for the "La", it seems to be up for grabs. Since "latex", as in the synthetic material, sets a precedence of sorts, maybe its not unreasonable to go with the long "a". We will use the TeX Live version of LaTeX in this course.

### Getting started.

Before beginning, PLEASE check to make sure your shell is getting set up to access local TeX and LaTeX resources. Type
printenv TEXINPUTS


#### Creating a simple LaTeX document.

A very stripped-down LaTeX document might look like this:
\documentclass{article}
\begin{document}
Hello world!
\end{document}

The effect is to first define the kind of document, here, an "article" (other options: book, letter, report), then to put the text in a "document" environment which tells the system that this is the material to be published.

These commands would be written up in a text file (using emacs, for example); let's call the file file.ltx, where the extension .ltx is recognized by emacs as standard for LaTeX. It is also quite common to use the extension .tex. The next step is to generate postscript by typing

pdflatex file.ltx

The effect of these statements is to invoke the LaTeX processor which spits out a file in Adobe pdf format called file.pdf

Note: bear in mind that if you have any cross-referencing (not to worry what that means) then LaTeX needs to make two passes at the file. You must, in this case, call the

pdflatex file.ltx

two times before getting a final document.

Note Jr.: if you use the .tex extension, you don't need to type it in when "latex-ing" the file.

#### Previewing the LaTeX document.

To see what you've done, just do
acroread file.pdf


#### A slightly more complicated example.

If you tried the above example, you'd find that the printed document would show a pretty small ("10 point" or "10pt") roman font. Maybe this is not desired. You can change this by introducing a "class" file in the \documentclass declaration. In this next example, we'll switch to a more legible 11pt fontset, as well as demonstrate other features of LaTeX:
\documentclass[11pt]{article}
% This is a comment! LaTeX ignores things to the right of a %-symbol

\begin{document}
Hello
again.

% the line break and extraneous spaces here are ignored.
% "Hello again." will appear as a sentence.
% But blank lines separate paragraphs:

This begins a new paragraph (with only one sentence in it).

We can muck with many features of the text. \\
For example, font types can be easily changed: {\it this is
in italics.}

% The "\\" is a new-line character which forces a line break
% "\it" is a LaTeX command causing italics to be printed.
% the curly braces limit the scope \it to be just the words
% enclosed. It is equivalent to:

For example, font types can be easily changed: \it this is
in italics. \rm

% In the above, the switch from italics to the default (roman)
% is made using the \rm command.

\end{document}

latex will ignore this, because it is beyond the
"end of the document."



#### Adding in math symbols and equations.

LaTeX has a "mathmode" which interprets special characters for mathematical typesetting. The $-sign toggles mathmode and normal mode. For example, if you want a math symbol or symbols in normal text, use The circumference is$2 \pi r$where$r$is the radius.  Note that$-signs always appear in pairs, bracketing mathmode text. Also note that the character \pi for the greek symbol is known only in mathmode. Typing it outside of a $...$ pair will cause latex to generate an error.

LaTeX can also make equations which, in the usual manner, appear on their own separate lines embedded in normal text.

Here is an equation:
$$\label{eq:quad} f(x) = a_0 + a_1 z + a_2 z^2$$

This construct will cause an equation number to appear with the formula; the (optional) label gets assigned the equation number ("2", for example); then the sentence following the equation (it could have preceeded it) will read "Note that Eq. 2 is quadratic."

The \label{tag}--\ref{tag} usage here is an example of referencing which might require the LaTeX to be run twice.

You'll probably find that almost every symbol you'd ever need is defined in LaTeX. However there are two which you may have to define yourself. They are the greater-than-almost-equal-to and less-than-... symbols. A workaround (which may not be necessary in new or future LaTeX versions) is to put the following lines in your LaTeX file above the begin{document} command:

\newcommand{\gae}{\lower 2pt \hbox{$\, \buildrel {\scriptstyle >}\over {\scriptstyle \sim}\,$}}
\newcommand{\lae}{\lower 2pt \hbox{$\, \buildrel {\scriptstyle <}\over {\scriptstyle \sim}\,$}}

Then in your equations you can use \gae or \lae to produce the symbols.

Figures are somewhat special in LaTeX, since it is a primarily a typesetting language. It is possible to include figures, but the standard way is first to generate them elsewhere in postscript format. Then, a particular style file graphicx can be used to help merge the LaTeX and figures in a variety of formats, the most common being Adobe pdf or encapsulated Postscript. Here is an example. We first construct a postscript file using gnuplot. Here are the gnuplot commands to make a Postscript file plotting sin(x):

set output "sine.ps"
set terminal postscript portrait color "Courier" 24
set size 1.0,0.5
plot sin(x)

Note that it is important to make the font size sufficiently large (we chose 24 point) to be legible when reduced to fit the space on a page. I like the clean Courier font, but that is a matter of taste. The size command reduces the vertical scale by a factor of 0.5, making a more pleasing rendering of the default plot. The portrait option is necessary, since the default mode is landscape, which rotates the plot 90o.

Convert the file to Adobe pdf format using the Unix command

epstopdf sine.ps


Then, here is how to insert the figure into the LaTeX document:
\documentclass[11pt]{article}
\usepackage{graphicx}
\begin{document}
...
\begin{figure}[htbp]
\begin{center}
\includegraphics[height=2.5in]{sine.pdf}
\end{center}
\caption{This is my sine.}
\label{mysine}
\end{figure}
...


The height scales the (encapsulated) postscript file sine.ps so that its vertical (y) dimension is 2.5 inches. (Other units of length are cm, pt=points). Use width= to scale the width. The LaTeX center environment centers the figure.

The [htbp] is optional; if is is included, it guides LaTeX in placing the figure on a page. The first choice is h=here, the second t=top, third is b=bottom, and p=page-of-its-own. Here we've made the figure 2.5 inches high, so it should fit in the text, but if the figure were too big to mingle with text on the page, it would be moved to a page of its own.

#### Formatting the page.

The standard document styles provided with LaTeX incorporate professional page design choices. So one is unlikely to improve the appearance of a document by altering the page layout. However, if this is not a concern, here is how you can play with the page layout. For example, on an 8.5" x 11" page, the default side margins are 1.5" wide. You can change this to 1" by putting

\evensidemargin 0.0in
\oddsidemargin 0.0in
\textwidth 6.5in

after the \documentclass command but before the \begin{document} statement. Odd and even refer to page numbers, and the margin always refers to the left side of the page. LaTeX defines the margin to be 1" plus the value of the sidemargin variables. We must also reset the text width variable if we want the right margin to be 1" also. (Clearly we want the sum of L+R margins and the text width to be the page width.)

See the file ~p6720/examples/latex/latex-cv-template.ltx for more possibilities.

### Hints

Please let the instructor know of any difficulties you may be having. This section will be updated as problems arise. For now, the best hint is to play with the LaTeX files .ltx and .tex in the ~p6720/examples/latex directory.