Skip to Main Content

Introduction to Latex Workshop

This workshop is designed for people with very little previous knowledge of LaTeX. It assumes that each student will have a computer and will code at the same time as the instructor does. The design of the workshop is inspired in Software Carpentry method

Getting started

You will need a LaTeX editor of some sort. For this workshop we will use Overleaf.

For your work with LaTeX outside of this workshop there are multiple options. There are several online editors available, including Overleaf or Authorea (Sharelatex just merged with Overleaf). You can also install LaTeX locally in your computer.

To sign up in Overleaf:

  1. Open your browser and go to www.overleaf.com
  2. If you don't already have an account, click on Register (top right of the page) and provide your e-mail and a password. 
  3. If you already have an account click on Log In and enter your e-mail and password.
  4. Click on the green button "New Project". 
  5. Choose Blank Project and give a name to your project

Document basic structure

LaTeX is pronounced "Lay-tech" or "Lah-tech". If you refer to LaTeX in an ASCII environment, you type LaTeX. 

The idea behind LaTeX is that the LaTeX environment does the work of a designer. The author needs to provide the text, and some additional information that describes the logical structure of the work. This information is given as LaTeX commands. The text that the author types, usually in a text editor, is different than the final output. The final output can be previewed on the screen after processing the file with LaTeX. This separation between the content and the formatting may be frustrating at the beginning, but once you get used to it, it allows you to focus exclusively on the content, while LaTeX takes care of the formatting. 

In Overleaf you can see your commands on the left side of the page, and the output on the right pane. Every time something is changed in the left pane, the blue Recompile button needs to be clicked, and the new document will appear on the right side.

For example, change the Author of the document from the automated \author{your.email } to \author{Your Name} 

The structure of a LaTeX command is 

\command[optional parameter]{parameter}

For example:

This text \textbf{is in bold} and this \textit{text is in italics}.

You can also include comments in your text file by preceeding with the % sign:

% This is a comment

If you are working with Overleaf you will see that a basic structure appeared on the text editor when we started. Copy it to your editor if you are not working with Overleaf

\documentclass{article}
\usepackage[utf8]{inputenc}

\title{Workshop}
\author{Your Name}
\date{September 2016}

\begin{document}

\maketitle

\section{Introduction}

% This is a comment
This text \textbf{is in bold} and this \textit{text is in italics}.

\end{document}

The file structure begins with \documentclass{...}. There are several document classes that can be used with LaTeX. We will use the article class. 

