LaTeX

Philosophy

LaTeX is a document preparation system, TeX is the underlying typesetting system. In contrast to WYSIWYG-systems (Word, OpenOffice-Writer, etc.) one does not immediately see on the screen, how the document will look, e.g. when printed. Rather, LaTeX works like a compiler, which takes a .tex-file as input and generates a .dvi-file. This dvi-file can then be

  • previewed (by e.g. kdvi, xdvi, etc. on UNIX-system, by e.g. yap on Windows),
  • converted to PostScript (dvips),
  • converted to PDF (dvipdf),
  • converted to HTML (latex2html),
  • printed (dvips), etc.

(For producing a PDF finally, an alternative is also "pdflatex", which produces pdf directly from the LaTeX-source without going through an intermediate dvi. Which is better depends on the situation.)

"Writing a LaTeX document" in this sense means "writing the tex-file and compiling it".

The LaTeX-source code contains (of course) the text content of the document + commands that instruct the compiler how to typeset the document. The LaTeX-source can be written in any text editor you like (emacs, vi, wordpad, Windows text editor, etc.) but we recommend to use some "programming environment" for LaTeX, like "Kile" (Linux) or "TeXnicCenter" (Windows), which make the generation of LaTeX-source much more convenient. From now on we will sometimes refer to Kile, but TeXnicCenter is basically equivalent.

Open a new document in Kile and choose e.g. "Article". You will get an "empty document" looking something like

\documentclass[...]{article}
...
\begin{document}
...
\end{document}

The "\" and the braces "{}" have a special meaning in LaTeX, every command starts with "\", every parameter to a command is written in braces. The part between \documentclass and \begin{document} is called the preamble. (If you read some documentation, you will learn that certain commands may only occur in the preamble (e.g. definition of new commands, see below).

When using Kile, you will find menus and toolbar buttons that help you with all the commands that are described in the sequel. Moreover, when going through the LaTeX-menu in Kile, you get an overview of the commands available in LaTeX, we cannot describe all of them in this short overview.

Please consider this document as containing pointers to some of the interesting concepts of LaTeX, for details find a LaTeX manual in the library or some online resources explaining LaTeX commands in detail.

Structuring the Document

When writing a document, concentrate on the structure, not on the final layout (this is a two-step process). LaTeX commands help in defining a good structure. On the coarsest level, a document is structured in sections (\section{name}), subsections (\subsection{name}), and subsubsections (\susubsection{name}), in documentclass report and book you even have \chapter. The text in each (sub)(sub)section is subdivided into paragraphs, which are indicated in the LaTeX-source by a blank line.

Some characters may not be used in text, because of their special meaning for the compiler (\, {, }, % (comment), $ (math, see below), and several more). Most of them can be generated by preceding them with a \, e.g. \$ if you need the dollar sign in your text. For simulating keyboard input (like when writing program code) consider \begin{verbatim} ... \end{verbatim}.

Enumerations are done with
\begin{enumerate}
\item ...
\end{enumerate}
lists without numbering with
\begin{itemize}
\item ...
\end{itemize}

Writing Mathematics

There are basically two different forms, how mathematics can appear in a document:

  1. inline formula: when a small piece of mathematics is in the running text and
  2. displayed formula: a formula on a line by itself.

Inline formulae are enclosed in $-signs, special mathematical characters (e.g. summation sign, greek letters, etc.) and special mathematical constructs (e.g. a fraction or a square root, a binomial coefficient, a vector, etc.) can be generated by special commands inside math mode. Example: "We solve $ax^2+bx+c=0$ for $x$ and then ..." (Note: also an individual symbols is considered a "formula" therefore we write "solve for $x$" and not just "solve for x", try the difference!)

Displayed formulae are enclosed in \[ ... \] or \begin{displaymath}\end{displaymath}, inside the same commands can be used like in inline formulae. Example: "We solve \[ax^2+bx+c=0 \text{ for }x\] and then ..." (Note: small pieces of text in a displayed formula must be written in \text, otherwise the text is interpreted and typeset as a formula, try out the difference. Spaces must be included in \text, otherwise they are probably eliminated.)

There are many variants of displayed formulae depending on whether they are numbered (\begin{equation}), how they are aligned, etc. Keyword: \usepackage{amsmath}.

