Professional Documents
Culture Documents
com/content/latex-made-easy
Listen to this: our Linux podcast kicks ass. Subscribe for free! Search
1 of 15 02/21/2011 05:28 PM
LaTeX made easy | TuxRadar Linux http://www.tuxradar.com/content/latex-made-easy
The basic idea underlying Tex and Latex is that authors of a document
should be concentrating on the structure of their work and leave the
presentation to designers, or in this case to the designer(s) of the
typesetting processor. So instead of choosing fonts and decoration of text
directly, you just identify it as a certain type of text (heading, list, diagram with
caption etc) and Latex will do the rest for you. It's incredibly powerful once
you get started.
\documentclass{article}
\title{A basic Latex document}
\author{Juliet Kemp}
\date{June 2008}
\begin{document}
\maketitle
Latex (pronounced lay-tech) is a document markup language and preparation
system, which uses the Tex typesetting language written by Donald E
Knuth. Tex is very low-level; Latex gives a more human-friendly way of
accessing it.
This is a second paragraph.
% This is a comment line which won't be typeset.
\end{document}
And this line here won't be typeset either.
So: first up is the preamble section - the part before the \begin{document}
line. This is where you put the document type (in this case, an article),
author, and date. The preamble is just setup information, and will not
appear on the page. The body of the document - the stuff that you want to
actually appear on the page - goes between the \begin{document} and
\end{document} lines.
The \maketitle command generates the title from the \title, \author, and \date
commands in the preamble. After that you get the text.
This can be useful for writing notes to yourself with no risk that they'll show
up in the finished article when you forget to remove them... Comments have
a % symbol at the start and are also ignored. You should have noticed that
Latex commands are all preceded by \. Some have arguments, which go
either in curly brackets (for obligatory arguments) or square brackets (for
optional arguments).
There are two ways to turn this into a document, both of which are run from
the CLI. The standard command is latex file.tex, which will produce a DVI
2 of 15 02/21/2011 05:28 PM
LaTeX made easy | TuxRadar Linux http://www.tuxradar.com/content/latex-made-easy
file, file.dvi, which you can check out with the viewer of your choice (Evince is
good). Alternatively, you can use pdflatex file.tex to produce a PDF. The
command-line output of latex file.tex is shown in the screenshot below.
If you get any errors in the output (they'll begin with !), hit X to get back to a
prompt. Unfortunately Latex error messages can be a little obscure, but the
line number gives you an area to start looking for problems. Common
problems include missing opening or closing brackets on a command
("Too many }'s" or "Runaway argument"), a missing \end{something}
command, or misspelled commands ("Undefined control sequence").
You may also see the warning "Overfull [or underfull] hbox" - this means that
a particular line can't be squashed or stretched enough to fill the line
correctly. Don't worry about this unless you're at final version stage, and
even then, eyeball the page before you decide to do anything. If it really does
look bad, you can force hyphenation in a particular word.
Part 2: Formatting
So, that's your first basic document. But you're going to want more
formatting than that, so let's look at other things you can do. From here on,
you should edit the document that you produced in the first part. Everything
in the code snippets given here will go in the document body, between the
\begin{document} and \end{document} tags, unless stated otherwise.
One useful option is to divide your document into sections, which as I'm
3 of 15 02/21/2011 05:28 PM
LaTeX made easy | TuxRadar Linux http://www.tuxradar.com/content/latex-made-easy
sure you're aware is good practice to avoid people's eyes (not to mention
brains) getting bored. For example:
\section{Latex Formatting}
There are different types of formatting available.
\subsection{Sections}
Like sections.
\subsubsection{Also sub-sub-sections}
There's a whole hierarchy going on here. It does have a limit, though.
Latex will then automatically produce appropriate heading styles for your
sections, as per the screenshot below. If you use the commands as above,
the sections will be numbered. To avoid using numbers, use \section*{} (
\subsection*{} etc) - I use this as default.
4 of 15 02/21/2011 05:28 PM
LaTeX made easy | TuxRadar Linux http://www.tuxradar.com/content/latex-made-easy
Section and sub-section headings - notice how the font size decreases the further
dow n the hierarchy you go.
Remember that every time you edit your source code file, you'll need to save
it and re-run latex file.tex before reloading it in your viewer. You can also
create lists, both ordered and unordered, as per the code sample, and
create definition lists, which have a slightly different structure. They're a bit
like a dictionary type list, where you provide a header and then an
explanation. Compare the code here with the screenshot, below, to see
what's going on.
\begin{itemize}
\item These list items
\item Will show up
\item With bullets.
\end{itemize}
\begin{enumerate}
\item These list items
\item Will show up
\item With numbers.
\end{enumerate}
\begin{description}
\item[LaTeX] Latex is the language.
\item[command] Commands structure what you can do with Latex.
\item[Package] Packages allow you to add extra commands.
\end{description}
5 of 15 02/21/2011 05:28 PM
LaTeX made easy | TuxRadar Linux http://www.tuxradar.com/content/latex-made-easy
You can get bulleted and numbered lists, as w ell as the less common
dictionary-style description lists.
The quotation environments will all indent the included text from the main
text body. The whole point of Latex is to go easy on the explicit formatting -
let the program do the work. But sometimes you want italics - try
\emph{italics}. Or bold type - \textbf{bold face}. Or Courier-style text -
\texttt{like this}.
6 of 15 02/21/2011 05:28 PM
LaTeX made easy | TuxRadar Linux http://www.tuxradar.com/content/latex-made-easy
Paragraphs
Latex treats an empty line as indicating a paragraph break, but this doesn't
mean that you get a blank line between paragraphs in your output file -
books don't do this (they use indents instead), so Latex by default doesn't
either. However, this behaviour can be customised. If you want 1cm
between paragraphs, put the following line in your preamble (not in the
main document environment!):
\setlength{\parskip}{1cm}
You can also alter the paragraph indent, with:
\setlength{\parindent}{1cm}
To turn it off entirely, use a zero value, but be warned that if you have no
paragraph indents and no paragraph spacing, it won't be clear where your
paragraphs are!
Latex takes measurements in cm, mm, inches, ex (the height of a typeset x),
em (the width of a typeset M), points and bp (big points, which are very
slightly larger than a regular point). I quite like ex and em if I'm going to be
messing around with font size, because they will change along with the font
size.
Part 3: Pictures
Sometimes, words alone will not suffice, which is why LXF is blessed with
pretty pictures. Latex supports adding pictures, although unfortunately the
process can be a bit of a nuisance.
What you'll end up with in your Latex source will look a bit like this:
First, you need to include the graphicx package, which will do the picture-
wrangling for you. This should be standard in your Latex installation.
A quick note here on packages. Packages are a way of extending the basic
Latex commands. You can define your own commands and package them
yourself, if you fancy a little wheel-reinvention, or you can use the numerous
packages that other people have already created, most of which are
available via CTAN (the Comprehensive Tex Archive Network). Once you've
put the package up in the preamble, you can access the commands from
that package in the rest of the document. It's dead easy.
OK, back to adding our picture. In the body of the document, the figure
7 of 15 02/21/2011 05:28 PM
LaTeX made easy | TuxRadar Linux http://www.tuxradar.com/content/latex-made-easy
However, if you really can't relax, or if you have a very figure-heavy document
which confuses Latex, you can add [tbh] to the figure environment, like this:
\begin{figure}[tbh]
This tells Latex to place the figure at the top (t) or bottom (b) of a page, or
here (h) - that is, where the figure is in the source code. A more emphatic
way of doing this is to use [tbh!], but you should in general be OK just letting
Latex do its thing.
You include the picture itself with the \includegraphics command. It needs to
be in EPS (Encapsulated PostScript) format (this would be one of the
sources of mild nuisance alluded to at the start of this section). Gimp will
generate EPS files for you from your JPG or PNG files, but they may not be
great quality; Inkscape, OpenOffice.org Draw or another vector editor
program may be better bets. EPS files are pretty much self-contained,
making them much easier to place inside another PS (or PDF) document,
which is why they're used here.
\usepackage{xspace}
\newcommand{\lf}{Linux Format}
8 of 15 02/21/2011 05:28 PM
LaTeX made easy | TuxRadar Linux http://www.tuxradar.com/content/latex-made-easy
The picture at Figure \ref{tree} shows how very useful this all is.
Note that you need to run the latex file.tex command twice to get the
references correct - on the first run you'll get a warning about undefined
references.
\usepackage{setspace}
\setlength{\parskip}{1ex}
\setlength{\topmargin}{0in}
\setlength{\oddsidemargin}{0in}
\setlength{\evensidemargin}{0in}
\setlength{\textheight}{9in}
\setlength{\textwidth}{6.25in}
\setlength{\headwidth}{6.25in}
9 of 15 02/21/2011 05:28 PM
LaTeX made easy | TuxRadar Linux http://www.tuxradar.com/content/latex-made-easy
An example Latex document - note the header and v ery w ide margins!
\documentclass{article}
10 of 15 02/21/2011 05:28 PM
LaTeX made easy | TuxRadar Linux http://www.tuxradar.com/content/latex-made-easy
You can also set up different types of document. The main types are as
follows:
\documentclass{letter}
\signature{Juliet Kemp}
\address{My House \\ London }
\begin{document}
\begin{letter}{Ms Smith \\ Their House \\ London }
\opening{Dear Ms Smith,}
I wondered if you might be interested in a bridge I have to sell. Light use,
one careful owner, all offers will be considered.
Please do get in touch.
\closing{Yours sincerely,}
\ps{P.S. I have enclosed the particulars of the bridge. As you can see, it
goes all the way over the river at both ends.}
\encl{enclosure.txt}
\end{letter}
\end{document}
Latex may initially seem a little awkward to write with, but you get used
11 of 15 02/21/2011 05:28 PM
LaTeX made easy | TuxRadar Linux http://www.tuxradar.com/content/latex-made-easy
Going further
Latex has many other exciting options. It handles maths
and equations particularly well, courtesy of the built-in
maths mode.
Bibtex (which comes by default with a standard Latex
install) is a bibliography-handling system which is very
useful if you're doing anything which utilises citations.
CTAN, the Comprehensive Tex Archive Network
(www.ctan.org) has an enormous directory of Tex
packages available for download. There are packages for
working with fonts, graphics, languages, indices, and
assorted other bits and bobs, making it well worth a
browse even if you're just curious.
Of course, if you can't find anything that does exactly what
you're after, you can create your own packages.
12 of 15 02/21/2011 05:28 PM
LaTeX made easy | TuxRadar Linux http://www.tuxradar.com/content/latex-made-easy
Your comments
LaTeX Editor
kdnewton (not verified) - April 4, 2009 @ 3:27am
I like to use Kile as my LaTeX editor of choice. It's got buttons to easiyl compile the
source direct to PS or PDF. It will give warnings for errors as well.
TeXmaker
Morten Juhl-Johansen Zölde-Fejér (mjjzf) (not verified) - April 16, 2009 @ 12:44pm
I used TeXmaker - pretty much the same as Kile, but only QT4-based. Still, I also
liked working with LyX for semi-WYSIWYG editing. I found TeXmacs annoying, but I
greatly respect their philosophy that a mathematical result or proof achieved with
the aid of a computer can only be accepted if the application is open source:
http://www.texmacs.org/tmweb/about/philosophy.en.html
Thanks!!
Stan (not verified) - July 22, 2009 @ 2:28am
Thank you very much for making this (though I may just be a year late ;) .)!!
Too often tutorials are made without being brief and introductory. I love articles on
TuxRadar like this, because it starts off just right and keeps the reader interested
and trying new things. :)
In particular, a list of mobile phones that work successfully with linux would be
useful...
13 of 15 02/21/2011 05:28 PM
LaTeX made easy | TuxRadar Linux http://www.tuxradar.com/content/latex-made-easy
Title:
Comment: *
CAPTCHA
Fill in this captcha, or you shall be mocked mercilessly.
14 of 15 02/21/2011 05:28 PM
LaTeX made easy | TuxRadar Linux http://www.tuxradar.com/content/latex-made-easy
15 of 15 02/21/2011 05:28 PM