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 Sharelatex.

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

1. Open your browser and go to www.sharelatex.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 "Create First 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 Sharelatex 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 ShareLatex 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 ShareLatex

\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.

 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 and . For example: Add$a$squared and$b$squared to get$c$squared. Or using a more mathematical approach: $$a^2+b^2 = c^2$$. 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, y_0 = x^{3/4} 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 $$and$$  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 sharelatex document named figures (upper left above main.tex).

Download a pdf image (for example download an example figure from here). 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

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 ShareLaTeX 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 ShareLaTeX 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.

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.

overleaf

authorea

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!

Data Management Specialist

Clara Llebot
Contact:
4th floor Valley Library
541 737 1192