It looks like you're using Internet Explorer 11 or older. This website works best with modern browsers such as the latest versions of Chrome, Firefox, Safari, and Edge. If you continue with this browser, you may see unexpected results.

# LaTeX

a guide to using LaTeX document preparation software

## Document Structure

Each LaTeX program has these three general parts:

• Preamble
• Document Environment
• [Optional] Special Pages

The Preamble includes defining the type of document you wish to create (such as an article, book, report, etc.) as well as a call to any packages you wish to use in addition to the default packages. For example, if we wish to write a journal article with 11pt font and use the package “graphicx”, our first two lines of our LaTeX program would look like:

The Document Environment is the body and actual content of your document and includes texts, tables, figures, images, equations, and anything else you wish to include in your actual document. You may include header information such as the title, subtitle, author, date, and abstract. You may also format it to your liking through the use of chapters, sections, paragraphs, and appendices. The commands and formatting options available vary by document class, but they are all similar in nature.

You begin and end this section of the document through the commands:

In general, the word environment refers to a section of your LaTeX edior that follows a certain format, has its own reference scheme, or has specific command options different from the rest of the document, and it is characterized by \begin{} and \end{} commands. The document environment is the main environment, but within that you can create environments for figures, tables, equations, etc.

The Special Pages are optional and may include such pages as an index, glossary, and/or bibliography. This is a more advanced topic not covered in this guide, but we do review how to create a simple bibliography using BiBTeX.

## Document Classes

The document classes specify what type of document you want to create. Though there are many predefined formats, there are really only two that students use most often:

• Article: used when writing scientific journal articles, presentations, short reports, etc.
• Report: used when writing longer reports, reports with chapters, small books, theses, etc.

The document class must be specified in the Preamble, before the body of the document, so that LaTeX knows how to format the content. You do this with the command:

The commands in the square brackets are optional and are used to override any defaults. At the end of this guide are templates for you to get started with LaTeX. There will be templates and examples for both document types.

## The Document Environment

The first thing to include in the document environment (after the \begin{document} line) is the information on the document’s title, author, date, etc. The commands to create this first part of your document is:

Note that you can include the actual date if you don’t want to print today’s date.

If you wish to include an abstract, that would go next using the commands:

Now the remaining part of the document is the main body, including your text and any tables, figures, equations, etc. You type it up in the same manner you want it to appear, but you’ll have to use some LaTeX commands to reformat, insert figures, create tables, etc. The text is typed exactly like you would in Word or another word processor, but if you want to change the font type, size, color, etc., that would require a LaTeX command as well. More detail on the text and formatting options follows later in this guide.

Many people like to organize their documents by the use of paragraphs, sections, or chapters. Each defines a new part to your document but the formatting is different, as is the way it is treated in a table of contents, if you decide to use one. They may vary by document class, but in general the formatting are as follows:

• A paragraph puts the title in bold, but the text of the paragraph follows immediately after in normal font on the same line:

• A section includes a number or other counter, puts the title in bold and a large font, but the text follows in normal font on the next line with an indent:

You could also include subsections (or even sub-subections), which will continue the numbering scheme of the larger section:

• A chapter includes a number or other counter, puts the title in bold and in a very large font, starts a new page, and the text follows in a normal font on the next line.

Typically the ordering, from highest level to lowest level, is chapter, section, subsection, sub-subsection, paragraph, and subparagraph. However, these are all optional, and furthermore, chapters are only available in the book and report classes. You will most likely only need to use the section, subsection, sub-subsection, and paragraph commands:

Note that when you add Table of Contents and other lists of references in the document you need to compile the document twice to make sure that the page numbers are correct.

## Creating a Bibliography

If you want to create a bibliography or references page, you can simply do it by embedding it in the LaTeX editor itself. You would format it (including title, typesetting, indentations, line breaks, etc.) and type in the text itself. You would probably want to create a new page, insert a header saying References or Bibliography, and then begin the actual list of references. It would look similar to as follows:

This is the simplest way to create a bibliography for beginners (the closest to the way you would do it within Word or another word processor), but it can also be time consuming.

As an alternative you can use BiBTeX to create your bibliography page. It’s a few more steps, but when you have many references and citations, it should actually save you time. You also don’t have to manually format the page, as the BiBTeX package will do it for you. Furthermore, if you write several papers or articles citing the same reference(s), you don’t need to continually create a new bibliography section. You can refer to the same set of references in several documents, yet each individual article to report will only include in its reference page only those references that you cite in that particular document. Therefore, if you are willing to take a little extra time to learn how to use BiBTeX, it can save you a lot of time and effort in the long run. It’s actually pretty simple once you learn how to use it! We highly recommend this route.

To create a bibliography using BiBTeX, instead of embedding the references in the actual LaTeX editor itself, you create a separate file. To do this, first open a new LaTeX editor. It is in this new, empty file that you will create your references file which you will refer to in your main LaTeX document. Now within this new file, you compile your references. A typical entry will look as follows:

The above creates a references to an article with tag ref1. The word after the @ symbol tells LaTeX what type of reference it is, and thus how to best format it. There are several other types of references, including books, chapters, theses, technical reports, and proceedings, among others. The type of reference dictates which field are required to correctly create the reference. Field may include author, title, edition, journal, month, year, publisher, pages, series, school, volume, etc. The following table summarizes the available reference types and required fields:

The table comes from Wikipedia’s article on Bibliography Management in LaTeX, the link for which is also provided in the resources at the end of this guide. That resource, along with some of the others, also provides a discussion of when to use which reference type and other formatting topics.

Once all entries have been completed, it is time to save the file. However, be careful! When you save it, you are no longer saving it as a .tex, which is the default extension. Instead, use the .bib extension. For example, you might save your reference document as Ref.bib. Be sure to keep this saved in the same directory as your original LaTeX document, the one to which you wish to apply the references. Once it is saved, you can close the bibliography file and return to your main .tex file of your document. To create the bibliography, at the end of your document (after all your other content but before the \end{document} line), you use the following commands:

The first line specifies the style and formatting of your bibliography, which you may want to change depending on your needs. For example, an instructor may expect a certain format if you are submitting a class project, or a journal may require another format if you are submitting an article for publication. There are formats that adhere to APA and MLA, in addition to others. One of the links in the resources provides information on the different bibliography styles available and what they will look like.

The second line provides the name of the ‘.bib’ file that contains the list of reference. In this example, the references are compiled in Ref.bib.

Finally, whether using BiBTeX or embedding the bibliography, you will likely want to cite the references throughout your document. Notice that in either form, we assigned a tag (ref1 in this case) to each of our references. These tags must be unique, because we will use them for citations. Within your document, whenever you wish to cite a reference from your bibliography, you use the following command:

Note that when you use this, LaTeX will format it according to whatever bibliography style you’ve chosen (or, if embedded, according to the document class). However, it will not add things like parentheses, so you must include that yourself if you need them.

## Use of Color in Edit Mode

Within your editor you have the option of using syntax coloring as you type in commands. This option is found in the menu under Format->Syntax Coloring->LaTeX. When this is turned on, the text is no longer all black but will change color based on the type of syntax. The advantage to using color is that you can quickly identify what is a command, what is text, etc. This is extremely useful for finding typos or when troubleshooting after getting an error message. LaTeX syntax coloring uses the following scheme:

## Text Options

Even though you may feel like there is a lot of syntax and LaTeX commands, the majority of your editor will still be the actual text and content of your document. Though the format is already defined based on your document class, there will be countless occasions when you may want to override the defaults. Here are some basic text options:

Note that, basically, when brackets are involved, the command is assigned only to the text in the brackets. When brackets are not used, the command is followed indefinitely until it is overridden by another. Most of these commands require the use of brackets, but some, such as font size, do not. The only time you may want to eliminate brackets is if, for some reason, you want a significant chunk of your document to be a different size than normal. Then you can use one of the commands without the brackets, but then you must be sure to revert back to normal size if you need it to be so:

## Formatting options

Just as with text editing, there are lots of ways you may want to format your layout that is not standard in the default. Some of these are automatically done for you as you start a new paragraph or section, so it is recommended that you first run the code, review the output, and then make any additional modifications that you desire. For example, LaTeX will typically indent new paragraphs on its own when you start writing text on a new line in the editor, so you needn’t include a command to indent for every new paragraph you write. However, if you look at the published document and decide that it needs some modifications, here are some basic formatting options:

## Lists

LaTeX makes it easy to create either enumerated (numbered) or itemized (bulleted) lists. To create a numbered list, use:

Similarly, if you wish to create a bulleted list, use:

## Symbols and Special Characters

Another one of the ways that LaTeX is different from a typewriter or a traditional word processing program is the way that special characters are treated. Some symbols are used to communicate with the typesetter. Some other symbols are used to typeset special characters. So, unlike in say, Word, what you see is not what you get.

Symbols used as commands

LaTeX has embedded commands in the form of symbols. For example, a percent sign (%) at the start of a line will ignore anything in that line (it acts as a comment for the editor and will not show up in the document). In these cases, to override LaTeX reading it as code, you need to add a backslash to the front of the symbol. Here are a few common examples of when you would need to do this:

Symbols that are typeset as other symbols

Depending on how the font is set up, LaTeX will typeset certain symbols differently, for example, the < (left than symbol) and the > (greater than symbol) will be typeset as the upside down explanation point and the upside down question mark used in the Spanish language. You can typeset symbols literally in a couple of different ways.

• Use symbols in an equation environment - see next section
• Use commands such as \textless{} for < and \textbar{} for >
• Change the font encoding (This is a bit more advanced)

Typesetting special characters

In select cases, , the keyboard version isn’t sufficient. Here are two common situations where you would want to avoid using the traditional keystroke:

## Mathematics and Equations

One of the main benefits from using LaTeX over other word processors is its ability to easily and expertly create professional-looking mathematical equations. Though not required, we recommend downloading and utilizing the “amsmath” package (\usepackage{amsmath} in the Premable) as it has some useful features for displaying equations.

If you try to use a mathematical term in your text without the proper environment, you will get an error. Therefore you need to tell LaTeX what is a mathematical expression, term, or equation. If you want to show in line with the other text, you simply use a dollar sign before and after the expression:

If, instead you wish for it to stand alone, you have one of two options. The quickest option is to start a new line in the editor and enclose your mathematical formulation with a series of square brackets and slashes:

This will center your function on a new line in your document, but will not number it. If you are interested in numbering your equation or if you have more a more complex expression, you may want to use the equation environment:

When using the equation environment as displayed above, LaTeX will number your equation so that you may reference it in the text. The tag for the equation is in the \label command – in this example, we tagged the equation as eq1. Now to cite this in the text, you use a command similar to the citation command from the references (this will print the number only, so you may want to put the word Equation before it):

If you want to use the equation environment and don’t want it numbered or don’t need to reference it later, you can just add a asterisk and eliminate the label command:

There are many cases where you will want to use a multi-line equation, such as when you want to show the steps to simplifying a function. To do that, you need an equation array environment:

Notice that, in this example, we used an asterisk so that it will not be numbered. If you want to number the array, just remove the asterisk. Additionally note the use of the ampersands. These will define the array at the point where they should line up. So in this case, all the equal signs will align on different lines. Recall that the double backslash will begin a new line.

There are many nuances of mathematics and equations in LaTeX that we will not get into in this guide, so we recommend reviewing the resources for more details. Additionally, the document containing the common LaTeX symbols also includes mathematical symbols and some information on creating equations. Three things we will note here:

• Generally, the spacing doesn’t matter in an equation environment – LaTeX already has default spacing depending on what you type. If you wish to add more spacing than what is given, a quick solution is using \; , which will add more space. You can use this several times in a row to create even more space: \;\;\;\;