Document Classes (https://en.wikibooks.org/wiki/LaTeX/Document_Structure)
article For articles in scientific journals, presentations, short reports, program documentation, invitations,...
iEEEtran For articles with the IEEE Transactions format.
proc A class for proceedings based on the article class.
report For longer reports containing several chapters, small books, thesis,...
book For real books.
slides For slides. The class uses big sans serif letters.
memoir For changing sensibly the output of the document. It is based on the book class, but you can create any kind of document with it.
letter For writing letters.
beamer For writing presentations.

The documentclass command accepts optional arguments. For example, we could specify the size of the paper:

\documentclass[a4paper]{article}

Exercise: 

Try removing a command and recompile to see what happens. For example, try to remove \maketitle.

Exercise:

Write a couple of paragraphs of text and compile

 

There are some areas where basic LaTeX cannot solve your problem. To enhance the capabilities of LaTeX you can load packages. These are activated with the command:

\usepackage[options]{package}

 

Basic commands

A usual way to learn LaTeX or to remember how to do things, is to see how others or yourself have done something in an other document. There is a huge community of helpful LaTeX users. Googling what you want to do will usually turn up something helpful. If a general search does not give you good results you can search specific communities. Stackoverflow is a good one. 

Exercise:

Add one or more sections to your text by looking at how the Introduction section was added.

You can organize your document with \section{} \subsection{} \subsubsection{} \paragraph{} \subparagraph{}.

LaTeX can be used to do some work easier. For example, a table of contents can be added by writing the command \tableofcontents where you want the table of contents to be placed. 

Some basic formatting commands: \textbf{} for boldface the text. \textit{} or \emph{} to italicise text.

Exercise:

Try out some of these commands, and see what they do:

  • \LaTeX 
  • \ldots and compare it to  ...

Accents and special characters: include accents and special characters by preceding them with a backslash like in these examples:

  • Se\~nora.
  • Per qu\`e el caf\`e quan \'es recent est\`a m\'es bo?
  • H\^otel.
  • Els bons ve\"ins costen de trobar.

If you will be writing in languages other than English there are packages that can help you with that. For many languages the package babel, by Johannes Braams, is what you will need, but we will not cover it in this workshop. 

Equations

To write (advanced) mathematics you should use the AMS-LaTeX bundle. To load it type \usepackage{amsmath} in the preamble of your document. 

There are several ways to use math mode. 

  1. Inline math mode: by placing a mathematical equation inside a pair of $. For example: Add $a$ squared and $b$ squared to get $c$ squared. Or using a more mathematical approach: $a^2+b^2 = c^2$.
  2. Display the equations apart from the rest of the paragraph by enclosing them between \begin{equation} and \end{equation}. For example: Add $a$ squared and $b$ squared to get $c$ squared. Or using a more mathematical approach: \begin{equation} a^2+b^2 = c^2 \end{equation}.

In math mode lowercase Greek letters are entered as $\alpha$, $\beta$, $gamma$,  $\delta$ etc. and uppercase letters as $\Gamma$, $\Delta$, ...

Superscripts and subscripts can be specified using the ^ and _ characters. They only act on the next character, but you can group characters with { }.  For example, 

\begin{equation}

y_0 =  x^{3/4}

\end{equation}

A couple of commonly used commands for math mode are \sqrt{} for square roots and \frac{topline}{bottomline} for fractions.

Exercise

Try out a simple fraction.

Try out a simple equation using a square root 

Exercise

Enter the following equation in between $ and in between \begin{equation} and \end{equation}  and compare how it looks

\int_0^\infty e^{-x^2} dx=\frac{\sqrt{\pi}}{2}

 

Figures

To include images in LaTeX you need to include a new package in the preamble of your document: \usepackage{graphicx}. You will also need to add the path to your figures folder with \graphicspath{ {figures/} }.

Create a new folder in your Overleaf document named figures (upper left above main.tex). 

Download a pdf image (for example download an example figure from this image on Google drive). Add the image to your document by using the upload button (upper left above your figures folder). 

To include a figure you just need to add \includegraphics{space_rocket.pdf}

It is useful, though, to include the image in a figure frame by creating a figure environment:

\begin{figure}

\includegraphics{space_rocket.pdf}

\end{figure}

Latex will try to place the figure where it thinks is more appropriate. You can change this behavior by suggesting it to be in a particular location 

\begin{figure}[h]

The size of the figure can be modified by specifying the width or height of the figure.

\includegraphics[width=150pt]{space_rocket.pdf}

You can also use \textwidth and \paperwidth to specify relative sizes. 

\includegraphics[width=0.5\paperwidth]{space_rocket.pdf}

We can add a caption writing \caption{Caption} under the includegraphics command. We can also center the figure adding \centering

Exercise:

Play with the size, caption and centering of the figure 

Cross References

LaTeX allows us to reference sections, figures, tables, formulas and, in general, anything that is numbered. To do so, we use the command \label{marker} to mark the object that we want to refer to. The command \ref{marker} to reference the object that was previously marked. And the command \pageref{marker} to print the page number where the object is. marker is a word of your choice used to identify the object. 

For example, if we wanted to refer to the section "Introduction" that we created previously, we can mark the introduction section by typing:

\section{Introduction} \label{sec:intro}

To refer to the section, we will type:

As we discussed in section \ref{sec:intro}, in page \pageref{sec:intro}, this subject has already been explored by other researchers. 

Similarly, if we wanted to reference a figure, we would type:

\begin{figure}

  \includegraphics{space_rocket.pdf}

  \caption{Figure of a rocket} \label{fig:rocket}

\end{figure}

And later on, 

As we can see in Figure \ref{fig:rocket}, one of the illustrations is a rocket. 

There could be many references in a document, and become confusing. To avoid this problem it is common practice to use a few letters to describe what is being referenced. 

Commonly used describers for reference markers. For more information see LaTeX/Labels and Cross-referencing
sec section
subsec subsection
fig figure
tab table
eq equation

 

BibTex

The best way of creating a bibliography in LaTeX is to use a feature called BibTeX. This involves creating a database of sources in a new file. 

Create a new file in your Overleaf workspace by clicking New File (top left corner of the page) and name your file with the extension .bib .  To include content in your file you have to include all the details of the source you want to cite. There are many entry types you can add, like article, book, mastersthesis, techreport, etc. A list of the entry types can be viewed, for example, in the BibTeX wikipedia page. Each source type has required fields and optional fields. For this example we will create an article entry:

@article{ArticleLabel,

author = " Namea Surnamea and Nameb Surnameb",

title = " {Title of the Example Article}",

journal = " Journal Name",

volume = " 1",

number = " 1",

pages = " 12--13"

}

All entries start with an @, followed by a { and a label that you will use to refer to the entry. Try to make the label informative. The authors are separated with the word and, and  can be introduced as Name Surname, or as Surname, Name. The curly brakets are used to preserve the capital letters. Fields that are numbers do not need to be in between commas, but they can. 

To have the bibliography appear in the text we need to add two commands at the bottom of the page: the bibliography style and the bibliography file. 

\bibliographystyle{plain}

\bibliography{bibliographyexample.bib} 

To cite a reference, add \cite{ArticleLabel} in the text, and recompile (if you are not using Overleaf you will need to run LaTeX, then bibTeX, and then LaTeX again).  

Exercise:

Go to www.sciencedirect.com and search for an article. Get the bibliographic reference by clicking Export (top left of the page, above the Elsevier logo) and choosing the bibtex option. Copy and paste that entry in your bibliography file. Cite this file in your text. 

The natbib package will give you more options to format your bibliography. When you publish an article in latex in a particular journal, many times the journal will provide you with a bibliography style file that you can use. In the preamble, add:

\usepackage[]{natbib}

Make sure you change the bibliographystyle,

\bibliographystyle{plainnat}

Now you can use the commands \citep{} and \citet{} for citations in between parenthesis or outside of parenthesis. 

Creating LaTeX bibliography files

Manually

You can always create your bibliography file manually. Many journals and databases will help you export the reference. 

For example, Google Scholar will show you a BibTex option if you click on the quotes button under a reference. 

LaTeX and Zotero

The University of Melbourne has a good guide on how to manage your bibliography with Zotero and write your articles in LaTeX. Check it out here

LaTeX and Mendeley

The Mendeley blog has detailed information about how to use LaTeX and Mendeley.

LaTeX and EndNote

The University of Melbourne has a good guide on how to manage your bibliography with EndNote and write your articles in LaTeX. You can find it here

Resources

Tutorials

Most of the exercises and instructions used in this guide have been obtained from these resources:

The not so short introduction to LaTeX, Tobias Oeticker, Huber Partl, Irene Hyna, Elisabeth Schlegl.

A short Introduction to LaTeX, lesson from Software Carpentry.

Video guides of Sharelatex: Creating your first LaTeX document, Paragraphs and Sections in LaTeX, Mathematics in LaTeX, Images in LaTeXBibliographies and natbib, Tables and Matrices in LaTeX, Longer Documents in LaTeX.

Online tools

overleaf

authorea

Templates

Kyle Niemeyer created this OSU Thesis template in LaTeX, available in GitHub https://github.com/Niemeyer-Research-Group/OSU-thesis

Where to get help

Stackoverflow

You can ask me! Contact at the bottom of the page. Just remember that I may not be able to help if you have advanced LaTeX questions.

Interested in forming a group? Let me know!