LaTeX

From Elvanör's Technical Wiki
Jump to navigation Jump to search

LaTeX is a fantastic typesetting package. Unfortunately it has a big problem: not a lot useful documentation. So this is a collection of useful tips I have learnt while spending a lot of time googling for LaTeX documentation.

Documentation

  • I found the following tutorials, books or cheatsheets useful:
    • The Not So Short Introduction to LaTeX 2e (general introduction).
    • TeX for the impatient (more complete book).
    • Latex cheat sheet (very useful list of commands).
  • They are free and available online.

Good resource site.

Basic Concepts

  • In LaTeX there are environments (started with the \begin command) and commands. These are separate concepts although sometimes they seem to overlap. For example you create a tabular environment but you give a command to generate a paragraph box (\parbox).
  • Sometimes you have to protect commands with the \protect command (which will apply to the next command). This happens when you have commands inside a caption for example. Note that the \\ (for a line break) also have to be protected.
  • References are defined via the \label command and used via the \ref command. If you plan to reference a section, use \label{sec:mysection}. For a table, use \label{tab:mytable}. Be careful though, for a table *all* \label commands must occur after the \caption command.

Packages

  • The url package allows you to output URLs in a better looking way. Just use the \url{} command.
  • The changepage package (previously chngpage) allows you to change the width of the page momentarily, thus reducing (or increasing) margins.
  • The graphicx package seems necessary to deal with images, import it like this:
\usepackage[pdftex]{graphicx}
  • The enumitem package allows you to create custome lists. In particular you can remove the indent produced by a normal list (which is useful for example in a table).
  • To write a document in French, use the following code:
\usepackage[francais]{babel}
  • You can change the name given to the table captions by using the command:
\renewcommand{\tablename}{T\scriptsize{ABLEAU}\normalsize}
  • The package a4wide makes horizontal margins smaller. It can be useful for some types of documents.

Conflicts

  • The enumitem package conflicts with [francais]{babel}. You must use the latest frenchb.ldf file (the one in TexLive 2007, installed by Gentoo, is not new enough), and place \usepackage{enumitem} after \usepackage[francais]{babel}. I also had to use the following command:
\frenchbsetup{ReduceListSpacing=false,CompactItemize=false}

which will negate the effects of French Babel on item lists.

Environments

titlepage

  • This environment allows you to write whatever you want as a cover page for the title.

Tables and Boxes

  • You can generate a tabular environment with \begin{tabular}. You give the definitions of the columns in the mandatory argument. Note that borders are created only if you enter "|" characters in the column definition area.
  • The tabular is usually included in a floating table environment. So the code would be like:
\begin{table}[h!]
\begin{tabular}{|p{4cm}|c|l|}
 ...
\end{tabular}
\end{table}
  • You cannot use a footnote inside a table. The best alternative is to use the threeparttable package, with the \tnote command and tablenotes environment.
  • The command \setlength{\extrarowheight}{0.2cm} allows you to set additional height for the table rows (only at the top though...). It needs the array package.
  • To change the normal alignment of cells for the table headers, you can use the \multicolumn command like this:
\begin{tabular}{|c|r|r|r|r|r|}
\hline
Année & \multicolumn{1}{c|}{1} & \multicolumn{1}{c|}{2} &
\multicolumn{1}{c|}{3} & \multicolumn{1}{c|}{4} & \multicolumn{1}{c|}{5}\\
\hline
  • To add a partial horizontal line, use \cline{i-j}, where i and j are the column numbers (i may be equal to j).
  • Use \rule{0pt}{0.7cm} to add a vertical margin to a single row (here it would add 0.7cm).

Bibliography

  • A simple bibliography can be produced with the environment thebibliography. Its usage is documented well in The Not So Short Introduction to LaTeX 2e.
  • For larger projects, you may need to use BibTeX.

Techniques

Choosing a document class

  • The letter class is not recommended, because it does not allow you to use sections, subsections, etc. The article class can be used as a general purpose class, it's not mandatory to have a title, abstract or bibliography.
  • Using \maketitle usually inserts a date, but you can get the date to be blank if you use \date{}.

Spacing and Page breaks

  • Use \pagebreak to make a page break.
  • Use \noindent to avoid the indentation of a paragraph.
  • Use the following code to make a perfectly blank page (useful on a PDF for the cover page, when printed in duplex):
\pagebreak
\thispagestyle{empty}
\addtocounter{page}{-1}
\hrule height 0pt
\pagebreak
  • Use \vspace (LaTeX command) rather than \vskip (TeX primitive). If you use \vspace at the top of the page, it seems you must use \vspace*{4cm}.
  • Use ~ to force a non-breaking space. If you just need a space after for example the \ldots command, you may just use \ldots\ or \ldots{} .

