::include_graphics("images/LaTeX_first_example.png") knitr
Getting started with LaTeX
LaTeX makes it easier to produce papers that look great, but it can be overwhelming at the start. These notes help you get up-and-running with LaTeX.
Thank you to Janet Bradly and Maria Racionero for their support of this workshop. These notes are based on ones prepared by Zac Cranko and I for a presentation in 2015 and those that I put together for a 2016 presentation. Zac’s work is used with permission.
Introduction
LaTeX makes it easier to produce papers that look great, but it can be overwhelming at the start. These notes help you get up-and-running with LaTeX.
By the end you will have created an example paper and slides that include a title, author, affiliation, abstract, sections, tables, figures and references that looks like this:
It is best to type everything out yourself, but you can download the tex file from here: https://github.com/RohanAlexander/blogdown_website/blob/master/static/img/paper.tex
Writing papers
Hello world!
You can treat LaTeX as a markup language. You mark your text with commands depending on how you want it to look, and then compile it to produce an output such as a PDF. Get started by downloading LaTeX, for free, from: https://www.latex-project.org/get/.
Use the default settings. You will likely end up installing a bunch of programs. (There is now a smaller version of MacTeX available, but for now it is probably best to stick with the original version for now.)
Let’s make a document. Open ‘TeXShop’ if you’re using a Mac, and ‘TeXStudio’ if you’re using a PC.
In TeXShop/TeXStudio type:
\documentclass{article}
\begin{document}
Hello world! \end{document}
Then click ‘Typeset’. A bunch of miscellaneous files are produced in the process of compiling. Don’t worry about these – all they do is make your directory ugly. The two important files are the tex file, which contains your markup, and the pdf file, which contains your output.
Square brackets are optional in LaTeX, but it is worth specifying the paper size and the font for the documentclass command. If you wanted to specify A4 paper and 12pt text by default then our ‘Hello world!’ becomes:
[a4paper, 12pt]{article}
\documentclass
\begin{document}
Hello world! \end{document}
The type of document that you want goes in the braces. We used ‘article’, which is good for producing papers. There are many classes of document, including ‘book’, ‘letter’, and ‘beamer’ which is for presentations, but ‘article’ is probably the one that you will most commonly use.
Packages
Packages expand the basics of LaTeX. There are a few that you should load every time because they’re often used when writing economics papers:
- amsmath;
- amsthm;
- amssymb; and
- graphicx.
Load these packages by adding the following after documentclass[a4paper, 12pt]{article}, but before begin document:
, amsthm, amssymb, graphicx} \usepackage{amsmath
Then click ‘Typeset’. Nothing should change in the output, we are just typesetting it to make sure that we have not introduced an error. The first three packages help when writing maths. The fourth helps with including graphs.
Everything before begin document is called the preamble, and everything after it is called the content.
Abstract
If you want an abstract, then LaTeX will do the formatting for you if you use the abstract tags. Add this markup just below maketitle:
\begin{abstract}. There are many examples that you can grab and use in your own work.
The point of this document is to introduce you to \LaTeX \end{abstract}
To summarise what your tex file should look like to this point:
[a4paper, 12pt]{article}
\documentclass
, amsthm, amssymb, graphicx}
\usepackage{amsmath
\begin{document}
\title{An Introduction To \LaTeX}, John Tang. All correspondence to rohan.alexander@anu.edu.au.}\\
\author{Rohan Alexander\thanks{Thank you to my primary supervisor
\textit{Australian National University}}, 2017}
\date{14 September
\maketitle
\begin{abstract}. There are many examples that you can grab and use in your own work.
The point of this document is to introduce you to \LaTeX
\end{abstract}
\end{document}
Sections
The main section commands are: section, subsection, and subsubsection. These produce headings of decreasing importance and are numbered automatically. That can be turned off using an asterisk, for instance: section*.
For now let’s add numbered introduction, model, and data sections to our document by adding the following markup after the abstract:
\section{Introduction}
\section{Model} \section{Data}
Maths
Now let’s add some maths into the model section of our paper. Type the following under the model section:
, the estimate becomes :
Using $\alpha$ to indicate the outcome of interest $$ \hat{\alpha} = \frac{\sum^J_{j=1}\beta_j}{\int^{\infty}_{0}f(k)} $$
When you compile your tex file you should get this equation:
\[ \hat{\alpha} = \frac{\sum^J_{j=1}\beta_j}{\int^{\infty}_{0}f(k)} \]
We will now go through the pieces of this.
Greek, limits, infinity, and integrals
We invoked ‘maths mode’ by using double dollar signs. That put the maths that you write on its own line. If you wanted to have your maths content without breaking the line, such as \(x = 5\), then just use single dollar signs.
Within maths mode you can get many greek letters by backslash followed by their name. The examples above were alpha and beta.
Limits, infinity and integrals are built into LaTeX math mode, and can be access with the command lim, infty, and int. You can use accents and underbars if you need to denote what the limit refers to or upper and lower bounds. For instance, add the following markup underneath the formula:
. And when $\lim_{k\rightarrow0}f(k)/k = \infty$ it is the case that $\int^{\infty}_{0}f(k) = 100$
When you compile this it should looks like:
And when \(\lim_{k\rightarrow0}f(k)/k = \infty\) it is the case that \(\int^{\infty}_{0}f(k) = 100\).
Probability, expectations, real numbers, integrals
The \(\mathbb{P}\), \(\mathbb{E}\), and \(\mathbb{R}\) that you may be used to seeing to denote probability, expectation, and the real numbers are made by a call to mathbb within maths mode. For instance add the following to your paper:
$\mathbb{P}$, because of expectations, $\mathbb{E}$, over real numbers $\mathbb{R}$. Often we care about probability,
When you compile it should look like this:
Often we care about probability, \(\mathbb{P}\), because of expectations, \(\mathbb{E}\), over real numbers \(\mathbb{R}\).
You can also make a call to mathcal, for instance in naming sets \(\mathcal{A}\), \(\mathcal{B}\), \(\mathcal{C}\) or for a nice Lagrangian \(\mathcal{L}\).
Fractions
Fractions are built into math mode using frac{}{} and you can nest them if you need to. For instance add the following to your paper:
. It can be surprising when you first learn that $\frac{\frac{x}{y}}{y} = \frac{x}{y^2}$
This should compile to:
It can be surprising when you first learn that \(\frac{\frac{x}{y}}{y} = \frac{x}{y^2}\).
Be careful when using brackets and fractions because sometimes the sizes need to be aligned. You can do it manually, but alternative specify left and right, for instance, compare with and without:
$$\left(\frac{\frac{x}{y}}{y}\right) = (\frac{\frac{x}{y}}{y})$$.
which compiles to:
\[\left(\frac{\frac{x}{y}}{y}\right) = (\frac{\frac{x}{y}}{y})\].
Theorems, definitions and proofs
Theorems and proofs draw on the amsthm package that was loaded earlier. You need to declare the name that you’ll use to refer to it in the preamble. After that you can call a theorem, proposition, description, whatever it was you defined, throughout the document.
First, add this to the preamble:
\newtheorem{theorem}{Theorem}
Then add this to the content:
\begin{theorem}.
Often we want to construct and prove theorems in economics \end{theorem}
In this case, I defined a theorem and LaTeX will print Theorem when compiled You could add another for propositions, etc.
Proofs are similar, but don’t need to be defined in the preamble:
\begin{proof}
My proof of the proposition. \end{proof}
Your tex file should now look like this:
[a4paper, 12pt]{article}
\documentclass
, amsthm, amssymb, graphicx}
\usepackage{amsmath
\newtheorem{theorem}{Theorem}
\begin{document}
\title{An Introduction To \LaTeX}, John Tang. All correspondence to rohan.alexander@anu.edu.au.}\\
\author{Rohan Alexander\thanks{Thank you to my primary supervisor
\textit{Australian National University}}, 2017}
\date{14 September
\maketitle
\begin{abstract}. There are many examples that you can grab and use in your own work.
The point of this document is to introduce you to \LaTeX
\end{abstract}
\section{Introduction}
\section{Model}
, the estimate becomes :
Using $\alpha$ to indicate the outcome of interest
$$ \hat{\alpha} = \frac{\sum^J_{j=1}\beta_j}{\int^{\infty}_{0}f(k)} $$
.
And when $\lim_{k\rightarrow0}f(k)/k = \infty$ it is the case that $\int^{\infty}_{0}f(k) = 100$
, $\mathbb{P}$, because of expectations, $\mathbb{E}$, over real numbers $\mathbb{R}$.
Often we care about probability
.
It can be surprising when you first learn that $\frac{\frac{x}{y}}{y} = \frac{x}{y^2}$
\begin{theorem}.
Often we want to construct and prove theorems in economics
\end{theorem}
\begin{proof}.
My proof of the proposition
\end{proof}
\section{Data}
\end{document}
Text
Paragraphs
LaTeX was designed for maths, but it does text well too. To start a new paragraph, just leave a blank line in your editor, LaTeX will take care of spacing. For instance add the following to your introduction:
. He was a tall, handsome, noble-looking man; he had a deep beautiful voice. My father was an ardent atheist and admirer of Clarence Darrow. He skipped grades the way other boys skip class, he lectured my grandfather's flock on carbon 14 and the origin of species, and he won a full scholarship to Harvard at the age of 15.
My father's father was a Methodist minister
.
He took the letter from Harvard to his father
. Something spoke with his beautiful voice, and it said: It's only fair to give the other side a chance. Something looked through my grandfather's beautiful eyes
There are a few aspects to be aware of:
- To get ‘a quote’, you need to use the key next to the ‘1’ on your keyboard for the opening mark and then the normal quotation mark for the end mark.
- You can makes words italic using textit{some italic words}, or make them bold using textbf{some bold words}.
- Because the dollar sign is used to invoke maths mode, if you want to refer to prices, say $4, then you need to use a slash before the dollar sign. This is the same for the per cent symbol, %.
Some people prefer different formatting on the paragraphs. Although it can cause some issues, you can change this by adding to the preamble:
\usepackage{parskip}
Lists
There are two main types of lists: itemize and enumerate.
Add the following to your data section:
:
Our datasets were downloaded from
\begin{itemize}
\item the ABS; and.
\item the RBA
\end{itemize}
:
We had two priorities
\begin{enumerate}
\item reproducibility; and.
\item ease of use \end{enumerate}
Tables
Tables are often annoying in LaTeX. Fortunately, many programs will automatically format their table outputs with LaTeX markup for your to copy-and-paste into your tex file, and there are websites that can help.
Simple tables are not a problem. For instance, add the following to your data section:
| c || r }
\begin{tabular}{ l
\hline
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\hline \end{tabular}
But it gets complicated. If you commonly use tables then it is easier to get your statistics program to output tables that have been formatted for LaTeX, or use an online table generator, such as http://www.tablesgenerator.com/.
In R, there is a package ‘Huxtable’.
Graphs and pictures
To include graphs or pictures in your document, add the file to the same folder that your tex file is in. From there you can add it. Many adjustments are possible in terms of size and layout. Fortunately, all the labelling is done for us. For instance, download the following image into the folder where your tex file is saved: https://github.com/RohanAlexander/blogdown_website/blob/master/static/img/me.png
Then add this markup into your data section:
[h!]
\begin{figure}
\caption{My first image}
\center[width=0.80\textwidth]{me.png}\label{australia_map}
\includegraphics
\end{figure}
. Income inequality in Australia is illustrated in Figure \ref{australia_map}
Your tex file should look like this:
[a4paper, 12pt]{article}
\documentclass
, amsthm, amssymb, graphicx}
\usepackage{amsmath
\newtheorem{theorem}{Theorem}
\begin{document}
\title{An Introduction To \LaTeX}
\author{Rohan Alexander\\
\textit{Australian National University}}, 2017}
\date{14 September
\maketitle
\begin{abstract}. There are many examples that you can grab and use in your own work.
The point of this document is to introduce you to \LaTeX
\end{abstract}
\section{Introduction}. He was a tall, handsome, noble-looking man; he had a deep beautiful voice. My father was an ardent atheist and admirer of Clarence Darrow. He skipped grades the way other boys skip class, he lectured my grandfather's flock on carbon 14 and the origin of species, and he won a full scholarship to Harvard at the age of 15.
My father's father was a Methodist minister
.
He took the letter from Harvard to his father
. Something spoke with his beautiful voice, and it said: It's only fair to give the other side a chance.
Something looked through my grandfather's beautiful eyes
\section{Model}, the estimate becomes :
Using $\alpha$ to indicate the outcome of interest
$$ \hat{\alpha} = \frac{\sum^J_{j=1}\beta_j}{\int^{\infty}_{0}f(k)} $$
.
And when $\lim_{k\rightarrow0}f(k)/k = \infty$ it is the case that $\int^{\infty}_{0}f(k) = 100$
, $\mathbb{P}$, because of expectations, $\mathbb{E}$, over real numbers $\mathbb{R}$.
Often we care about probability
.
It can be surprising when you first learn that $\frac{\frac{x}{y}}{y} = \frac{x}{y^2}$
\begin{theorem}.
Often we want to construct and prove theorems in economics
\end{theorem}
\begin{proof}.
My proof of the proposition
\end{proof}
\section{Data}:
Our datasets were downloaded from
\begin{itemize}
\item the ABS; and.
\item the RBA
\end{itemize}
:
We had two priorities
\begin{enumerate}
\item reproducibility; and.
\item ease of use
\end{enumerate}
| c || r }
\begin{tabular}{ l
\hline
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\hline
\end{tabular}
[h!]
\begin{figure}
\caption{My first image}
\center[width=0.80\textwidth]{me.png}\label{australia_map}
\includegraphics
\end{figure}
.
Income inequality in Australia is illustrated in Figure \ref{australia_map}
\end{document}
References
LaTeX uses Bibtex for references. To use this open a new file in TeXShop/TeXStudio and add the following:
@Book{DeWitt2000,
,
author = {Helen DeWitt},
title = {The Last Samurai},
publisher = {Hyperion Books},
place = {United State of America},
year = {2000} }
Save it as first_bibliography.bib in the same folder as your tex file. Then add the following at the end of the paragraphs in your Introduction:
\cite{DeWitt2000}
And add the following at the end of your document, on the line above end{document}.
\bibliography{first_bibliography}
Then typeset as BibTeX, (you’ll only need to do this once each time you update your bib file), and then typeset as normal with LaTeX.
Your tex file should look like this:
[a4paper, 12pt]{article}
\documentclass
, amsthm, amssymb, graphicx}
\usepackage{amsmath
\newtheorem{theorem}{Theorem}
\begin{document}
\title{An Introduction To \LaTeX}
\author{Rohan Alexander\\
\textit{Australian National University}}, 2017}
\date{14 September
\maketitle
\begin{abstract}. There are many examples that you can grab and use in your own work.
The point of this document is to introduce you to \LaTeX
\end{abstract}
\section{Introduction}. He was a tall, handsome, noble-looking man; he had a deep beautiful voice. My father was an ardent atheist and admirer of Clarence Darrow. He skipped grades the way other boys skip class, he lectured my grandfather's flock on carbon 14 and the origin of species, and he won a full scholarship to Harvard at the age of 15.
My father's father was a Methodist minister
.
He took the letter from Harvard to his father
. Something spoke with his beautiful voice, and it said: It's only fair to give the other side a chance \cite{DeWitt2000}.
Something looked through my grandfather's beautiful eyes
\section{Model}, the estimate becomes :
Using $\alpha$ to indicate the outcome of interest
$$ \hat{\alpha} = \frac{\sum^J_{j=1}\beta_j}{\int^{\infty}_{0}f(k)} $$
.
And when $\lim_{k\rightarrow0}f(k)/k = \infty$ it is the case that $\int^{\infty}_{0}f(k) = 100$
, $\mathbb{P}$, because of expectations, $\mathbb{E}$, over real numbers $\mathbb{R}$.
Often we care about probability
.
It can be surprising when you first learn that $\frac{\frac{x}{y}}{y} = \frac{x}{y^2}$
\begin{theorem}.
Often we want to construct and prove theorems in economics
\end{theorem}
\begin{proof}.
My proof of the proposition
\end{proof}
\section{Data}:
Our datasets were downloaded from
\begin{itemize}
\item the ABS; and.
\item the RBA
\end{itemize}
:
We had two priorities
\begin{enumerate}
\item reproducibility; and.
\item ease of use
\end{enumerate}
| c || r }
\begin{tabular}{ l
\hline
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\hline
\end{tabular}
[h!]
\begin{figure}
\caption{My first image}
\center[width=0.80\textwidth]{me.png}\label{australia_map}
\includegraphics
\end{figure}
.
Income inequality in Australia is illustrated in Figure \ref{australia_map}
\bibliographystyle{unsrt}.bib}
\bibliography{first_bibliography
\end{document}
Slides
Making slides is similar to writing a paper in that the markup can be the same. But you need to specify when the content of a slide should start and stop and also what the title should be.
To get started, open a new file in TeXShop/TeXStudio and add the following:
\documentclass{beamer}
, amsthm, amssymb, graphicx}
\usepackage{amsmath
\begin{document}
\end{document}
You’ll notice that the only difference is that the document class has been changed to beamer.
Copy the title, author and date, etc markup from your paper, and then paste it between begin{frame} and end{frame}. So your file should look like this:
\documentclass{beamer}
, amsthm, amssymb, graphicx}
\usepackage{amsmath
\begin{document}
\begin{frame}
\title{An Introduction To \LaTeX}
\author{Rohan Alexander\\
\textit{Australian National University}}, 2017}
\date{14 September
\maketitle
\end{frame}
\end{document}
When you save and compile this you should get slides.
You can add a content slide by adding the following after that first slide:
\begin{frame}
\frametitle{Main results}
\begin{itemize}.
\item I find that income inequality is increasing.
\item This is because of slower income growth in country areas
\end{itemize} \end{frame}
I’ve chosen to include a list, but you could include paragraphs, or images or tables using the same markup that you have in your paper.
Misc
Learning more and getting help
- Check that there is an end for every begin, and similarly that all braces that are opened are also closed.
- Compile frequently so that you have a better idea of where the error is.
- Stack Overflow is helpful if you want specific changes or features.
- The wikibooks guide - https://en.wikibooks.org/wiki/LaTeX - is an excellent resource to improve your knowledge.