You are on page 1of 7

A Brief Introduction to LATEX February 2, 2019

A Brief Introduction to LATEX


Stuart Field
Department of Physics, Colorado State University
Version 1.2, January 31, 2019

1 Obtaining a TeX/LaTeX installation


TeX is a large set of programs and files, originally written by Donald Knuth, that allows for high-quality typesetting,
with a particular focus on mathematical typesetting. Raw TeX is actually rather difficult to use, so Leslie Lamport
wrote LaTeX, and extensive set of TeX macros (since extended by many others) that make using the TeX system
quite straightforward. In this document, we’ll refer to the TeX/LaTeX system as simply LaTeX.
There are numerous high-quality, free LaTeX installations available. For any installation, you will need two
components. First, you’ll need to install LaTeX itself. A full LaTeX installation can be quite large, on order of
4 GB. This installation will contain all the files needed to run LaTeX in a command-line environment. Second,
you’ll need a front end program for LaTeX that allows for easy text entry, running LaTeX, and viewing of the
resulting output as a pdf file.
For Mac, Windows, and Linux, a popular LaTeX installation is MikTeX. Installation of this package is straight-
forward; see the instructions for your operating system on the MikTeX homepage. It’s easiest to install the basic
package, at around 190 MB, which contains only a small subset of all LaTeX packages. If you run LaTeX and a
package you need is not available, MiKTeX will download it automatically. I found that for the LaTeX files and
packages that we’ll be using, MiKTeX will need download several files, so for the first time you LaTeX a file there
may be some delays in processing.
A nice front end for Mac, Windows, and Linux is Texmaker. For Mac, I have also used TeXShop.
A third possibility is to to use Overleaf, a free online TeX/LaTeX system. This might be the best choice for
anyone who doesn’t want to deal with the hassle of downloading and installing a large LaTeX installation. You can
get a free account that will work perfectly for the level of LaTeX used in this course. In Overleaf, you work on
your document in your browser, and the output file is shown in a separate window, and updated in semi-real time.
Your files are stored in Overleaf’s cloud, but you can create a pdf of your LaTeX’d file at any time for reference or
printing.
Use the file Template.tex on Canvas to get started. The header—everything above and including the begin{document}
command—can be left as is. Fill in the title{} and author{} brackets, and replace the text between \begin{abstract}
and \end{abstract} with the text of your abstract. Then, you can just fill in the \section{} brackets, and go
ahead and type in your text. Below are some hints on how to use some other common features of LATEX. This is
hardly a comprehensive treatment. For more information, see Leslie Lamport’s book, LATEX, A Document Prepara-
tion System.

1
A Brief Introduction to LATEX February 2, 2019

2 Preliminaries
In this document, the text you type into your .tex file is shown on the left, and the formatted LATEX output is shown
on the right.
LaTeX ignores multiple spaces. One LaTeX ignores multiple spaces. One space is as good as
space is as good as two. LaTeX two. LaTeX also ignores single carriage returns, as you
also ignores single carriage returns, can see. To get a new paragraph, you need to insert a
as blank line.
you Like this...
can see. To get a new paragraph, you
need to insert a blank line.

Like this...

3 Math
One of the main reasons for using LaTeX is that it produces very high-quality mathematical output. It is significantly
better than that produced by Word’s Equation Editor, and much much better than that typically produced by a PH425
student. Here I’ll just touch on a few common math techniques. The template file shows a few more things.
To type symbols and equations inline, you need to bracket them with dollar signs $. . . $. Thus we have

Einstein’s famous equation, Einstein’s famous equation, E = mc2 , relates mass...


$E = mc^2$, relates mass...

Note that all symbols are math, and so should be bracketed with $. . . $.
...where $x$ is the particle’s position, ...where x is the particle’s position, v its velocity, and θ
$v$ its velocity, and $\theta$ its angle its angle of launch.
of launch.

Often you want to type display equations, that is, equations that are shown on their own line. A numbered display
equation is formatted using the equation environment. Note that no $...$ are needed.
\begin{equation}
E = mc^2 E = mc2 (1)
\label{eq:Einstein}
\end{equation}

