A Brief Introduction to the LaTeX Typesetting Environment/Typesetting: The Basics

From Wikibooks, open books for an open world
Jump to: navigation, search

Chapter 2: Typesetting, What is it and How do I do it?[edit]

In the LaTeX environment, as we've discussed before, things are slightly different from what you'd expect from a word-processor; this is because LaTeX is not a word-processor, it is a typesetting application. But what is typesetting and how is it different? We'll explore that here before moving on to explain how to typeset a document because a lot of the confusion regarding LaTeX revolves around the issue of typesetting and the paradigms involved.

When you create a document in LaTeX you control nearly every bit of what is put on the page. The level of complexity available to a seasoned user is almost baffling but it is beautiful in that it allows highly professional documents to be created. That said, LaTeX also provides simple templates to create basic documents quickly and professionally as well so there's no need to worry.

The first thing you'll notice when you open up the LaTeX application you've chosen is that what you type into the window doesn't have a whole lot of options. You can't make it italic or bold, you can't make a strikethrough or change the font size. This is because what you're typing in that window isn't the final document. What goes there is the content of the finished product; once the content has been typeset things like italics and boldface fonts will appear.

If it helps to think of it in terms of programming, consider Java or C or another language you may be familiar with, HTML even. What's written in the code isn't the final product - it's simply the instructions passed to the computer to produce an application. The same is true of LaTeX; what you type in the box isn't the final document, it's the content and instructions that are sent to the computer to produce it.

Example 1[edit]

As a simple example, let's use a LaTeX template to create a simple document. Afterwards we'll discuss what everything we typed did and how it can be modified or expounded upon in the future. This example doesn't produce anything particularly pretty so don't expect a gorgeous end-result.

Open the LaTeX application you chose to install and create a new .tex document. In the entry window, type the following and be sure to change <Your Name Here> to your actual name:

\documentclass[10pt,a4paper,oneside]{article}
\usepackage{geometry} 
\geometry{letterpaper} 
\usepackage[parfill]{parskip}
\usepackage{epsfig}
\usepackage{float}
\usepackage{subfigure}
\usepackage[]{hyperref}
\usepackage{amssymb}
%
\begin{document}
\begin{figure}
\centering
{\large\bf My First LaTeX Document}\\
By: <Your Name Here>\\
\end{figure}
%
This is my first document using the LaTeX typesetting environment.  
%
It isn't very detailed, but it certainly looks good.
%
\end document

The \documentclass property[edit]

This particular option can be set anywhere in the document in theory, although good practice generally dictates placing it before the \begin{document} flag. For convenience, the author suggests placing it as the first part of any document, simply so that it isn't forgotten. If this flag is not specified before typesetting, the outcome is difficult to anticipate; some typesetting applications simply won't typeset while others will assume some default which may or may not be what you like. If you're planning on making the .tex file available to others, or typesetting on another machine later which may have a different application, it's best just to place it at the top.

Let's dissect the command used above:

\documentclass[10pt,a4paper,oneside]{article}

First is the command itself, called by typing \documentclass before the \begin{document} command which is discussed in a later section. Secondly come the options. Specifics come inside the brackets - [] and specify a variety of things. There are a ridiculous number of options you can specify here so we'll ignore those for now and just discuss the ones you're most likely to use in the beginning.

First there's the 10pt option we used. This is the font-size for the document. This can be changed in paragraphs or for individual words later in the document, however there is a separate command for that we'll discuss later as well. If you'd like to see how this varies, change the command we used but make 10pt 11pt, or 12pt, and you'll see how the text size for the document changes.

Note that between options there is no space but a comma (,); this is the general format we will use for options in this book. It is also the standard for option modifiers in the \LaTeX language but some specific packages allow spaces while others don't and it's best to be safe.

Second there's the command a4paper. If you're not familiar with the types of paper available, or the modifiers available in \LaTeX, don't bother changing this command. a4paper is used for standard 8.5",11" paper while the 'legal' flag is used for legal sized paper. Until you're more familiar with the system, the author advises not playing with this command too much.

Finally there's the oneside option. As it says, this causes \LaTeX to produce documents that will print one-sided. This is a bit of an outmoded command since usually when you print a document on a printer that even supports multisided documents the option is instead handled by the printer driver. Nevertheless, \LaTeX embeds a command in the .pdf it creates that will cause some print drivers to automatically print one-sided regardless of the defaults although, generally speaking, this kind of driver will not be encountered by the average user.