Cross-Referencing

Whereever LaTeX generates number automatically (\section, \subsection, \begin{equation}, etc.) you can use "\label{lab}" to set a label refering to this number. When you want to refer to this number somewhere else in your document, just write "\ref{lab}" and LaTeX will substitute always the correct number (very helpful, in particular, since these numbers usually change while developing a document ...). Checkout also \pageref and \eqref, the latter for refering to equation numbers.

Tables and Arrays

Tables are produced with \begin{tabular} and are used for tabular arrangements within text, arrays produced with \begin{array} are basically the same but are used in math mode. Enclosing a tabular in \begin{table} \end{table} produces a so-called floating body that will be placed on an appropriate place in the document, not necessarily exactly at the position where \begin{table} is written in the source.

Boxes

When LaTeX typesets text, it divides the text into boxes and then arranges the boxes accordingly. Sometimes when you need to position items in a particular way, you need to use boxes also.

  • \mbox{text} puts text in a box avoiding linebreaks (can be used to suppress linebreaks).
  • \parbox{3cm}{text} puts text into a box of 3cm width and automatic linebreaks. Some commands are not allowed to occur in text.
  • \begin{minipage}{3cm} text \end{minipage} is like \parbox, only that there are no restrictions on text.

Including Graphics / Images

Load the package "graphicx" to have the command \includegraphics{file} to include a graphic stored in file. Usually you need not specify the file suffix, and LaTeX will always search for file.eps, i.e. you must have the graphics in encapsulated PostScript in order to include it into your LaTeX document. When using pdflatex, the format must be jpg, pdf, or png (read documentation for other formats may be available).

Remark: Of course most of the graphics formats can be converted but the decision between LaTeX and pdflatex may be influenced by the need to include graphics of a certain type.

LaTeX also has a picture-environment that recognizes certain commands to specify graphics. For elaborated graphics this mechanism is quite cumbersome. Note however, that some drawing programs (e.g. inkscape, xfig) allow to save/export drawings in LaTeX-format, i.e. as a LaTeX-picture essentially (maybe using some additional PostScript utilities lieke e.g. pstricks), which can  be included into the document (see "Splitting the Input" below).

A common problem with externally generated embedded graphics is that the font in the graphics differs from the font in the document and that LaTeX-formulae cannot be used in the graphics. One solution is given by the psfrag-package. Roughly, what you do is the following:

  1. Put keywords into the graphics in all places where you want LaTeX to interfere.
  2. Export the graphics as EPS.
  3. Use psfrag to substitute the keywords in the EPS by arbitrary LaTeX expressions when the EPS is included.

In order to make this work the keywords must be stored as plain text in the EPS, otherwise the substitution does not work. Some programs, however, convert text to curves (or something else) when exporting to EPS, sometimes this is a special option when saving as EPS, sometimes this cannot be prevented at all.

Makros

It is possible to define one's own commands in the preamble.

\newcommand{\setof}[2]{\left\{#1\;|\;#2\right\}}

would define a command \setof with 2 parameters, in the definition the parameters are referred to as #1 and #2. The command above can be used in math-mode (because its definition will expand into constructs available only in math-mode). Commands are helpful as abbreviations, but mostly they help in achieving uniformity in your document, plus they make it very easy to change notation easily (you only have to change the command definition and magically it will affect the entire document on next compilation.

Similar to \newcommand there is \newenvironment{name} to define a new environment to be used with \begin{name} ... \end{name}, see documentation.

Packages

A package is essentially a collection of macros in order to provide additional functionality for a special purpose. With

\usepackage{P}

in the preamble of the document you load package P. There are dozens of useful packages, just google ...

Splitting the Input

The LaTeX-source may be split across several files. Use \input{file} or \include{file} in order to input a file into another (you need not write the .tex-suffix).

Specialities

Table of contents, list of references, etc. can all be produced more or less automatically. Consider inquiring about BibTeX when it comes to citation of literature.

There is a special documentclass "beamer" that can be used to produce slideshow presentations, very powerful. We refer to the lecture on "presentations" later in this series for details.

Zuletzt geändert: Mittwoch, 28. September 2011, 17:41