Presumably, if you number an equation you’ll want to refer to it in the text. This is done using the \label
command. The text inside the brackets (eq:Einstein) is the label. It can be any text you like without spaces. I use
the eq: part to remind myself that this is a label for an equation (figures and tables are labeled similarly), but you
don’t need to do things this way. To refer to the equation in the text, you type Equation~\ref{eq:Einstein} at
the beginning of a sentence, or Eq.~\ref{eq:Einstein} within a sentence. This will show up in the output file as
“Equation 1” or “Eq. 1”, respectively. The hyphen ~ is a non-breaking space, so that the word “Equation” and the
number don’t appear on separate lines. A nice feature of this method is that all your equations and their references
are automatically numbered in sequence, and updated correctly if you insert a new equation in between two existing
ones.

2
A Brief Introduction to LATEX February 2, 2019

An un-numbered equation is written


\begin{equation*}
E = mc^2 E = mc2
\end{equation*}

There are a very large number of LaTeX math symbols available. They are typed in your LaTeX file as a
backslash (\) command, always in math mode ($. . . $). Many of them have pretty obvious command names. Thus,
for instance, the Greek letter alpha is typed as \alpha. See the file “LaTeX Math Symbols” on Canvas for a
comprehensive list. Some of the common ones you might need in this course are

α \alpha µ \mu φ \phi × \times ∑


R
\sum
β \beta π \pi ω \omega ≤ \leq \int
γ \gamma ρ \rho Ω \Omega ≥ \geq cos \cos
δ \delta σ \sigma ∆ \Delta h̄ \hbar sin \sin
θ \theta τ \tau ± \pm ∇ \nabla tan \tan
◦ \circ  \ll  \gg

Note that there are special symbols for common function names such as \sin that result in the characters “sin”
being typeset in non-italic letters, which is how function names should be typeset. Compare

\sin^2 u + \cos^2 u = 1 sin2 u + cos2 u = 1 (correct)


with

sin^2 u + cos^2 u = 1 sin2 u + cos2 u = 1 (incorrect)

There are also a number of LaTeX structures you’ll commonly be using. These include:
• Superscripts and subscripts:

x^2 x2
x^{2 y} x2y
x_0 x0
x^2_0 x02

Superscripts and subscripts that represent symbols are typeset in math mode, but superscripts and subscripts that
are words (or abbreviations of words, such as “e” for “electron”) are in Roman font, using the \mathrm{} command:
The velocity $v_x$ The velocity vx
The applied field $B_\mathrm{magnet}$ The applied field Bmagnet [not Bmagnet ]
The electron neutrino $\nu_\mathrm{e}$ The electron neutrino νe [not νe ]

• Fractions:
y2
x = \frac{y^2}{z} x=
z

3
A Brief Introduction to LATEX February 2, 2019

• Square roots:
p
r = \sqrt{x^2 + y^2} r= x2 + y2

• One thing above another:

x â ū
\overline{x} \hat{a} \bar{u}
ẋ ẍ ~v
\dot{x} \ddot{x} \vec{v}

• Boldface and normal (Roman) font in math mode:


$\mathrm{cos}\theta = cosθ = A · B
\mathbf{A \cdot B}$

To place tall math such as fractions in parentheses or brackets, enclose the math in \left( and \right); the
brackets can be {}, [], (), ||, etc:
q = \frac{1}{2}
x3
  
\left(\frac{x^3}{b^2}\right) 1 sin(x)
q=
\left[\frac{\sin(x)}{\cos(x)}\right] 2 b2 cos(x)

4 Figures
Figures can be inserted using the \includegraphics command, inside a figure environment:

-9 19
16x10 2.0x10

14 RH

Carrier Density (cm-3)


12 1.5
1 \begin{figure}[h]
RH (Ω⋅cm/G)

10
2 \centering 8 1.0
3 \includegraphics[scale=0.7]{myFigure.pdf} 6
4 \caption{This is the caption. It describes 4 n 0.5

5 what the figure is about.} 2

6 \label{fig:myFigure} 0 0.0
-8 -6 -4 -2 0 2 4 6 8
7 \end{figure} B (kG)

Figure 1: This is the caption. It describes what the


figure is about.