• Text in an equation will be read like math symbols, so letters will be italicized and won’t be spaced properly. To use text within a mathematical environment, use the \mbox{Regular text...} command.

• You can use brackets whenever you want to assign a command to more than just the sole next character to appear. For example, if you want x^12 to appear, you must use x^{12} rather than x^12, which will give you x^12.

## Figures and Tables

Figures and tables can easily be incorporated in your document. Though not required, we recommend downloading and utilizing the graphicx package (\usepackage{graphicx} in the Premable) as it has some useful features for handling graphics.

Figures should be saved as separate files in one of the following formats: .jpg, .png, .pdf, or .eps. The easiest thing to do is the save it in exactly the same directory as your editor file, as this is where LaTeX will go to look for figures. You can also create subfolders within that directory and refer to them that way. In this case, you will have to use an address format with forward slashes – e.g., if you save the figure fig1 in a folder called Figs, which is in the directory of your editor, the file name will look like Figs/fig1.pdf. If you use fig1.pdf LaTeX will assume that it is in the same directory as the editor. There are ways you can change the default directory for figures, but we won’t go into that here.

Once you have all your figures saved in the proper format and in the appropriate folder, you can input them into your document using the following commands in the editor:

This chunk of code will center and print the figure “Figs/fig1.pdf,” print a caption (“This is the caption.”), and tag it for referencing the text of the document. If you don’t want to center the figure, just remove the \centering command. Just as we did with equations, we can refer to this particular figure number with (again we may want to put the word “figure” before the reference since it only prints the figure number):

Recall that typically anything in square brackets ( [ ] ) after a command is where we insert options for that command. In this case we must put a placement option. The table below outlines what the options are for the placement, and what it will do:

The table comes from Wikibooks’s Floats, Figures, and Captions page in their LaTeX guide. Note that it recommends using the float package if you want the figure to go exactly where you insert it in the editor. The guide also provides some information on more advanced figure formatting options, such as subfigures, changing the size, etc.

Tables are a bit trickier to insert in a document. They require more LaTeX commands and it is less straightforward than figures. A basic table will look something like this:

There is certainly a lot going on here. First we will note there are some elements that are the same in both the table and figure environments: the placement option, the centering command, the caption, and the label. These are all used identically, so you can refer to the discussion of figures for descriptions of these. What is new in the table is the tabular environment; this is what we need to include to actually create the table.

The { | l | c | r | } line, immediately following the start of the tabular environment, defines the format of the table. Specifically, we define the alignment of the columns and whether the columns are separated by vertical lines (rather than just a space). A single letter represents a column, with “l” meaning left-justified, “r” meaning right-justified, and “c” meaning centered. The vertical line, |, indicates that you do indeed want a vertical line printed in between those two particular columns (or at the left- and right-side of the table). You may also use two vertical lines, ||, if you want a double vertical line printed between the columns. If omitted, there will just be a space between columns. So, in the example above, there are three columns; the first is left-justified, the second is centered, and the third is right-justified. Additionally, all columns are separated by vertical lines, and table also has vertical line at the rightmost and leftmost sides.

The next few lines, before the \end{tabular} command, compose the actual contents of the table. The ampersands (‘&’) act as column separators, the double backslash (‘\\’) goes to the next row, and the \hline command inserts a horizontal line that goes across all columns. Note that the number of ampersands in each row MUST be equal to the number of columns minus 1 (because there should be one between each cell entry). If you wish to insert a horizontal line across only certain columns, you can use the following command (in this example it starts in column 2 and ends in 4, but of course can be modified to fit your needs):

We recommend you peruse the templates and resources to see more examples of how to create tables.

Just as we were able to create a table of contents for the document, we can also create a list of figures and tables easily. The table and figure numbers and captions will be included, along with their page locations. To include either a list of figures or list of tables, simply insert the following command(s) wherever you wish it to appear in your final document (usually after the table of contents):