The last bit of the \documentclass modifier is located in curly-brackets {} and is '{article}'. While what we created isn't technically an article it's the easiest style of document to work with. There are also {book} and {letter} modifiers which are typical, and a whole slew of others you can use once you're more familiar. For now, we'll stick with the {article} class because it will allow us to create simple documents like those most readers will be looking to create.

An important note[edit]

In the above example you'll notice that some of the document is written inside of two flags, namely:

\begin{figure}

and

\end{figure}

These specify a figure in \LaTeX which is not itself a very simple object, as nothing in \LaTeX truly is. However, we will not be playing a lot with the options available in \LaTeX figures so don't worry.

The caveat here is that if you were to try to typeset the above example without using the following command before the \begin{document} constructor:

\usepackage{subfigure}

it would still work. This is because a figure is not a subfigure. We'll use a subfigure in later examples but if you were to try to use a subfigure without \usepackage{subfigure} you'd get an error and your document wouldn't typset. This is because a figure is an inherent object in the \LaTeX library where a subfigure is not and therefore \LaTeX has no idea what you're talking about when you say subfigure unless you give it a \usepackage to reference.

Remembering this fact will solve a lot of problems in the future if you're using different packages and should help with troubleshooting when you're writing your own documents.

The geometry option[edit]

The geometry option is also important to set in any document. It can be specified with the flag as shown above. More often than not you'll be printing in a portrait rather than landscape since the document will probably be something you'd read in print.

It's important to note, and we will get into this later on in the book, that this flag cannot be changed throughout the document. It's a one-time deal. If you've got a page of graphs that you think would look better displayed in a landscape style there is a different command for this and we will introduce it later on.

The \usepackage option[edit]

This is probably the simplest concept to think of, while also being one of the more difficult to deal with. If you're using a different application to typeset your documents than the ones suggested in this book then you'll have to look at the documentation when you're using this command.

Essentially what's happening here is that you're adding functionality to your code. It's similar to adding a library to a Java program so that your code can utilize simple functions predefined by yourself or other people. However, not every LaTeX distribution comes with every package you would need.

MacTex has a majority of the commonly used packages preinstalled and MikTex has an option to automatically download any that aren't already installed on the system being used. However, if you should come across an error where your typesetting application cannot find the package or doesn't have it installed you'll either have to go about writing without using it, or find it online and install it yourself.

The details of installing a usepackage on a LaTeX typsetting program is a bit tedious and the author recommends using a Google search or the documentation provided with your application for those purposes since every distribution on different operating systems is different and therefore would be too much to go into in a short crash-course such as this one.

Let's take a look at two of the author's favorite \usepackage functions.

\usepackage[parfill]{parskip}[edit]

The author loves this option because it makes writing the document feel slightly more like what we're all used to if we learned to type in a word-processor.

If this \usepackage wasn't specified, something typed as so:

This is a paragraph

This is also a paragraph

Would typeset to one single paragraph. If you're not using the \usepackage[parfill]{parskip} \usepackage in your document, you'll have to end paragraphs as so:

This is the end of a paragraph I've written; I'd like the next to be on a new line \\

The double slash specifies the end of a paragraph or line.

\usepackage{amssymb}[edit]

The details of this package are a bit convoluted, but it's important for anyone who will be typing mathematical functions. There are a lot of math symbols out there and having access to them in the \LaTeX environment is crucial. For instance, with the \usepackage{amssymb} modifier at the beginning of the document typing:

$$\alpha = \frac{\pi}{\Beta}$$

Will give you the proper Greek letters. Other modifiers may do so as well, but they may look different or require different commands to call them.

For the rest of this book, unless otherwise specified, the amssymb \usepackage will be used in all the documents to handle the mathematical aspects. For that reason, if you're using a different math symbol package you may experience errors in recreating the examples provided; the author cannot help other than to say that Google is your friend when looking up functions in \LaTeX.

The \begin{} and \end{} functions[edit]

In our example we had two of these. The first was

\begin{document}

Which tells \LaTeX that what we're going to be typing next is the document itself, rather than the commands that it needs to have to specify how to typeset the document. If you try to typeset any document without the \begin{document} command you will get an error.

We also have the following command:

\begin{figure}

Which tells \LaTeX that what follows needs to be put in the class of 'figure' and handled as such. As we said above, the figure is actually quite complicated, but for our purposes it's just like a box we can put things which we want to stay together.

Now, one must be very careful when using a \begin{} statement because if something begins, it must end. Think of a Java class. We cannot say

