Creating a LATEX Document Tom Coleman [email protected]

This document is an introduction on how to start a document in LATEX. It explains how to get a LATEX editor for your computer, and how to create a simple document alongside the fundamental concepts of LATEX, such as document classes, packages, environments and reserved characters in LATEX.

Introduction to LATEX

LATEX is a system for preparing documents which is particularly suited to typesetting scientific and mathematical content. However, it may also be used for all other kinds of documents, from a simple list of notes to a CV. While basic tasks may initially seems unnecessarily complicated, you will soon appreciate the power and simplicity of LATEX when producing a complex document. LATEX takes care of all of your formatting issues, referencing, and document structure, leaving you to concentrate on what really matters; writing your document. This guide (and others, see below) will introduce you to the fundamentals of LATEX documents, and go on to explain how to create beautifully typeset documents and presentations with minimal stress and reformatting.

LATEX editors for your computer

IMPORTANT: In the SCI2RC2Y course on Introduction to LATEX, you will be using a UEA com- puter with both a TEX distribution and preinstalled. To find TEXmaker on your UEA machine, it is under “Start"→“Programs"→“Office Applications at UEA"→“TEXmaker". There is no need to worry about the rest of the information in this section unless you want to write and use LATEX at home.

Although you can use any text editor (such as Notepad) to write LATEX code, you need what is known as a TEX distribution to produce documents like this one. You can think of a TEX distribution as the engine that does all the work behind the scenes (this is sometimes known as the back-end). The most common TEX distributions in use are:

TEXLive (runs on everything including Unix/);

MiKTEX (optimised for Windows);

MacTEX (TEX Live for Mac).

These are all available online for no cost. TEX distributions tend to be quite large files contain- ing lots of packages and machinery; do not be surprised if your distribution takes a while to install.

Once you have your TEX distribution, it is recommended to also install a TEX editor; a program which you can type your code. You can think of a TEX editor as the control panel that directs the engine (the TEX distribution) to do what you want (this is sometimes called the front-end). Again, there are many alternatives to choose from, but here are some common ones:

TEXmaker (internal viewer, shortcut buttons, for all platforms);

1 TEXShop (quite minimalist, good on Mac);

Kile (similar to TEXmaker, available on Linux/Windows);

TEXnicCenter (similar to TEXmaker, Windows only);

TEXWorks (similar to TEXShop, for all platforms)

A good comparison can be found at the Wikipedia page on comparison of TEX editors. Most TEX editors (including all those listed above) are available online for no cost. Once you have both a TEX distribution and a TEX editor installed, you are ready to go!

Starting a document

Step by step

1) First, open TEXmaker or equivalent. 2) Next, type (or copy and paste) the following as shown into the text interface.

\documentclass[11pt]{article} \usepackage{graphicx} \begin{document} Hello world! \end{document}

3) Now save your document to any folder in your documents.

It is recommended to save your document to a dedicated folder for your LATEX work. This is because when you “build" your document a number of auxillary files are created; saving in a dedicated folder helps to reduce clutter in your documents. You may call your file anything you like, but make sure to end the file with .! There are many different classes of files associated with LATEX, and you must make sure you have the right extension for a document. It is important to save before proceeding to the next step.

4) Finally, go up to “Tools" and select “PdFLATEX" (the keyboard shortcut is F6 in TEXmaker).

This will save your document and then compile (or build) your LATEX file into a .pdf doc- ument, which will be created in the same folder as your .tex file. To view it in TeXmaker’s internal viewer, simply click on “Tools" again and select “View PDF" (or the keyboard short- cut is F7). If this does not work, go into the folder that your .tex file is in to find a .pdf document; this should have the same name as your .tex file. Open this through Adobe Reader.

5) Congratulations, you’ve made your first LaTeX document! Every time you make changes to your document, you will have to repeat Step 4 in order to see the changes in the .pdf document.

2 Explanations

The basic LATEX file created here is all very well and good, but it is quite empty. Before you think about adding content to your file, you need to consider the basic framework of a LATEX document. Understanding this is key to making sure that your document builds each time without any nasty errors.

Below are explanations of what each of the lines in your initial code mean, and what they do in terms of your document.

Commands

Writing commands in LATEX is easy; they always begin with a backslash, and look like \command. You may have noticed that there are different brackets for commands. Sometimes commands look like this:

\command[options]{argument}

Braces {...} contain the argument of the command; like the document class article. Brackets [...] contain the options of the command; like font size 11pt for the document class. Not every command in LATEX looks like this; as you will see throughout the course.

B When writing commands (or indeed, anything in LATEX), you must make sure any opened braces or brackets are closed! If there are any left open, your document will not compile.

Preamble and content

The LATEX document we have written is split into two distinct parts; everything above the \begin{document} command and everything between the \begin{document} and \end{document} commands. The divide is illustrated by the dotted line below.

\documentclass[11pt]{article} \usepackage{graphicx} ------\begin{document} Hello world! \end{document}

