Basic LaTeX

Learning Objectives:

  • What is LaTeX?
  • Hello World
  • Title Page
  • Sectioning/Appendix/List
  • Reference
  • Font/Page Layout/Line Spacing
  • Mathematics
  • Table/Graphics/Figures
  • Page Margin
  • Line spacing
  • Font Size

What is LaTeX?

LaTeX is a program that writes document using programming. It is What you see is what you mean (WYSIWYM). Hence, you should not need to bother with formatting when you create the content. You write a document by focusing on the content instead of how it looks. The appearance would be taken care by the software.

The program will do it for you or you tell how it should be done. It is standard for academic publishing, and it is also very easy to include mathematics, graphics and tables with professional look.

Basic setup

Hello World

Since we are producing a document, we start to tell the program so by using the command \documentclass{article}. We need to tell what document we are producing. For our purpose, we are focusing on simple documents so that article would suffice. Other frequently used options for documentclass are book and beamer for producing books and presentation slides.

Then we need to tell which section should be included in the output file by using \begin{document} and \end{document}.

Finally, type the words “Hello World!” between the \begin{document} and \end{document}. Whatever text, pictures, or tables is included in between the 2 commands will appear on the document itself, and you can see this on the right-hand side.

\documentclass{article}
\begin{document}
Hello World!
\end{document}

If you compile the file, you should get the following pdf output:

Front Matter

Usually a document would start with a title. We just need to tell the program by \title{}.

Similarly, we will usually have author after title. We only need to say \author{}

Moreover, we often put a time stamp to the document. To do so, we use \date{}. Often we use \today to represent the date to set this up.

Finally, we need to tell the program where to put these information by using \maketitle.

An example is as follows:

\documentclass{article}
\title{My important paper}
\author{VVIP}
\date{\today}
\begin{document}
\maketitle
Hello World!
\end{document}

Sectioning

Next, we move on to sections and subsections. This is important if you wish to divide your text into nice, separate sections for readability.

When you include sections by using \section{}, LaTex automatically numbers each section for you. You might have seen this in academic papers, this is why people use LaTex when writing their papers. Subsections will take on that particular section’s number, and then include a second number behind to indicate that it is a subsection. We use \subsection{} here. You can also use \subsubsection{} if you wish.

You code will look like this:

\documentclass{article}
\title{My important paper}
\author{VVIP}
\date{\today}
\begin{document}
\maketitle
Hello World!

\section{Introduction}
This is my best paper
\section{Model}
We will have a lot of math.
\subsection{Basic Model}
Math here is simplier.
\subsection{Full Model}
Math here is crazy.
\section{Conclusion}
My paper is great!
\end{document}

Appendix

Appendix is another section that does not following numeric numbering (but alphabetical ordering). We just need to say \appendix where the appendix starts.

\documentclass{article}
\title{My important paper}
\author{VVIP}
\date{\today}
\begin{document}
\maketitle
Hello World!

\section{Introduction}
This is my best paper
\section{Model}
We will have a lot of math.
...
\section{Conclusion}
My paper is great!

\appendix
\section{Data Description}
Here is the appendix.
\end{document}

Reference

Reference is very easy to manage under latex. Use \cite{key} to cite paper in the bibligrpahy section enclosed by \begin{thebibliography} and \end{thebibliography}:

\cite{key1} says lalala 
\cite{key2} says blahblahblah
\begin{thebibliography}{99}
\bibitem{key1} Me, My paper, Trash Bin, P1-100.
\bibitem{key2} You, My paper 2, Trash Bin, P2-200.
\end{thebibliography}

The argument to \begin{thebibliography}{} is used for setting the hanging indentation of the bibliography items. If you use numeric labels, then the argument should be a single digit (9 is commonly used) if there are less than ten items; two digits (commonly 99) if there are from 10 to 99 items and so on.

To create items in the bibliography, we use \bibitem{}, where the name in the brackets is what you use when you cite in the main text. \bibitem{key} Name, Year, Journal, Page Number.

The \cite{key} command will automatically extract information from the bibliography where
key is the name defined in the thebibliography.

If there is no such information in the bibliography, the citations will simply show a question mark ?.

Lists

There are three types of list:

  • Itemize is unnumbered list: each item is denoted by a bullet or a dot. Commonly used for unordered items
  • Enumerate is numbered list or ordered list: each item is denoted by number or letter. Commonly used for ordered items
  • Description is descriptive list: each item is denoted by customized description.

Unordered List

Undered lists are enclosed by \begin{itemize} and \end{itemize} and we use \item to tell it is an item. A typical example is:

\begin{itemize}
  \item I have an apple.
  \item You have an orange.
\end{itemize}

Ordered List

Likewise, the \begin{enumerate} and \end{enumerate} also works the same way, and we also use \item to create items. The only difference is that enumerate uses numbers, and itemize uses bullet points.

\begin{enumerate}
  \item I have an apple.
  \item You have an orange.
