Absolute Beginners
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.
- 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'. (Without the quotes!)
What does it all mean?
% hello.tex - Our first LaTeX example! | The first line is a comment. Comments are for humans only and are completely ignored by LaTeX. Lines beginning with the percent symbol (%) are comments. The are completely optional and there's no requirement to include a comment at the beginning or any where in your LaTeX source code. However, they can be useful for leaving useful notes for yourself or other potential readers/contributors to the document. |
\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.
- Type the command:
latex hello
(the .tex extension is not required, although you can include it if you wish) - 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.
What's a DVI file?
DVI stands for the DeVice Independent file format, and is something of a relic of the state of early computerised typesetting from the 1980s. The idea is that LaTeX can output to an intermediate format that contains all the information to render the document, and then DVI "drivers" could interpret the DVI file to its particular requirements, e.g. display on screen or convert to other formats.
As a format it never gained any traction beyond the LaTeX ecosystem and didn't become the PDF format of it's day. And whilst there exist plenty of tools related to the DVI format, at best it's merely a stepping stone to PDF or Postscript.
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:- 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) - 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 bypass the DVI/PS step and go staight to PDF. This is done via the pdflatex
tool which is a LaTeX extension that provides PDF support. It's a topic in its own right and and such there is a
tutorial dedicated to it.
But as a brief introduction, to generate PDF directly simply run:
pdflatex hello
To learn more about LaTeX and the PDF format then jump ahead to 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.