Free Online Introduction To Latex Part 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

An Interactive Introduction to LATEX

Part 2: Structured Documents & More

Dr John D. Lees-Miller

December 2, 2017
Outline

Structured Documents Bibliographies


Title and Abstract bibTEX
Sections Exercise
Labels and Cross-References
Exercise What’s Next?
Figures and Tables More Neat Things
Graphics More Neat Packages
Floats Installing LATEX
Tables Online Resources
Structured Documents

I In Part 1, we learned about commands and environments for


typesetting text and mathematics.
I Now, we’ll learn about commands and environments for
structuring documents.
I You can try out the new commands in Overleaf:

Click here to open the example document in Overleaf


For best results, please use Google Chrome or a recent FireFox.

I Let’s get started!


Title and Abstract

I Tell LATEX the \title and \author names in the preamble.


I Then use \maketitle in the document to actually create the title.
I Use the abstract environment to make an abstract.
\documentclass{article}

\title{The Title}

\author{A. Author}
The Title
\date{\today} A. Author
December 2, 2017

\begin{document} Abstract

\maketitle Abstract goes here...

\begin{abstract}
Abstract goes here...
\end{abstract}

\end{document}
Sections
I Just use \section and \subsection.
I Can you guess what \section* and \subsection* do?
\documentclass{article}
\begin{document}

\section{Introduction} 1 Introduction
The problem of . . .
The problem of \ldots
2 Method
\section{Method}
We investigate . . .

We investigate \ldots 2.1 Sample Preparation


2.2 Data Collection
\subsection{Sample Preparation}
3 Results
\subsection{Data Collection} 4 Conclusion

\section{Results}

\section{Conclusion}

\end{document}
Labels and Cross-References
I Use \label and \ref for automatic numbering.
I The amsmath package provides \eqref for referencing equations.
\documentclass{article}
\usepackage{amsmath} % for \eqref
\begin{document}

\section{Introduction}
\label{sec:intro}
1 Introduction
In Section 2, we . . .
In Section \ref{sec:method}, we \ldots
2 Method
eiπ + 1 = 0 (1)

\section{Method} By (1), we have . . .

\label{sec:method}

\begin{equation}
\label{eq:euler}
e^{i\pi} + 1 = 0
\end{equation}

By \eqref{eq:euler}, we have \ldots

\end{document}
Structured Documents Exercise

1
Typeset this short paper in LATEX:

Click to open the paper

Make your paper look like this one. Use \ref and \eqref to avoid
explicitly writing section and equation numbers into the text.

Click to open this exercise in Overleaf

I Once you’ve tried, click here to see my solution .

1
From http://pdos.csail.mit.edu/scigen/, a random paper generator.
Outline

Structured Documents Bibliographies


Title and Abstract bibTEX
Sections Exercise
Labels and Cross-References
Exercise What’s Next?
Figures and Tables More Neat Things
Graphics More Neat Packages
Floats Installing LATEX
Tables Online Resources
Graphics

I Requires the graphicx package, which provides the


\includegraphics command.
I Supported graphics formats include JPEG, PNG and PDF
(usually).

\includegraphics[
width=0.5\textwidth]{gerbil}

\includegraphics[
width=0.3\textwidth,
angle=270]{gerbil}

Image license: CC0


Interlude: Optional Arguments

I We use square brackets [ ] for optional arguments, instead


of braces { } .
I \includegraphics accepts optional arguments that allow
you to transform the image when it is included. For example,
width=0.3\textwidth makes the image take up 30% of the
width of the surrounding text (\textwidth).
I \documentclass accepts optional arguments, too. Example:
\documentclass[12pt,twocolumn]{article}

makes the text bigger (12pt) and puts it into two columns.
I Where do you find out about these? See the slides at the end
of this presentation for links to more information.
Floats

I Allow LATEX to decide where the figure will go (it can “float”).
I You can also give the figure a caption, which can be
referenced with \ref.

\documentclass{article}
\usepackage{graphicx}
\begin{document}

Figure \ref{fig:gerbil} shows \ldots

\begin{figure}
\centering Figure 1: Aww. . . .
\includegraphics[%
Figure 1 shows . . .
width=0.5\textwidth]{gerbil}
\caption{\label{fig:gerbil}Aww\ldots.}
\end{figure}

\end{document}

Image license: CC0


Tables
I Tables in LATEX take some getting used to.
I Use the tabular environment from the tabularx package.
I The argument specifies column alignment — left, right, right.
\begin{tabular}{lrr}
Item & Qty & Unit \$ \\ Item Qty Unit $
Widget & 1 & 199.99 \\ Widget 1 199.99
Gadget & 2 & 399.99 \\ Gadget 2 399.99
Cable & 3 & 19.99 \\ Cable 3 19.99
\end{tabular}
I It also specifies vertical lines; use \hline for horizontal lines.
\begin{tabular}{|l|r|r|} \hline
Item & Qty & Unit \$ \\\hline Item Qty Unit $
Widget & 1 & 199.99 \\ Widget 1 199.99
Gadget & 2 & 399.99 \\ Gadget 2 399.99
Cable & 3 & 19.99 \\\hline Cable 3 19.99
\end{tabular}
I Use an ampersand & to separate columns and a double
backslash \ \ to start a new row (like in the align*
environment that we saw in part 1).
Outline