\end{enumerate}

Deescription List

Likewise, the \begin{decription} and \end{description} also works the same way, and we also use \item[name] to create items. The only difference is that we have to tell the name for each item.

\begin{description}
  \item[First] I have an apple.
  \item[Second] You have an apple.
  \item[Third] We have two. 

Nested List

Nested lists can be easily created by nesting one list over the other. The below show how one numbered list put inside another numbered list. One can also put an unnumbered list insider another unnumbered list. Moreover, one can also cross-over.

First, we nest an unordered list inside another unordered list:

\begin{itemize}
  \item I have an apple.
  \item You have an orange.
  \begin{itemize}
    \item I have an apple.
    \item You have an orange.
  \end{itemize}
\end{itemize}

Second, we nest an ordered list inside another ordered list:

\begin{enumerate}
  \item I have an apple.
  \item You have an orange.
  \begin{enumerate}
    \item I have an apple.
    \item You have an orange.
  \end{enumerate}
\end{enumerate}

Or even mix them together:

\begin{enumerate}
  \item I have an apple.
  \item You have an orange.
  \begin{itemize}
    \item I have an apple.
    \item You have an orange.
  \end{itemize}
\end{enumerate}

Tables

There are two types of table: (1) tabular and (2) table. Tabular environment is to display information in table form while table environment is to allow tabular to float.

Tablular

Tables are created using the \begin{tabular}{} and \end{tabular} format. We can specify what type of table we want, and LaTex will create it for us.

Input Meaning
l left-justified column
c centered column
r right-justified column

An example is

\begin{tabular}{l c r}
   1 & 2 & 3 \\ \hline
   40 & 50 & 60 \\
   700 & 800 & 900 \\
\end{tabular}

In the example above, we have used “l c r”, so LaTex creates a table with 3 columns, with the corresponding directional justifications.

Table

LaTeX can help you to determine the location of the table automatically. We start with \begin{table} and end with \end{table}

There are several choices for placement options after \begin{table}:

Specifier Location
h Place the float here
t Position at the top of the page.
b Position at the bottom of the page.

Graphics

Similar to table, there are two ways to display graphics: inline or floating.

Includegraphics

To include graphics files (e.g., jpg and png) in the document, we need to make use of the graphicx package, which is done by using \usepackage{graphicx}. Note that package has to put before \begin{document}.

To include a graphic file, we use the command \includegraphics{filename}. Note that the file has to be in the same file as the tex file for this code to work.

Figure

To float the graphics, we start with using \begin{figure}[placement option] and ends with \end{figure}. You also need to write down the caption. You may use \centering to center the graphics.

\begin{figure}[htp]
\centering
\includegraphics[width=\textwidth]{picture.jpg}
\caption{I am a figure.}
\end{figure}

Mathematics

Inline Mathmatics

Mathematics is one of the main reasons to use LaTeX over other word processors. To let the program know that you are working on math, enclose your expression with dollar sign $:

$ 3^2 + 4^2 = 5^2$

Why dollar sign? Typesetting with math was expensive in the old days.

Alternatively, we can enclose our expression by \( and \). The latter way of writing improves readability if we have a complicated mathematical expression.

Greek Letters

We can write symbols by using symbol name, for example \alpha for $\alpha$, \beta for $\beta$ etc.

Displayed equation

If we want a display equation, simply use double dollar sign $$:

$$ 3^2 + 4^2 = 5^2$$

Why dobule dollar sign? Typesetting with displayed math was even expensive in the old days.

Alternatively, we can enclose our expression by \[ and \]. The latter way of writing improves readability if we have a complicated mathematical expression.

Numbered equation

If you want the equation to be numbered, enclose it with \begin{equation} and \end{equation}:

\begin{equation}
3^2 + 4^2 = 5^2
\end{equation}

If you want it to be referenced, then add an \label{key} and use \ref{key} later.

\begin{equation}\label{NiceEq}
3^2 + 4^2 = 5^2
\end{equation}

I like Eq (\ref{NiceEq}) very much. 

Note that the bracket has to be manually added.

Multi-line equation

To use multi-line equation, we enclose the set of equations by \begin{align} and \end{align}. For multi-line equations, we need to separate each line by \\ and specify where equations should align by &:

\begin{align}
x +y  &= 1 \\
x - y &= 2  
\end{align}

Theorem

To write a formal statement, it is convenient to use amsthm package by \usepacakge{amsthm}:

\usepackage{amsthm}
\newtheorem{theorem}{Theorem}

\begin{theorem}\label{NiceThm}
One plus one is two! $$1+1=2.$$
\end{theorem}
I like Theorem \ref{NiceThm} very much. 

Page Layout

Page Margin

The geometry package allows you to change the margins of the page, if you wish to.

\usepackage{geometry}

To change the individual sides’ margins, simply use \geometry{margin=1.1in}. Take note that this should be put before the \begin{document} since it is for the whole document. For example, we have