In line 1, the [h] tries to put the figure “here”, as close as possible to where the \includegraphics command
is. Other choices are [t] for the top of the page and [b] for the bottom of the page. Line 2 centers the figure on the
page. Line 3 actually places the graphic. Between the square brackets you can put a parameter to scale the figure, or
to set its absolute width ([width = 3.0 in]). If you omit the square brackets entirely, the figure will be placed at
its natural width.
The filename of your figure goes between the braces {...}. In both TeXShop and MiKTeX you can use .pdf,
.jpg, and .png files. Of course, .pdf files are preferred because they are resolution-independent, but if you have to

4
A Brief Introduction to LATEX February 2, 2019

use a bitmap format, .png is better than .jpg because the latter is a compressed format. You can put the graphics files
in the same folder as the .tex file. Or, you can put them in a folder (say, “myFigures”) in the folder that contains the
.tex file. Then (see Template.tex) you’ll need to include \graphicspath{{myFigures/}} at the top of your file.
Every figure needs a caption in braces, as shown in lines 4–5. Finally, because every figure needs to be referenced
in the text, you need some way to refer to it. To do so, use the \label command, as shown in line 6. The label in
the braces can be any text (without spaces) you like. I often use fig: to remind myself that this is a figure label.
You can then refer to the figure elsewhere in the text as Fig.~\ref{fig:myFigure}, which appears in the text as
“Fig. 1”.
Always refer to figures by their numbers, as shown above. Never refer to them in the form, “in the figure below
we see that. . . ” or something similar. Also, it’s common for a figure reference that starts a sentence to have the word
“Figure” written out in full, while a reference in the middle of a sentence uses the abbreviation “Fig.” Also note that
in each case the Figure or Fig. is capitalized.
Graphing and figure miscellany:
• Don’t insert gigantic figures into your report. Scale them appropriately so that they don’t overwhelm the page.
• Don’t use too large a font in figures; 8 or 9 point is plenty large. I often create my figures at about twice the
scale I want them to appear in my document, and use a 16 point or so font which looks about right when scaled
down by 2×.
• In Igor’s Modify Axis dialog (double click on an axis), under the Axis tab, turn off the Standoff checkbox. If
it’s checked, it adds an annoying little extra length to the axis.

5 Tables
Tables in LaTeX are somewhat tricky. There’s an example table in Template.tex that you can look at:

1 \begin{table}[h]
2 \centering
3 \begin{tabular}{ccccl}
4 \hline
5 ~$\rho $~ & $~~~~T_c~~~~$ &~~$\lambda(T/T_c = 0.9)$~~ & $\kappa$ &Source\\
6 ($\mu\Omega\cdot\mathrm{cm})$ & (K) & (nm) & & \\
7 \hline
8 10.8 & 1.64 & 510 & 2.6 & Ekin\\
9 20.8 & 1.81 & 675 & 5.0 & Ekin (interpolated)\\
10 35.0 & 2.0 & 830 & 8.4 & Daldini {\it et al.}\\
11 \hline
12 \end{tabular}
13 \caption{Properties of several granular aluminum films. $\kappa$ is the
14 Ginzburg-Landau parameter.}
15 \label{table:superconductors}
16 \end{table}

5
A Brief Introduction to LATEX February 2, 2019

which gives

ρ Tc λ (T /Tc = 0.9) κ Source


(µΩ · cm) (K) (nm)
10.8 1.64 510 2.6 Ekin
20.8 1.81 675 5.0 Ekin (interpolated)
35.0 2.0 830 8.4 Daldini et al.

Table 1: Properties of several granular aluminum films. κ is the Ginzburg-Landau parameter.

Lines 1 and 2 are similar to their figure counterparts. Line 3 starts the table. In the brackets you need to
put one letter for each column of your table, either c for a centered column, l for a left-justified column, or r
for a right-justified column. Whenever you want a horizontal line in your column, use \hline. Each row of
the table contains the table data separated by ampersand (&) characters; the end of each row should have two
backslashes (\\). The caption and reference are handled similarly to a figure; refer to the table above using
Table~\ref{table:superconductors}.
In Table 1, I used a number of non-breaking spaces (~) in the header row to space out the columns a little. There
is a way to add space by using actual numerical spacing, but it’s so complicated as to not really be worth it.

6 Punctuation and Spacing Around Equations


Equations are actually part of flow of your written text and need to be punctuated and spaced accordingly. If we were
to write out an equation in words, it might look like “Newton’s second law, force equals mass times acceleration,
is perhaps the. . . ” The commas here are obviously needed. So if were write this using a mathematical equation we
punctuate it in exactly the same way. The table shows the correct way, and some common mistakes.