Structured Documents Bibliographies


Title and Abstract bibTEX
Sections Exercise
Labels and Cross-References
Exercise What’s Next?
Figures and Tables More Neat Things
Graphics More Neat Packages
Floats Installing LATEX
Tables Online Resources
bibTEX 1
I Put your references in a .bib file in ‘bibtex’ database format:
@Article{Jacobson1999Towards,
author = {Van Jacobson},
title = {Towards the Analysis of Massive Multiplayer Online
Role-Playing Games},
journal = {Journal of Ubiquitous Information},
Month = jun,
Year = 1999,
Volume = 6,
Pages = {75--83}}

@InProceedings{Brooks1997Methodology,
author = {Fredrick P. Brooks and John Kubiatowicz and
Christos Papadimitriou},
title = {A Methodology for the Study of the
Location-Identity Split},
booktitle = {Proceedings of OOPSLA},
Month = jun,
Year = 1997}

I Most reference managers can export to bibtex format.


bibTEX 2

I Each entry in the .bib file has a key that you can use to
reference it in the document. For example,
Jacobson1999Towards is the key for this article:
@Article{Jacobson1999Towards,
author = {Van Jacobson},
...
}

I It’s a good idea to use a key based on the name, year and title.
I LATEX can automatically format your in-text citations and
generate a list of references; it knows most standard styles,
and you can design your own.
bibTEX 3
I Use the natbib package2 with \citet and \citep.
I Reference \bibliography at the end, and specify a
\bibliographystyle.
\documentclass{article}
\usepackage{natbib}
\begin{document}
Brooks et al. [1997] show that . . . . Clearly, all odd numbers are prime
[Jacobson, 1999].
\citet{Brooks1997Methodology}
References
show that \ldots. Clearly, Fredrick P. Brooks, John Kubiatowicz, and Christos Papadimitriou. A method-
ology for the study of the location-identity split. In Proceedings of OOPSLA,
all odd numbers are prime June 1997.
Van Jacobson. Towards the analysis of massive multiplayer online role-playing
\citep{Jacobson1999Towards}. games. Journal of Ubiquitous Information, 6:75–83, June 1999.

\bibliography{bib-example}
% if `bib-example' is the name of
% your bib file

\bibliographystyle{plainnat}
% try changing to abbrvnat

\end{document}
2
There is a new package with more features named biblatex but most of
the articles templates still use natbib.
Exercise: Putting it All Together

Add an image and a bibliography to the paper from the previous


exercise.
1. Download these example files to your computer.
Click to download example image
Click to download example bib file
2. Upload them to Overleaf (use the project menu).
Outline

Structured Documents Bibliographies


Title and Abstract bibTEX
Sections Exercise
Labels and Cross-References
Exercise What’s Next?
Figures and Tables More Neat Things
Graphics More Neat Packages
Floats Installing LATEX
Tables Online Resources
More Neat Things

I Add the \tableofcontents command to generate a table of


contents from the \section commands.
I Change the \documentclass to
\documentclass{scrartcl}
or
\documentclass[12pt]{IEEEtran}
I Define your own command for a complicated equation:
\newcommand{\rperf}{%
\rho_{\text{perf}}} ρperf = c0 X + ε
$$
\rperf = {\bf c}'{\bf X} + \varepsilon
$$
More Neat Packages

I beamer: for presentations (like this one!)


I todonotes: comments and TODO management
I tikz: make amazing graphics
I pgfplots: create graphs in LATEX
I listings: source code printer for LATEX
I spreadtab: create spreadsheets in LATEX
I gchords, guitar: guitar chords and tabulature
I cwpuzzle: crossword puzzles
See https://www.overleaf.com/latex/examples and
http://texample.net for examples of (most of) these packages.
Installing LATEX

I To run LATEX on your own computer, you’ll want to use a


LATEX distribution. A distribution includes a latex program
and (typically) several thousand packages.
I On Windows: MikTEX or TEXLive
I On Linux: TEXLive
I On Mac: MacTEX
I You’ll also want a text editor with LATEX support. See http:
//en.wikipedia.org/wiki/Comparison_of_TeX_editors
for a list of (many) options.
I You’ll also have to know more about how latex and its
related tools work — see the resources on the next slide.
Online Resources

I The LATEX Wikibook — excellent tutorials and reference


material.
I TEX Stack Exchange — ask questions and get excellent
answers incredibly quickly
I LATEX Community — a large online forum
I Comprehensive TEX Archive Network (CTAN) — over four
thousand packages plus documentation
I Google will usually get you to one of the above.
Thanks, and happy TEXing!

You might also like