\usepackage{geometry}
\geometry{maring=1.1in}

To specify margins on four sides, we use \geometry{left=2.5in, right=2.5in, top=2.5in, bottom=2.5in}

\usepackage{geometry}
\geometry{left=2.5in, right=2.5in, top=2.5in, bottom=2.5in}

Vertical spacing

Command Meaning
\ linebreak
\newpage starts a new page
\smallskip create small vertical space
\medskip create medium vertical space
\bigskip create big vertical space
\vspace{4in} create vertical space of 4 inches

Horizontal spacing

Command Meaning
\noindent suppresses paragraph indentatio
\enskip horizontal space of half of the size of letter M
\quad horizontal space of one of the size of letter M
\qquad horizontal space of two of the size of letter M
\hspace{2cm} horizontal space 2cm
\hfill horizontal space to make the line right-aligned

Paragraph Layout

Line spacing

The setspace package allows you to edit the line spacing between each line for overall text, similar to Word’s function. Options include \singlespacing, \onehalfspacing, \doublespacing.

\usepackage{setspace}
\singlespacing
\onehalfspacing
\doublespacing

Text Alignment

Default of text is justified.

  • For centered text, enclosed \begin{center} and \end{center}
  • For right-aligned text, enclosed \begin{flushright} and \end{flushright}
  • For left-aligned text, enclosed \begin{flushleft} and \end{flushleft}
\begin{center}
	I am centered text.
\end{center}
\begin{flushright}
	I am right-aligned text.
\end{flushright} 
\begin{flushleft}
	I am left-aligned text.
\end{flushleft}

Font Layout

Global font Size

To change font size of the whole document, we define it the premable:

\documentclass[12pt]{article}

As shown above, we can change the font size used throughout the paper by changing the number here. In this example, font size is 12, and we can change it by just typing another number to replace the 12. If you want other size, you can choose 10 or 11.

\documentclass[10pt]{article}
\documentclass[11pt]{article}

Local font size

To change the font size only for some part of the text, we use some relative size commands (from small to big):

\Huge
\LARGE
\Large
\large
\normalsize
\small
\footnotesize
\scriptsize
\tiny

Here is the example:

\begin{document}
You know what? 
I am an honor student of economics of NUS. 
Our school is the {\Huge  best} in {\tiny Asia}.
What does that mean? 
I am {\large Best} of {\LARGE best}.
Really? 
{\Huge Yes}.\footnote{Seriously?}
\end{document}

Technically, it is the mapping of the relative size command:

Command 10pt 11pt 12pt
\tiny 5pt 6pt 6pt
\scriptsize 7pt 8pt 8pt
\footnotesize 8pt 9pt 10pt
\small 9pt 10pt 11pt
\normalsize 10pt 11pt 12pt
\large 12pt 12pt 14pt
\Large 14pt 14pt 17pt
\LARGE 17pt 17pt 20pt
\huge 20pt 20pt 25pt
\Huge 25pt 25pt 25pt

Sometimes, we want to exactly match directly. We can use \fontsize{size}{leading}\selectfont. For example, \fontsize{10}{12}\selectfont means 10pt type with 12pt leading.

Font color

To change color, use package color and then enclosed the passage with brace.

\usepackage{color}
{\color{red} abc is 123.}

Basic font transformation

For most daily usages, there are six commands (italic, bold, small cap, slant font, typewriter):

\textit{italic} 
\textbf{boldface}
\textsc{small cap}
\textsl{slant font}
\textsf{Sans Serif}
\texttt{typewriter}

For more details, see below discussion on font family, font series, and font shape.

Font emphasis

Font emaphsis involves underline, italic and upper case:

Command Emphasis
\underline{…} underline
\emph{…} Italic
\uppercase{…} all capital letters

Highlighting and strikeout are not included in standard package. To use it, we need to use the packages color and soul:

\usepackage{color,soul}
\hl{I am highlighted}
\sout{haha}

Font Family

Under computer modern font family, roman (or serif typeface) font is used. We can use sans serif and typewriter font family.

Command Switch font family
\texttt{…} {\ttfamily …} typewriter
\textsf{…} {\sffamily …} san serif font
\textrm{…} {\rmfamily…} roman font

Font Series

Font series mean how dark the pen and how width of the pen.

selectfont Command Switch font series
\fontseries{m} medium
\fontseries{b} \textbf{…} {\bfseries …} bold
\fontseries{bx} bold extended
\fontseries{sb} semi-bold
\fontseries{c} condensed

Font shape

Font shape is the form of letter:

Selectfont Command Switch Shape
\fontshape{it} \textit{…} {\itshape …} italics
\fontshape{sl} \textsl{…} {\textsl …} slant
\fontshape{sc} \textrm{…} {\scshape …} small capital
\fontshape{ui} upright italic
\fontshape{ol} outline
Previous
Next