Learning Material on Data Science


Introduction to LaTeX

by Markus Mößler


Syntax of LaTeX


This section is about the syntax of LaTeX documents (.tex-documents) and includes

  1. the syntax of the general structure of LaTeX documents and…
  2. the syntax of particular structures, such as lists, tables, figures, etc.

General Structure

The following sections is about the general structure of .tex-documents.


Simple Document

Concept

In general, a document consists of:

  • A documentclass, e.g., article for articles or beamer beamer slides
  • A preamble, where packages are loaded and formats and styles are specified
  • A document area, where you insert your content

Comments:

Use % to add comments to your document!


% Define class, e.g., article or beamer,...
\documentclass{article}

% "Preamble area" to load packages and specify formats and styles
% See later...

% Start of actual document
\begin{document}

% A very simple content
Hello world!

% End of actual document
\end{document}

Replication (local)

Replicate the example using your LaTeX-editor, e.g., here, TeXStudio:

  1. Open TeXStudio and click on “File”“New” to open a new .tex file.
  2. Copy the LaTeX-code above.
  3. Insert the code into the new .tex-document.
  4. Click on “Compile” (single green arrow) or on “Build & View” (double green arrow) in the “tools bar”.

Replication (web based)

Replicate the example using Overleaf:

  1. Log into your overleaf account.
  2. Click on “New Project”, “Blank Project” and name the project, e.g., “Simple Document”.
  3. Mark everything and replace it by the LaTeX code above.
  4. Click on the “Split screen” symbol on the very right (two white arrows).
  5. Click on “Recompile”.

Back to Start of LaTex


Styles

Concept

There are certain tags to enforce certain styles, e.g., \textit{bla} for text styled in italics.


% Define class, e.g., article or beamer,...
\documentclass{article}

% "Preamble area" to load packages and specify formats and styles
% See later...

% Start of actual document
\begin{document}

% A very simple content very big and in italics
\huge{\textit{Hello world!}}

% A very simple content very small and in bold
\tiny{\textbf{Hello world!}}

% A very simple content in typewriter
\texttt{Hello world!}

% End of actual document
\end{document}

Back to Start of LaTex


Environments

Concept

Environments are delimited by an opening tag \begin and a closing tag \end. Everything inside these tags will be formatted in a special manner depending on the type of the environment.

Thus, the document environment starting with \begin{document} and ending with \end{document} is the central and most general environment of the document and in the preamble you define the formats and styles of the document environment.

However, there are also other, more specific, pre-defined environments, such as tables and figure (see “Particular Structures” later).

A quite simple pre-defined environments is the environment called center.


% Define class, e.g., article or beamer,...
\documentclass{article}

% "Preamble area" to load packages and specify formats and styles
% See later...

% Start of actual document
\begin{document}

% A very simple content inside a very simple environment
\begin{center}
   Hello world centered!
\end{center}

% End of actual document
\end{document}

Help

Additional help on environments can be found here.


Back to Start of LaTex


Floats

Concept

Floats are used to contain things that must be placed inside a single page, i.e., they cannot be broken over multiple pages.

Floats can contain tables or figures or one can define custom floats.


Purpose

Floats deal with desirable properties of objects which cannot be broken over multiple pages, i.e.:

  1. We should be able to specify the position of these objects in the document.
  2. We should be able to add a caption to these objects.
  3. We should be able to add a reference to these objects, so that it can be cross-referenced elsewhere in the document.

Later we will see how to specify the position, a caption and a reference to tables, figures and equations (see “Particular Structures” later).


Help

Additional help on floats can be found here.


Back to Start of LaTex


Packages

Concept

There are lot of pre-defined formats and environments provided in packages and class files.

The Comprehensive TeX Archive Network (CTAN) is the central place for all kinds of material around TeX and LaTeX.

You can browse lists of TeX and LaTeX packages and class files on CTAN subpage here.


Download and Management

Download and management of the packages is part of the functionality of your TeX-distribution, e.g., MikTeX.

However, the download starts automatically during the compilation of the .tex-file using your TeX-editor, e.g., TeXstudio.

E.g. by including \usepackage{amsmath} in the preamble, the package amsmath will by automatically downloaded if it has not already been done. Here, amsmath is a package to facilitate mathematical expressions.

Sometimes you have to confirm the download.


Back to Start of LaTex


Particular Structures

The following sections is about selected particular structures of .tex-documents, i.e., how to insert lists, tables, figures, math expressions, and how to use citations.


Sections

Include sections

Sections can be added using commands section{}, subsection{} and subsubsection{}.


% Define class, e.g., article or beamer,...
\documentclass{article}

% "Preamble area" to load packages and specify formats and styles
\usepackage{hyperref} % use hyperref package for links

% Start of actual document
\begin{document}

\section{Section 1}\label{Sec_01}

This is the content of Section \ref{Sec_01}.

