Home Coding » Writing » Photography »

Getting to Grips with LaTeX

Absolute beginners

by Andrew Roberts

This tutorial is aimed at getting familiar with the bare bones of LaTeX. 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.

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 to 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.

OK, so let us begin by deciding what we will actually get LaTeX to produce. As I said, we will produce the absolute bare minimum that is needed in order to get some output, and so I think the well known 'Hello World!' approach will be suitable here.

  1. Open your favourite text-editor. If you use vim or emacs, they also have syntax highlighting that will help to write your files.
  2. 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}
    			
  3. Save your file as 'hello.tex'. (Without the quotes!)

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 (%), which when LaTeX sees, 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 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} An educated guess would tell you that this command 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} Once again, this is not too difficult to understand. It tells LaTeX that the document source is complete.

You should also notice that 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.

Note, if you want to use the backslash or percent symbols within your text, you need to actually issue a command to tell LaTeX to draw the desired symbols, otherwise it will expect a command or a comment! The commands are:

Symbol Command
% \%
\ \textbackslash

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.

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.

Viewing the document

LaTeX has now done its job, so we can view the output. The default format is DVI (device independent), of which viewers exist freely for most platforms. However, the chances are that you would prefer to have a postscript file or PDF. Fortunately, there exist tools that can convert DVI to PS (and PDF) easily.

Converting to Postscript

Type the command: dvips hello.dvi -o hello.ps

dvips is the utility that actually performs the conversion. The first argument is the DVI file to be converted. The -o argument says that you want the output to be saved as a file. And the argument immediately after is the name you wish to call it. You could give it any name, but it makes sense to stick with hello, as well as giving it an informative .ps extension.

Converting to PDF

There are two easy routes to get a PDF:
  1. Type the command: dvipdf hello.dvi hello.pdf (Note that there is no -o with this command, because although the utilities look almost identical, they have slightly differing syntax)
  2. If you already have a postscript version, then type: ps2pdf hello.ps hello.pdf

Now it is simply a matter of using your preferred PS or PDF viewer to see the output. What you should see at the top left of the page are the words Hello World! and at the bottom is the current page number. All in a standard times font.

Going straight to PDF

It's now quite common to avoid the DVI/PS step. I think it's important to cover because it's quite a fundamental part of the LaTeX system. However, to learn how to go directly from LaTeX to PDF then view the PDF tutorial.

Summary

OK, we've created possibly the simplest possible document that LaTeX will produce (except for a blank page of course!) which is why it is not much to look at. However, now we have seen the basics, and how to actually use the LaTeX software, we can progress towards the more typical documents that you are likely to produce.

Files: hello.tex | hello.pdf

Last updated: February 22, 2012

« Back to LaTeX tutorials.