The section above the \begin{document} command is known as the preamble. Things that go in the preamble include the document class command, packages and other global settings for the document. The section between the \begin{document} and \end{document} commands is the content of the document; essentially, everything you want typeset when you compile. The terms preamble and content are referred to throughout; you should have a good grasp of what these are.

B Take care of where you must put certain commands. For instance, if you put a \usepackage{} command in the content, the compilation of your document will fail.

3 Document classes The first line of our document was this one:

\documentclass[11pt]{article}

Typically the first line in a .tex file, the document class is an essential line in compiling a LATEX document! The braces contain the class you want (see Table 1); the argument here specifies what type of document you are writing. The brackets contain the options of the class; these control things like universal font size, paper type and orientation and number of columns for text. This particular option displays font in 11pt; it is 10pt by default.

B If you do not include a \documentclass[]{}, line, LATEX will not build the document.

There are many different types of document classes. Some common document classes are summarized in Table 1 along with a few notes on usage. Please note that there are more than just these classes!

Document class Uses article article is the document class this is written in; it is for short docu- ments, usually of only a single chapter comprising of a few sections. report report is the bigger brother of article, used mainly for longer doc- uments with several chapters and sections. book book does what it says on the tin; it’s for books. letter For writing letters. beamer For presentations, beamer is an alternative to proprietary presenta- tion software. It is particularly useful for presenting mathematics to audiences. See the guide Creating a Beamer Presentation for more information.

Table 1: Some document classes

You can switch between article, report and book at will; this should help you decide on the layout for the document you want. Apart from the visual differences, there are other distinguish- ing elements between these; see the guide Layout, Formatting, Referencing.

You can write your own document class should you want to. For instance, if you choose to write a thesis in LATEX then you can write a thesis class to satisfy your thesis layout guidelines. They are written in .cls files.

Packages Packages provide many useful commands that help to customize the look and feel of your document. To utilise a package, enter the following into your preamble

\usepackage[options]{package} where the [options] denote the options and {package} denotes the package name. Most packages are included in your TEX distribution; however, there are many more available for download. If you need to do this, make sure that the package you download is contained in the same folder as your LATEX file. See Table 2 for a few of the most common packages and a few

4 Packages Uses amsmath, These are AMS (American Mathematical Society) packages used for amssymb, mathematical symbols and providing theorem environments. amsthm babel babel is used for writing a document in a language other than English; the language used is to be specified in the options. color For changing font colour; more will be said later. Make sure there is no ‘u’ in it! enumerate For greater control over lists. geometry For adjustment of margins. graphicx For inclusion of graphics into your document. mhchem For formatting chemical equations. tikz For in-document illustrations such as mathematical diagrams. url For handling urls in a document.

Table 2: Some packages

notes to accompany them.

More than one package can be specified in the \usepackage{...} command; simply space each individual package with commas \usepackage{package1,package2}. However, if you specify options in your package (using geometry, for instance), you must use a separate \usepackage{} command.

As with document classes, you can create custom packages. These are saved with the .sty file extension.

B Remember that package commands go in the preamble and not in the content!

Environments Aside from the preamble, the only commands in our document are

\begin{document} ... \end{document}

This is an example of an environment, well defined spaces inside a LATEX document which present information in different ways. They always start in the form \begin{environment} and end in end{environment}. Note that the \begin{document} and \end{document} commands signify an environment; in fact, these are essential lines in any LATEX document. Table 3 is a reference guide to some of the more common environments used in a LATEX document.

B Environments always go in the content of the article and not the preamble.

B If you use a \begin{environment} command, make sure you include an \end{environment} command after it in the content; otherwise your document will not compile.

For a more in depth guide on how to use different environments, see the guide Environments in LATEX.

5 Environments Uses array For producing arrays of data such as mathematical matrices. document For enclosing your document. enumerate For numbered lists. equation For centred maths equations. figure For a graphic, table or other centred object with caption. itemize For bulleted lists. tabular For generating tables. verbatim For displaying code.

Table 3: Some environments

Special characters The last thing to mention in this introductory document are the special characters. These are reserved characters for common functions in writing a LATEX document. For example, you have seen how curly brackets enclose the argument of a command. Table 4 details some of the more common reserved characters and their purposes.

Characters Uses $...$ For inline mathematics, i.e. writing equations mid-sentence. \ For denoting commands. A double backslash denotes a line break. To typeset a backslash, use $\backslash$. {...} Curly brackets are used to contain the argument of a command. % Putting a percentage sign before a line of code will ensure the compiler skips the line in the building process, omitting it from the document. & Used for alignment purposes (see the guide Environments in LATEX). # Used for defining new commands. ^, _ Used in inline mathematics mode to render superscripts and subscripts respectively; see the guide Layout, Formatting, Referencing.

Table 4: Some reserved characters

With the exception of the backslash, all of these characters can be printed in text by simply adding a backslash before them; for instance \% produces %.

Where to next?

Guide: Layout, Formatting, Referencing Guide: Environments in LATEX Guide: Creating a Beamer Presentation

Website and email

6