\subsection{Section 1.1}\label{Sec_0101}

The is the content of Section \ref{Sec_0101}.

\section{Section 2}\label{Sec_02}

This is the content of Section \ref{Sec_02}.

\subsection{Section 2.1}\label{Sec_0201}

The is the content of Section \ref{Sec_0201}.

The content of Section \ref{Sec_01} can be found above.

% End of actual document
\end{document}

Help

Additional help on sections can be found here.


Back to Start of LaTex



Lists

Include lists

Lists can be added using the itemize and enumerate environment for unordered and ordered lists, respectively.


% Define class, e.g., article or beamer,...
\documentclass{article}

% "Preamble area" to load packages and specify formats and styles

% Start of actual document
\begin{document}

This is a list of itemizations:

\begin{itemize}
  \item Sub01
  \begin{itemize}
    \item Sub0101
    \begin{itemize}
      \item Sub010101
      \item Sub010102
      \item[...] 
    \end{itemize}
    \item[...]
  \end{itemize}
  \item[...]
\end{itemize}

This is a list of enumerations:

\begin{enumerate}
  \item Sub01
  \begin{enumerate}
    \item enumerate
    \begin{enumerate}
      \item Sub010101
      \item Sub010102
      \item[...] 
    \end{enumerate}
    \item[...]
  \end{enumerate}
  \item[...]
\end{enumerate}

% End of actual document
\end{document}

Help

Additional help on lists can be found here.


Back to Start of LaTex


Tables

Include tables

Tables can be added using a combination of the table and tabular environment and the booktabs package.

Note, the bookstabs package is based on the following philosophy of designing tables:

  1. Never use vertical lines.
  2. Never use double lines.

% Define class, e.g., article or beamer,...
\documentclass{article}

% "Preamble area" to load packages and specify formats and styles
\usepackage{hyperref} % use hyperref package for links
\usepackage{booktabs} % use booktabs package

% Start of actual document
\begin{document}

Table \ref{Tab_01} shows some random table positioned (1) here (\texttt{h} in \texttt{htp}) if feasible, if not, than (2) at the top (\texttt{t} in \texttt{htp}) if feasible, if not, than, (3) at a special page for floats (\texttt{p} in \texttt{htp}).

\begin{table}[htp]
  \centering
  \begin{tabular}{lccc}
    \toprule
    1st column & 2nd column & 3rd column & 4th column \\
    \midrule
    1st row & value $1$ & value $2$ & value $3$ \\
    2nd row & value $4$ & value $5$ & value $6$\\
    $\vdots$ & $\vdots$ & $\vdots$ & $\vdots$ \\
    \bottomrule
  \end{tabular}
  \caption{Some random table}
  \label{Tab_01}
\end{table}

Table \ref{Tab_02} shows some random table positioned at the bottom.

\begin{table}[b]
  \centering
  \begin{tabular}{lccc}
    \toprule
    1st column & 2nd column & 3rd column & 4th column \\
    \midrule
    1st row & value $1$ & value $2$ & value $3$ \\
    2nd row & value $4$ & value $5$ & value $6$ \\
    $\vdots$ & $\vdots$ & $\vdots$ & $\vdots$ \\
    \bottomrule
  \end{tabular}
  \caption{Some random table (again)}
  \label{Tab_02}
\end{table}

% End of actual document
\end{document}

Help

Additional help on:

  • tables can be found here
  • booktabs can be found here

A table generator can be found here.


Back to Start of LaTex


Figures

Include figures

Figures can be added using an figure environment and the package graphix.


Download the figure below and save in the same directory as your .tex file.

% Define class, e.g., article or beamer,...
\documentclass{article}

% "Preamble area" to load packages and specify formats and styles
\usepackage{hyperref} % use hyperref package for links
\usepackage{graphicx} % use graphicx package

% Start of actual document
\begin{document}

Figure \ref{Fig_01} shows the real USD GDP from 1990 to 2021.
    
\begin{figure}[htp]
  \centering
  \includegraphics[scale=0.5]{gdp_01.pdf}
  \caption{Quarterly seasonally adjusted real US GDP from 1990 to 2021}
  \label{Fig_01}
\end{figure}

% End of actual document
\end{document}

Help

Additional help on figures can be found here.


Back to Start of LaTex


Math Expressions

Include math expressions

Math expressions can be added inline using the notation $$, e.g., $Y$.

Equations can be added using an equation environment, e.g., the equation or align environment, of the package amsmath.


% Define class, e.g., article or beamer,...
\documentclass{article}

% "Preamble area" to load packages and specify formats and styles
\usepackage{hyperref} % use hyperref package for links
\usepackage{amsmath} % use amsmath package
\newcommand\numberthis{\addtocounter{equation}{1}\tag{\theequation}} % new command for labeling multiple equations
% See: https://tex.stackexchange.com/questions/42726/align-but-show-one-equation-number-at-the-end