Newton’s second law,


Correct. Mimics the written version.
F = ma,

is perhaps the. . .
Newton’s second law is: Wrong. You wouldn’t write, “Newton’s second
law is: force equals. . . ”
F = ma.
Two mistakes. First, the sentence after the equa-
tion shouldn’t be indented; it’s just a continuation
Newton’s second law is of the existing paragraph. To prevent this, don’t
leave a blank line after the equation. Second, the
F = ma, word “Which” should not be capitalized, as this
sentence fragment is again just a continuation of
Which is perhaps the most. . . an existing sentence.

7 References
It’s likely that your report will have some references. Here’s how to put them in. In the text, where you eventually
want the superscripted citation to appear (. . . as shown in 1978 by Smith23 ), you write ...by Smith\cite{Smith1978}.
The Smith1978 is your cite key, and it can be any text you like. The author’s last name followed by the year is a
good way to keep track of things. You can have more than one reference at a time:

6
A Brief Introduction to LATEX February 2, 2019

...by several researchers\cite{Smith1978,Jone1996}


. . . by several researchers23, 24
Then, at the end of the file, after the thebibilography command, you create a series of bibitems, each with
the corresponding key. See Template.tex for details.
To get the references properly numbered (as wells equations and figures) you’ll need to run LaTeX twice. Also
note that punctuation goes before the citation: . . . in 1978 by Smith.23 (not . . . in 1978 by Smith23 .)

8 Miscellaneous LATEX Stuff


• An opening double quote is made with two acute (‘) characters (to the left of the 1 key); the closing quote is
two apostrophes (’). Thus Shakespeare wrote ‘‘to be or not to be’’ typesets to Shakespeare wrote
“to be or not to be”.
• LaTeX automatically adds extra space after a period, except if the period follows a single capital letter (so
that initials of names don’t have extra space after them). Sometimes you don’t want this extra space, such as
when you type Dept. of Energy. If I type this as Dept. of Energy I get Dept. of Energy, which you can see
has a little too much space after the “Dept.” To prevent this, use a “regular space” command \ (a backslash
followed by a space). Then, typing Dept.\ of Energy I get Dept. of Energy, which is correctly spaced.
• Sometimes you need a non-breaking space, so that the two words separated by the space cannot be broken
across two lines. An example would be the space between the “Fig.” and the “1” in Fig. 1. This looks bad as
Fig.
1. So put a non-breaking space (tilde, or ~) between them. Note that this also fixes the extra space you’d get
after the period in Fig. Another use for the nonbreaking space is between a number and its unit: 23~V.
• You can italicize text by using {\it my text}, giving my text; bold text can be written using {\bf...}.
• A simple hyphen is used to, well, hyphenate: ...a two-electron state (. . . a two-electron state). For
a run of numbers, one uses an “n dash”, created by two hyphens, which is a little longer than a hyphen:
...from 34--65 (“. . . from 34–65”). Finally, an “m dash” is formed by three hyphens:
...the neutron---discovered in 1932---was found to have...
. . . the neutron—discovered in 1932—was found to have. . .
• Use \times (×) for multiplication, not “*”.
• Units are not in italics, and are spaced from the number: 12.0 V, not 12.0V or 12.0 V . You should use a
non-breaking space ~ between the number and the unit. (Note: The spacings you get this way are a little too
big. If you want to become a TeXpert, look up the siunitx package, which perfectly formats numbers and
SI units.)
• Names of elements are not italicized (e.g., helium, not Helium).
• Scientific notation should be written as 6.02 × 1023 , not 6.02E23!

9 Important stuff unrelated to LATEX but I had to put it somewhere


• Don’t share your graphs with your lab partner. Each student needs to make their own graphs, just as each
student needs to write their own text.
• Don’t report more than two significant digits in an error.
• Errors should be reported as follows. For a simple decimal number, something like 1.75 ± 0.08 m/s is straight-
forward to interpret. For scientific notation, I would suggest (5.29 ± 0.05) × 10−18 C as being very easy to
read. Certainly don’t write 5.29 × 10−18 C ± 5.0 × 10−20 C—it’s very difficult to compare the size of the error
to that of the value itself.
• Spellcheck!

You might also like