Introduction

Overview:

  • Teaching: 15 min
  • Exercises: 5 min

Objectives

  • Write your first document in $\LaTeX$

What is $\LaTeX$?

LaTeX is a typesetting system, specifically designed for scientific report writing. It is freely available and has many editors, both downloadable and online, which come ready-to-use and even include document templates. There is an active community and several forum sites from which you can get help online; as well as a large development community creating additional packages which add to the functionality of $\LaTeX$ itself.

How does it work?

Unlike other document creation and editing programs, like MS Word or iOS Pages, $\LaTeX$ (or more specifically TeX) doesn't let you use a Graphical User Interface to manipulate your document. That means that you can't "drag-and-drop" text and images around a page, instead what you do is write a plain text file (saved as a .tex file) which contains commands for $\LaTeX$ to read to assemble your document!

Why $\LaTeX$?

Losing the ability to interact graphically with your document is a scary prospect for some people, and might sound like you're loosing a lot of functionality - but as you'll see over the course of these lessons working from a plain text file and having $\LaTeX$ interpret it provides us with much more power and control over our document. One of the main advantages of $\LaTeX$ is the way it seemlessly combines text (prose), images, equations, tables and the like; all of which are generated through commands and can have their properties controlled directly and precisely. Contrast this to a problem that I'm sure we have all experienced before: Moving an Image in MS Word

Getting started with Overleaf

  • Overleaf is a website for writing documents in $\LaTeX$
  • It compiles your $\LaTeX$ automatically to show you the results
  • As we go through this introduction, try out examples by typing them into the example document on Overleaf https://www.overleaf.com/

Let's firstly create a new project for this session. To do so, you should register an account in order to use the online $\LaTeX$ service. You can also use your own $\LaTeX$ editor but things may look different to any screenshots in the lessons.

Local Editors

For those who have installed a local editor like TeXMaker or MikTeX as recommended in the schedule, if you open the editing application and hit "create new", you should be greeted with a similar interface to that which is described for Overleaf below.

You may have a few additional features, and a few features missing - TeXMaker for example doesn't show all your files in the current folder, but does have several more options and toolbars when compared to Overleaf. For now these are largely irrelevant differences; you should have an editing/input pane and viewing pane like Overleaf, and a fairly obvious "compile and view document" button. Working locally, you'll need to hit the "compile and view document" button every time you change your source code and want to update the output.

As you use $\LaTeX$ more often you should explore the additional features of local editors to find one that suits your writing style, but for the purposes of these lessons we will only be exploring writing content of a document, rather than document presentation.

Overleaf display

You should see an interface like the following. There are three coloums: the left-end coloum shows all the files/folder under your current project; the middle column is where you type your source $\LaTeX$ code; the column on the right shows you the output of your document. As you go through this introcution course, you can type/copy the provided code to the input pane (the middle coloumn) and click the Recompile button on the top of the output pane (the right coloum) to see the result.

Create a new project

Now empty your input pane and place in the following code instead. This is an example of a (really) small $\LaTeX$ document; commonly called a "minimum working example".

\documentclass{article}

%you can type some commands before the document begins here. This section is called the preamble and we will talk about what it can be used for later.

\begin{document}

    %all the content for your document goes in here
    Hello World!

\end{document}

You should end up with a document that contains the phrase "Hello world!".

$\LaTeX$ Syntax

You should notice a few syntax rules for the source code in this simple example:

  • A command starts with a backslash, \, followed by it's name and arguments. Command names are case-sensitive.
  • Every document starts with a \documentclass command, which tells $\LaTeX$ what kind of document you want to write. There are several options available that you can lookup.
  • Commands can be given arguments (or parameters) in curly braces {}. Some commands require a certain number of arguments, whilst other commands can take optional arguments. In our example , the argument article tells $\LaTeX$ the kind of document we are creating.
  • A percent sign % starts a comment, meaning that $\LaTeX$ will ignore the rest of the line. Use comments as reminders when writing your source code, like for summarising the previous paragraph so you can find content faster when you come back to your document.

Typesetting Text

You can start writing your document by typing text between \begin{document} ... \end{document}. This is also where you will include commands for placing images into your document, breaking the document into sections, creating title pages and contents, amongst other things.

$\LaTeX$ interpretation

Try to type the following text into your document and see what comes out:

Fish and $\, \, \, \, \, \,$ chips!

What do you notice?

Solution

Typesetting Text: caveats

Some common characters have special meanings in $\LaTeX$:

  • % percent sign (for comments, as we have seen)
  • # hash sign (technical use in user-defined commands)
  • & ampersand (used to align mathematical equations and tables)
  • $ dollar sign (used to begin mathematical equations)

If you want these to appear in the output you need to put a backslash before them as shown below, otherwise you'll get an error and your document won't be created.

In [3]:
%%pdflatex
\$ %produces a dollar sign
\% %produces a percent sign and doesn't start a comment!
\& %produces an ampersand
\# %produces a hash sign
Out[3]:

Environments

Environments are used to format blocks of text in a $\LaTeX$ documents. Environments are delimited by an opening tag \begin and a closing tag \end. Everything inside those tags will be formatted in a special manner depending on the type of the environment.

For example, the itemize and enumerate environments generate lists:

In [4]:
%%pdflatex
This first environment will create a bullet-point list:
\begin{itemize} %for bulleted lists
    \item Biscuits
    \item Tea
\end{itemize}
This environment will create a numbered list:
\begin{enumerate} % for numbered lists 
    \item Biscuits
    \item Tea
\end{enumerate}
Out[4]:

Within an environment, additional commands are made available that won't work outside the environment. For example you can't use a \item command outside an enumerate or itemize environment; and most mathematical symbols cannot be used outside of math environments, which we will see in lesson 2.

Packages

All of the commands and environments we've used so far are built into $\LaTeX$. Packages are libraries of extra commands and environments for different purposes. We have to load each the packages we want to use with a \usepackage{} command in the preamble of the source file - this is the part of your .tex file before the \begin{document} command. In the preamble, you define the type of document you are writing and the language, load extra packages that you need, and set several parameters like the font and text size. You can also use style files to load some of the information that would go in a preamble.

For example, the amsmath package is a suppliment for the existing mathematical capabilities of $\LaTeX$. In the following code, we create an equation environment which is defined in the amsmath package. We will a more in-depth discussion of writing equations in the next lesson.

Your $\LaTeX$ file should look like this:

\documentclass{article}

\usepackage{amsmath} % preamble, in which we load the amsmath package. If you don't have this package installed on your machine, your editor will prompt you to download it. 

\begin{document}

\begin{equation}
  -u'' = -\omega^2 u
\end{equation}

\end{document}

And below is the document output.

In [5]:
%%pdflatex
\begin{equation}
    -u'' = -\omega^2 u
\end{equation}
Out[5]:

What's this %%latex and %%pdflatex?

You may notice the %%latex and %%pdflatex syntax that appears in these lessons above the $\LaTeX$ commands we want to execute. This is simply an artifact of how we have written these lessons, and you can ignore it. We write these lessons in jupyter notebooks, and these commands effectively load our preamble for us, including the amsmath package.

Other useful packages:

In addition to amsmath, there are some other packages that you might find yourself using quite often:

  • amsthm: for organising theorems, lemmas, proofs, and other things you often find in mathematics textbooks!
  • beamer: for creating presentations (and posters) - see the lesson on beamer for more information.
  • tikz: introduces tools to allow for drawing diagrams in a $\LaTeX$ document.
  • grahicx: provides a variety of options for the layout of images and other figures.
  • listings: provides a good range of options for including source code in a $\LaTeX$ document; if you want to share code you have written in say Python, R, or MATLAB.

For further refence, see the examples on Overleaf or TeXample. These websites have examples of (most of) these packages!

Your second document

Your first task is to typeset the following paragraph (in the quotation marks) in a $\LaTeX$ document:

" In March 2006, Congress raised that ceiling an additional $\$$0.79 trillion to $\$$8.97 trillion, which is approximately 68 $\%$ of the GDP. As of October 4, 2008, the Emergency Economic Stabilization Act of 2008 raised the current debt ceiling to $\$$11.3 trillion. "

Solution

Key Points:

  • You can install $\LaTeX$ on your local device, or use an online service.
  • We use \documentclass{} to tell $\LaTeX$ which kind of document we are writing.
  • We display special characters by putting a backslash before them.
  • Environments are used to format blocks of text.
  • The premable is the space in our source code before \begin{document}; we can load extra packages and set document-wide styles by placing commands there.