LaTeX/Absolute Beginners

From Wikibooks, the open-content textbooks collection

< LaTeX
Jump to: navigation, search

This tutorial is aimed at getting familiar with the bare bones of LaTeX. First, ensure that you have LaTeX installed on your computer (see Installation for instructions of what you will need). We will begin with creating the actual source LaTeX file, and then take you through how to feed this through the LaTeX system to produce quality output, such as postscript or PDF.

Contents

[edit] The LaTeX source

The first thing you need to be aware of is that LaTeX uses a markup language in order to describe document structure and presentation. What LaTeX does is convert your source text, combined with the markup, into a high quality document. For the purpose of analogy, web pages work in a similar way: the HTML is used to describe the document, but it is your browser that presents it in its full glory - with different colours, fonts, sizes, etc.

The input for LaTeX is a plain ASCII text file. You can create it with any text editor. It contains the text of the document, as well as the commands that tell LaTeX how to typeset the text.

For the truly impatient, a minimal example looks something like the following (the commands will be explained later):

\documentclass{article}
 
\begin{document}
Hello world!
\end{document}

[edit] Spaces

"Whitespace" characters, such as blank or tab, are treated uniformly as "space" by LaTeX. Several consecutive whitespace characters are treated as one "space". Whitespace at the start of a line is generally ignored, and a single line break is treated as “whitespace.” An empty line between two lines of text defines the end of a paragraph. Several empty lines are treated the same as one empty line. The text below is an example. On the left hand side is the text from the input file, and on the right hand side is the formatted output.

It does not matter whether you
enter one or several             spaces
after a word.
 
An empty line starts a new
paragraph.
It does not matter whether you enter one or several spaces after a word.

An empty line starts a new paragraph.

[edit] Special Characters

The following symbols are reserved characters that either have a special meaning under LaTeX or are unavailable in all the fonts. If you enter them directly in your text, they will normally not print, but rather make LaTeX do things you did not intend.

# $ % ^ & _ { } ~ \

As you will see, these characters can be used in your documents all the same by adding a prefix backslash:

\# \$ \% \^ \{ \} \& \_ \{ \} \~ \textbackslash

The other symbols and many more can be printed with special commands in mathematical formulae or as accents. The backslash character \ can not be entered by adding another backslash in front of it (\\); this sequence is used for line breaking. For introducing a backslash in math mode, you can use \backslash instead. The command \~ produces a tilde which is placed over the next letter. For example \~n gives ñ. To produce just the character ~, use \~{} which places a ~ over an empty box.

If you want to insert text that might contain several particular symbols (such as URIs), you can consider using the \verb command, that will be discussed later in the section on formatting.

[edit] LaTeX Commands

LaTeX commands are case sensitive, and take one of the following two formats:

  • They start with a backslash \ and then have a name consisting of letters only. Command names are terminated by a space, a number or any other "non-letter".
  • They consist of a backslash \ and exactly one non-letter.

Some commands need a parameter, which has to be given between curly braces { } after the command name. Some commands support optional parameters, which are added after the command name in square brackets [ ]. The general syntax is:

\commandname[option1,option2,...]{argument1}{argument2}...

[edit] LaTeX environments

Environments in LaTeX have a role that is quite similar to commands, but they usually have effect on a wider part of the document. Their syntax is:

\begin{environmentname}
text to be influenced
\end{environmentname}

between the \begin and the \end you can put other commands and nested environments. In general, environments can accept arguments as well, but this feature is not commonly used and so it will be discussed in more advanced parts of the document.

Anything in LaTeX can be expressed in terms of commands and environments.

[edit] Comments

When LaTeX encounters a % character while processing an input file, it ignores the rest of the present line, the line break, and all whitespace at the beginning of the next line.

This can be used to write notes into the input file, which will not show up in the printed version.

This is an % stupid
% Better: instructive <----
example: Supercal%
            ifragilist%
icexpialidocious
This is an example: Supercalifragilisticexpialidocious

The % character can also be used to split long input lines where no whitespace or line breaks are allowed.

[edit] Input File Structure

When LaTeX processes an input file, it expects it to follow a certain structure. Thus every input file must start with the command

\documentclass{...}

This specifies what sort of document you intend to write. After that, you can include commands that influence the style of the whole document, or you can load packages that add new features to the LaTeX system. To load such a package you use the command

\usepackage{...}

When all the setup work is done, you start the body of the text with the command

\begin{document}

Now you enter the text mixed with some useful LaTeX commands. At the end of the document you add the

\end{document}

command, which tells LaTeX to call it a day. Anything that follows this command will be ignored by LaTeX. The area between \documentclass and \begin{document} is called the preamble.

[edit] A Typical Command Line Session