Symbols

  • To use the Euro symbol, use \usepackage[official]{eurosym}. You can then write the symbol with the command \euro, or use \EUR{50} to directly write a monetary amount.
  • To add a bullet symbol without using \begin{itemize}, use \bullet. This can be useful in tabulars for example (in this case write $\bullet$). You may want to add \, after \bullet to produce the horizontal space normally automatically added when using {itemize}.

Font size and style

  • Use the commands \tiny, \large, \Large, \huge to change the font size.
  • Use {\bf } for boldface, {\it } for italics. However, a more modern and better way would be to use \textbf{} or \textit{}. Note that you can also use \emph{}.

Alignment

  • By default LaTeX justifies a paragraph. Should you need to have it aligned use:
\begin{flushright}
This is some text.
\end{flushright}
  • There is also a \begin{flushleft} environment, of course. To center a block, use the \begin{center} command.
  • You can align a section title like this:
\section*{\begin{center}STATUTS\end{center}}
  • \begin{flushright} starts a new paragraph. Sometimes you don't want that happening, in which case you can use the \raggingleft command which does the same thing (warning: the left/right arguments are inversed from flush).

Tables and Boxes

  • The \parbox command allows you to create a paragraph box (eg, it will be in paragraph mode, which means its content can span several lines). This command takes a mandatory width argument, and an optional one controlling the vertical alignment of the contents.

Beamer

  • Beamer is a LaTeX package allowing you to create powerful presentations. It will output PDF files.
  • You create slides by using the \frame command. You should use the normal \section and \subsection commands which will create a normal table of contents. In some styles, the table of contents, or the current section / subsection will be displayed in all slides (in a left frame, or at the top, for example).
  • The style can be very easily changed globally with a single command
\usetheme{Frankfurt}
  • To pause the presentation, use the \pause command. This will actually create a new page in the resulting PDF.
  • To highlight some important text, you can use \alert{}.
  • You can use \begin{block} or \begin{beamerboxesrounded}{} to create nice frames around some blocks of text. If you use \begin{beamerboxesrounded}{}, don't forget the empty {} (for a non-existent title).

Tools

Viewer

  • My favorite viewer when writing LaTeX files is kpdf. It automatically reloads changes to the underlying document which is very nice.

LaTeX Distribution

  • As of 2008, the best available distribution for Linux/UNIX seems to be TexLive (version 2007, 2008 is not released yet). It has a stable ebuild for Gentoo Linux (the old tetex distribution is now obsolete).

Eclipse Support (via Texlipse)

  • The support is good overall. It allows you to mix freely *.tex files within another project (with PHP files for example). It will configure a LaTeX builder for the project, which will build your document. However, it is intended that only one document is created (although you can combine multiple source TeX files to create it).
  • Check the project properties to define which is the main LaTeX source file and which document will be built. You can also define what program will be used for the build (pdflatex etc).

Code Examples

  • Two columns layout:
\begin{tabular}[t]{lr}
\parbox[t]{5cm}{11 bis, rue de Genève\\
91140 Villebon-sur-Yvette\\
France\\

(33) 09.52.29.23.58 \\
(33) 06.28.33.13.68}
& \parbox[t]{7cm}
{
	\raggedleft
    \url{jean-noel.rivasseau@polytechnique.org}\\
    \url{http://www.elvanor.net/}\\
	\url{http://www.shoopz.com/}
}
\end{tabular}
  • Inclusion of an image, while reducing the page's margins for this image (you need to import changepage package):
\begin{figure}[t]
\begin{adjustwidth}{-4cm}{-4cm}
\begin{center}
\includegraphics[width=18cm]{kameleon-1}
\end{center}
\end{adjustwidth}
\caption{Write your caption here.}
\end{figure}
  • Use of threeparttable package:
\begin{table}
\begin{adjustwidth}{-2cm}{-2cm}
\begin{center}
\begin{threeparttable}
\begin{tabular}{|p{4cm}|p{4cm}|p{6cm}|}
\hline
{\bf Description} & {\bf Examples} & {\bf Intérêt}\\
\hline
Réseaux sociaux & Facebook, LinkedIn, Vidaeo, Odnoklassniki & Aucun\tnote{1}\\
\hline
\end{tabular}
\begin{tablenotes}
\item [1] Voici un semblant de footnote.
\end{tablenotes}
\end{threeparttable}
\end{center}
\end{adjustwidth}
\begin{center}
\caption{Troisième segment
\end{center}

Issues

  • If you include a .png file and compile with pdf2latex, the resulting PDF file will be perfect under kpdf but NOT on Adobe Acrobat Reader (version 8). This is probably a bug in Reader, however this does not happen with .jpg files so if you need high quality, JPEF should be used.