% Start of actual document
\begin{document}

In general, \LaTeX{} is great at typesetting mathematics. Let $X_1, X_2, \ldots, X_n$ be a sequence of independent and identically distributed random variables with $\text{E}[X_i] = \mu$ and $\text{Var}[X_i] = \sigma^2 < \infty$, and let
\[S_n = \frac{X_1 + X_2 + \cdots + X_n}{n}
      = \frac{1}{n}\sum_{i}^{n} X_i\]
denote their mean. Then as $n$ approaches infinity, the random variables $\sqrt{n}(S_n - \mu)$ converge in distribution to a normal $\mathcal{N}(0, \sigma^2)$.

Alternative math environments can be used. For example, this is an equation without label using the \texttt{equation} environment:

\begin{equation*}
  Y_{t} = \phi Y_{t-1} + w_{t} 
\end{equation*}

This is an equation with a label using the \texttt{equation} environment:

\begin{equation}\label{Equ_01}
  Y_{t} = \phi Y_{t-1} + w_{t} 
\end{equation}

Equation \ref{Equ_01} shows a first-order stochastic difference equation.

These are multiple equations alinged without a label using the \texttt{align} environment:

\begin{align*}
  Y_{t} &= \phi^{Y} Y_{t-1} + w^{Y}_{t} \\
  X_{t} &= \phi^{X} X_{t-1} + w^{x}_{t}
\end{align*}

These are multiple equations alinged with a label using the new command \texttt{numberthis} defined in the preamble and the \texttt{align} environment:

\begin{align*}
  Y_{t} &= \phi^{Y} Y_{t-1} + w^{Y}_{t} \numberthis \label{Equ_02} \\
  X_{t} &= \phi^{X} X_{t-1} + w^{x}_{t} \numberthis \label{Equ_03}
\end{align*}

Equation \ref{Equ_02} and \ref{Equ_03} show two first-order stochastic difference equations.

% End of actual document
\end{document}

Help

Additional help on mathematical expressions can be found here.


Back to Start of LaTex


Citations

Include citations

Citations can be added using the biblatex package.

To insert citations one has to enter a bibliography list first. This can be done in the preamble of the .tex file using the filecontents environment (see below) or in a separate .bib file.

Compilation

  • The most widely used file format to enter a bibliography is a .bib file.
  • The bibliography file .bib-file is processed by BibTeX.
  • Depending on your TeX-editor you have to follow the steps:
    1. Compile the .tex-file using the usual PDFLaTeX-engine.
    2. Compile the generated .aux-file using the BibTeX-engine.
      • TeXstudio: “Tools”“Commands”
    3. Compile the .tex-file again using the usual PDFLaTeX-engine.

Generation/management .bib entries

There are several ways how to generate .bib entries:

  • Use the citation information provided by the journal (first choice).
  • Use the citation information provided by Google Scholar (second choice).
  • Other resources, e.g., zoterobib.

There are several software programs to manage .bib entries:


% Define class, e.g., article or beamer,...
\documentclass{article}

% "Preamble area" to load packages and specify formats and styles
\usepackage{hyperref} % use hyperref package for links

\usepackage[backend=bibtex,natbib=true,style=authoryear]{biblatex} % Use biblatex packagge
% Use the bibtex as engin (vs. biber)
% Use natbib compatible mode
% Use authoryear style

% Usually provided in an external .bib file
\begin{filecontents}{bibliography.bib} 
    
@Book{StockWatson2020,
    author    = {Stock, James H. and Watson, Mark W.},
    publisher = {Pearson Education},
    title     = {Introduction to Econometrics},
    year      = {2020},
}

@Book{Wooldridge2019,
    author    = {Wooldridge, Jeffrey M.},
    publisher = {Cengage Learning},
    title     = {Introductory Econometrics: A Modern Approach},
    year      = {2019},
}

@Article{NeweyWest1986,
    author={Newey, Whitney K and West, Kenneth D},
    title={A simple, positive semi-definite, heteroskedasticity and autocorrelationconsistent covariance matrix},
    journal={Econometrica},
    year={1986},
    volume={55},
    number={3},
    pages={703-708}
}   

\end{filecontents}

% Add bib source
\addbibresource{bibliography.bib}

% Start of actual document
\begin{document}
    
% If author appear in ongoing text
\citet{Wooldridge2019} and \citet{StockWatson2020} is the textbook for the lecture in "Introductory Econometrics" and "Econometric Methods in Business and Economics", respectively. 
% If author appears not in ongoing text (2n citation)
\citet{NeweyWest1986} showed that under fairly general assumptions the HAC estimator is a consistent estimator of the variance of $\widehat{\beta}_{1}$. \citep[see][p. 623]{StockWatson2020}
    
% Add biblography list
\printbibliography
    
% End of actual document    
\end{document}

Help

Additional help on biblatex can be found on Overleaf and on CTAN.


Back to Start of LaTex