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.

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
\documentclass[12pt,a4paper]{report}
\pagenumbering{roman}

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

\begin{document}

\maketitle

\begin{abstract}

\end{abstract}

\tableofcontents

\newpage

\section{Introduction}

This is a quick introduction to using Latex.

\subsection{Example}

Here is a subsection.

\section{Conclusion}

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

\end{document}

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) (./example.tex LaTeX2e <2001/06/01> Babel <v3.7h> and hyphenation patterns for american, french, german, ngerman, n ohyphenation, loaded. (/usr/share/texmf/tex/latex/base/report.cls Document Class: report 2001/04/21 v1.4e Standard LaTeX document class (/usr/share/texmf/tex/latex/base/size12.clo)) 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 example.ps
' TeX output 2004.12.16:1220' -> example.ps
$dvipdf example.dvi example.pdf$ ls *ps *pdf
example.pdf  example.ps
<texc.pro>. [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.

Solutions

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:

\newcount\c@HOUR
\newcount\c@FINALHOUR
\newcount\c@MINUTE
\newcount\c@HOURSINMINUTES
\newcount\@INTVAL

\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@HOURSINMINUTES=\c@HOUR\multiply\c@HOURSINMINUTES by 60\relax
\def\THEHOUR{\the\c@FINALHOUR}
\def\THEMINUTES{\the\c@MINUTE}

% time in HH:MM (24 hour clock)
\newcommand\rightnow{\twodigit{\THEHOUR}:\twodigit{\THEMINUTES}}

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.