LaTeX itself does not have a GUI (graphical user interface), since it is just a program that crunches away at your input files, and produces either a DVI or PDF file. Some LaTeX installations feature a graphical front-end where you can click LaTeX into compiling your input file. On other systems there might be some typing involved, so here is how to coax LaTeX into compiling your input file on a text based system. Please note: this description assumes that a working LaTeX installation already sits on your computer.

  1. Edit/Create your LaTeX input file. This file must be plain ASCII text. On Unix all the editors will create just that. On Windows you might want to make sure that you save the file in ASCII or Plain Text format. When picking a name for your file, make sure it bears a .tex extension.
  2. Run LaTeX on your input file. If successful you will end up with a .dvi file. It may be necessary to run LaTeX several times to get the table of contents and all internal references right. When your input file has a bug LaTeX will tell you about it and stop processing your input file.

Type ctrl-D to get back to the command line.

latex foo.tex

Now you may view the DVI file. On Unix with X11 you can type xdvi foo.dvi, on Windows you can use a program called yap (yet another previewer).

You can run a similar procedure with pdflatex to produce a PDF document from the original tex source. Similar to above, type the commands:

pdflatex foo.tex

Now you may view the PDF file, foo.pdf.

[edit] Our first document

Now we can create our first document. We will produce the absolute bare minimum that is needed in order to get some output; the well known Hello World! approach will be suitable here.

  • Open your favourite text-editor. If you use vim or emacs, they also have syntax highlighting that will help to write your files.
  • Reproduce the following text in your editor. This is the LaTeX source.
% hello.tex - Our first LaTeX example!
\documentclass{article}
\begin{document}
Hello World!
\end{document}
  • Save your file as hello.tex.

[edit] What does it all mean?

% hello.tex - Our first LaTeX example! The first line is a comment. This is because it begins with the percent symbol (%); when LaTeX sees this, it simply ignores the rest of the line. Comments are useful for humans to annotate parts of the source file. For example, you could put information about the author and the date, or whatever you wish.
\documentclass{article} This line is a command and tells LaTeX to use the article document class. A document class file defines the formatting, which in this case is a generic article format. The handy thing is that if you want to change the appearance of your document, substitute article for another class file that exists.
\begin{document} This line is the beginning of the environment called document; it alerts LaTeX that content of the document is about to commence. Anything above this command is known generally to belong in the preamble.
Hello World! This was the only actual line containing real content - the text that we wanted displayed on the page.
\end{document} The document environment ends here. It tells LaTeX that the document source is complete, anything after this line will be ignored.

As we have said before, each of the LaTeX commands begin with a backslash (\). This is LaTeX's way of knowing that whenever it sees a backslash, to expect some commands. Comments are not classed as a command, since all they tell LaTeX is to ignore the line. Comments never affect the output of the document.

[edit] Generating the document

It is clearly not going to be the most exciting document you have ever seen, but we want to see it nonetheless. I am assuming that you are at a command prompt, already in the directory where hello.tex is stored.

  1. Type the command: latex hello (the .tex extension is not required, although you can include it if you wish)
  2. Various bits of info about LaTeX and its progress will be displayed. If all went well, the last two lines displayed in the console will be:
Output written on hello.dvi (1 page, 232 bytes).
Transcript written on hello.log.

This means that your source file has been processed and the resulting document is called hello.dvi, which takes up 1 page and 232 bytes of space. This way you created the DVI file, but with the same source file you can create a PDF document. The steps are exactly the same as before, but you have to replace the command latex with pdflatex:

  1. Type the command: pdflatex hello (as before, the .tex extension is not required)
  2. Various bits of info about LaTeX and its progress will be displayed. If all went well, the last two lines displayed in the console will be:
Output written on hello.pdf (1 page, 5548 bytes).
Transcript written on hello.log.

you can notice that the PDF document is bigger than the DVI, even if it contains exactly the same information. The main differences between the DVI and PDF formats are:

  • DVI needs less disk space and it is faster to create. It does not include the fonts within the document, so if you want the document to be viewed properly on another computer, there must be all the necessary fonts installed. It does not support any interactivity such as hyperlinks or animated images. DVI viewers are not very common, so you can consider using it for previewing your document while typesetting.
  • PDF needs more disk space and it is slower to create, but it includes all the necessary fonts within the document, so you will not have any problem of portability. It supports internal and external hyperlinks. Nowadays it is the de facto standard for sharing and publishing documents, so you can consider using it for the final version of your document.

About now, you saw you can create both DVI and PDF document from the same source. This is true, but it gets a bit more complicated if you want to introduce images or links. This will be explained in detail in the next chapters, about now assume you can compile in both DVI and PDF without any problem.

Note, in this instance, due to the simplicity of the file, you only need to run the LaTeX command once. However, if you begin to create complex documents, including bibliographies and cross-references, etc, LaTeX needs to be executed multiple times to resolve the references. But this will be discussed in the future when it comes up.


Previous: Introduction Index Next: Basics
Personal tools
Create a book