static void main([] int)
{

without also specifying the end of the class, the code would not compile. In order to get a functioning program, we'd need

static void main([] int)
{
   Commands
}

The same is true of a LaTeX \begin{} command, we also need to end what we began or the document will not typeset. For the figure, this is done as follows:

\begin{figure}
   Contents of the figure
   More contents of the figure
   Maybe some math ...
\end{figure}

And everything is all well and good.

However, the \begin{document} function is slightly more crucial and will need to be used with care.

\begin{document}
   We've now begun the document.  Everything here is part of the document we wish to typeset.  For instance, if we want a figure:
   \begin{figure}
       Contents of the figure
   \end{figure}
   And now we have more of our document to deal with.  When we're finished with everything we want to typeset as a document, we must be sure to end it as follows
\end{document}

Now everything will be good to go and you will be able to typeset the code.

Mathematical Functions[edit]

LaTeX is a wonderful change from word-processing in the way that it handles mathematical input. In most word processors another application needs to be run within the main program in order to create the properly formatted equations. Once this is done they are usually placed in the document as an image but even if they aren't putting them precisely where you want them is often a hassle, changing the document after placing the equation is difficult, and many times the formatting of the mathematics will be different from that of the rest of the document making it look unprofessional.

LaTeX is different in that you won't be able to see what you've written until you typeset the document. For that reason the equations you type are handled by the back-end and placed in the document with proper formatting and in the right location.

There are two main ways to insert a mathematical equation into your document. Read the 2 following sections and then modify the example above to have 2 equations you may wish to see in a document, or two random equations, with each method.

Inline Mathematical Functions[edit]

The simplest way to place a mathematical function in a LaTeX document is to do it inside of a sentence or paragraph your writing. For instance, say we wanted to define a variable we'll use in a later equation in the same sentence it is described:

We will let the function f(x) be defined as $f(x) = \sqrt{x}$

The way this was achieved was with the use of the dollar-sign symbols. Whenever you'll be placing an equation inside of a paragraph or sentence, you use the "$" symbol to tell LaTeX that what comes next should be treated as a math equation rather than as simple text.

You'll want to be careful about using words inside of equations however, since they are handled very differently. The best method would be as follows:

We assume that the constant n is defined to be in units of moles$^{-1}$

Which will place "-1" in the exponent of "moles" making it the unit of inverse-moles. Where this specifically would be useful the author cannot be sure, but it serves to demonstrate the point that while we wanted the "-1" to be an exponent, we did not want "moles" to be typeset as an equation would be. It would change the formatting of the letters and look different from the rest of the text.

Separate Mathematical Functions[edit]

If one wishes to insert an equation on its own line there are a variety of methods for doing this. Later on we shall see how to insert a list of equations in the table of contents so it's important to note that equations on their own line are not necessarily numbered by LaTeX.

If we don't want an equation numbered we can do it using two dollar signs ($$) like so:

This is a sentence that would describe the following random equation:
$$f(x) = \frac{\sqrt{x}}{2}$$
Where $f$ is a function of $x$ called $f(x)$.

Not that any of the above is particularly helpful in real life, but it demonstrates the difference between a single dollar-sign and a double dollar-sign. While the things given to LaTeX as $f$, $x$, and $f(x)$ will be kept in their respective sentence, the other equation kept in the double dollar-signs ($$) will be given its own line.

Please note that a double dollar sign can be substituted with $[ and $] and that both forms will not be numbered.

If we wish to have a numbered equation so that it can be referenced later or automatically added to a list of equations in the table of contents, we would modify the above example as so:

This is a sentence that would describe the following random equation:
\begin{equation}
   f(x) = \frac{\sqrt{x}}{2}
\end{equation}
Where $f$ is a function of $x$ called $f(x)$.

Everything inside of the \begin{equation} and \end{equation} brackets will be formatted as a single equation and it will be numbered by LaTeX. This is especially useful if you'll be referencing the equation a lot in your text, or if you'd like to make it available later on in the table of contents.

It's important to note that if you have two separate equations that you want numbered you would not place both of them inside the \begin{} and \end{} functions. This would be typeset as a single equation which would not look very much like what you're hoping for. Each individual equation would need to be bracketed within its own \begin{} and \end{} functions as shown here:

Here are our first two equations:
\begin{equation}
   f(x) = 2x^2
\end{equation}
\begin{equation}
   g(x) = f(x)^2 = 4x^4
\end{equation}

And each of these would be labeled as equations in their own right as well as being displayed separately.

There are a variety of other environments available for inputting equations besides the '$', '$$', or 'begin{equation}' options, but for the sake of levity we will limit our discussion to these alone.


Introduction · Writing a Document from a Template