This is a short worked example on how to write and build a LaTeX document. Note that this is not a primer on the language! You should get hold of the Not so short Introduction to Latex for a more complete introduction.

Writing your .tex file

Because this is just a trivial example on how to use LaTeX, not on how to actually write LaTeX documents, my example .tex file won't contain a lot. LaTeX can do plenty more than I'm demonstrating below -- make sure you read the better references mentioned above.

Edit a file called example.tex, and put the following text in it

\title{Latex Example}
\author{Daniel Lawson}








This is a quick introduction to using Latex.


Here is a subsection.


In conclusion, this example doesn't show a lot. Go read the Not So Short Introduction to \LaTeX for more information.


Building it

Seeing as we're not doing anything complicated with LaTeX, we only need to parse the file once. If you were doing more complicated things, such as including a bibliography, you'd need to run a few more programs. See LatexMakefiles on more tips for that.

Run latex example.tex in the directory you saved your example.tex in earlier.

$ latex example.tex
This is TeX, Version 3.14159 (Web2C 7.3.7)
LaTeX2e <2001/06/01>
Babel <v3.7h> and hyphenation patterns for american, french, german, ngerman, n
ohyphenation, loaded.
Document Class: report 2001/04/21 v1.4e Standard LaTeX document class
No file example.aux.
[1] [1]
No file example.toc.
[1] [2] (./example.aux) )
Output written on example.dvi (4 pages, 1180 bytes).
Transcript written on example.log.

This completed successfully, outputting some extra files created by LaTeX on the way -- the .aux and .toc files are used for internal references. The .dvi file is the first output file.

You can view this with a program called xdvi:

xdvi example.dvi

Making it useful

DVI is a Device Independent format, which in theory makes it really useful. However, it tends to be harder to view in Certain OperatingSystems. Instead, you should convert your .dvi to a PostScript or PDF file:

$ dvips example.dvi -o
This is dvips(k) 5.86e Copyright 2001 Radical Eye Software (
' TeX output 2004.12.16:1220' ->
$ dvipdf example.dvi example.pdf
$ ls *ps *pdf
<>. [1] [1] [1] [2]

You can now view these with a PostScript or PDF viewer, such as GhostView or AcrobatReader.

Dates and times

The LaTeX command \today prints the date that the document is processed in a format determined by your language settings. There is a time record, but this gives the number of minutes since midnight and this isn't particularly useful in itself.


There are a number of ways to include the current time in your document. In order of potential usefulness they are:

Using the datetime package

The datetime package provides commands for not only easily reformatting the date but for printing the time in several formats.

Inserting your own time calculation code in the document

The most satisfying approach for a coder. Programming in TeX is an interesting challenge. The following code (included in a LaTeX file preamble) can be used to print the time in 24-hour format:


\newcommand{\twodigit}[1]{\@INTVAL=#1\relax\ifnum\@INTVAL<10 0\fi\the\@INTVAL}

\c@FINALHOUR=\time\divide\c@FINALHOUR by 60\relax
\c@HOUR=\time\divide\c@HOUR by 60\relax
\c@MINUTE=\time\advance\c@MINUTE by -\c@HOURSINMINUTES\relax

% time in HH:MM (24 hour clock)

Anywhere in the document \rightnow may be used to print the time the document was assembled (or more correctly, the time on the system clock when the document assembly began).

Modifying the source .tex file when processing the file.

Calling an editor like awk or sed to pre-process the source file is potentially popular with command-line geeks, but is rather clunky and has the feeling of a kludge about it. Not recommended.