%%%%%% The class of document
\documentclass[11pt]{article}
%%%%%% Choosing the layout
\usepackage[a4paper]{geometry}
%%%%%% Choosing the language (for general typographic conventions)
\usepackage[francais,english]{babel}
\usepackage[latin1]{inputenc}
%%%%%% Mathematics
\usepackage{amsmath,amsthm,amssymb,mathtools}
\theoremstyle{break}
\newtheorem{theorem}{Theorem}
\newtheorem{lemma}{Lemma}
\newtheorem{proposition}{Proposition}
\newtheorem{corollary}{Corollary}
\theoremstyle{definition}
\newtheorem{definition}{Definition}
\newtheorem{example}{Example}
\newtheorem{exercise}{Exercise}
%%%%%% Tables
\usepackage{booktabs}
%%%%%% Graphics
\usepackage{graphicx,rotating}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows}
\usetikzlibrary{automata}
\usetikzlibrary{shapes}
%%%%%% Bibliography
\usepackage[round]{natbib}
\bibliographystyle{plainnat}
%%%%%% Index
\usepackage{makeidx,multicol}
\makeindex
\makeatletter
\renewenvironment{theindex}
  {\parindent\z@
    \parskip\z@ \@plus .3\p@
    \let\item\@idxitem}
  {}
\makeatother
%%%%%% Framed
\usepackage{framed}
\definecolor{shadecolor}{rgb}{.9,.9,.9}
\newenvironment{outex}%
  {\begin{framed}\noindent\footnotesize}%
  {\end{framed}}
%%%%%% Algorithms and code
% \usepackage[linesnumbered]{algorithm2e}
\usepackage{listings}
\lstloadlanguages{[LaTeX]TeX}
\lstset{%
  language=[LaTeX]TeX,%
  numbers=left,%
  numberstyle=\tiny\color{black!30},%
  basicstyle=\footnotesize,%
  %showspaces=true,%
  columns=fullflexible,%
  gobble=2,%
  backgroundcolor=\color{black!5}%
}
%%%%%% Page headers/footers
\usepackage{fancyhdr}\pagestyle{fancy}
\lhead{ENS Cachan}                     % left header
\chead{\LaTeX\ for Beginners}          % center header
\rhead{February 1, 2012}               % right header
\cfoot{\thepage}                       % footer
%%%%%% URLs and hyperlinks
\usepackage{url}
\usepackage{hyperref}
\hypersetup{pdftitle={LaTeX for Beginners},pdfauthor={Sylvain Schmitz}}
\renewcommand{\sectionautorefname}{Section}
\renewcommand{\subsectionautorefname}{Section}
\renewcommand{\subsubsectionautorefname}{Section}
%%%%%% Home-made macros
\usepackage{ifthen,xifthen}
\newcommand{\XeTeX}%
  {X\hspace{1.2ex}\raisebox{1.1ex}{\turnbox{180}{E}}\hspace{-.4ex}\TeX}
\newcommand{\ConTeXt}%
  {\textsc{Con\hspace{-.2ex}\TeX{}\hspace{-.4ex}t}}
\newcommand{\BibTeX}%
  {Bib\hspace{-.2ex}\TeX}
\renewcommand{\title}[1]{%
  \begin{center}
    {\Large\bfseries #1}
  \end{center}
  \vspace{1em}\par
}
\newcommand{\pkglink}[1]%
  {``\href{http://tug.ctan.org/pkg/#1}{\lstinline!#1!}\index{packages!#1}''}
\newcommand{\cmdidx}[1]{\index{commands!\textbackslash\textbf{#1}}}
\newcommand{\cmdsymb}[2][]
 {\ifthenelse{\isempty{#1}}
   {\index{symbols!#2@\textbackslash\textbf{#2} ($\protect\csname #2\endcsname$)}}
   {\index{symbols!#2@\textbackslash\textbf{#2} ($\protect\csname #2\endcsname #1$)}}}
\newcommand{\cmdlink}[2][]%
  {``{\footnotesize\textbackslash\textbf{#2}%
    \ifthenelse{\isempty{#1}}{}{\{#1\}}}%
    \cmdidx{#2}''}
\newcommand{\envlink}[1]%
  {``\lstinline!#1!\index{environments!\lstinline?#1?}''}
%%%%%% Document
\begin{document}
\selectlanguage{english}
  
\title{\LaTeX\ for Beginners}

This short document should help you get started with \LaTeX.  You can
also find a lot of documentation on the web, for instance
\url{http://en.wikibooks.org/wiki/LaTeX}, and of course the full
documentation of packages on CTAN\index{CTAN} \url{http://ctan.org/}.

\section{Installing a \LaTeX\ Distribution}

The \TeX\ ``environment'' is a pretty large one, including processing
programs, libraries, fonts, and many extensions.

\subsection{Installing a Distribution}

The most comprehensive distribution is \TeX\ Live\index{Tex Live@\TeX\ Live}
\url{http://tug.org/texlive/}, and can be installed on your favorite
operating system:
\begin{description}
\item[GNU/Linux] preferably use the package manager of your GNU/Linux
  distribution to install \TeX\ Live,
\item[MacOS X] use Mac\TeX\ \url{http://tug.org/mactex/}
  or your package manager (for instance MacPorts
  \url{http://macports.org}),
\item[Microsoft Windows] use the \TeX\ Live distribution for Windows
  or MiK\TeX\index{MiKTeX@MiK\TeX} \url{http://miktex.org/}.
\end{description}
Unless you are short on disk space, you can start right away with
a full installation (which might use several GiBs!).

\subsection{Updating a Distribution}

If you installed your \TeX\ distribution through a general-purpose
package manager, use that.  If you installed \TeX\ Live directly, then
you can maintain your \TeX\ installation through the \emph{\TeX\ Live
  package manager} \texttt{tlmgr}\index{tools!tlmgr@\texttt{tlmgr}}: see
\url{http://www.tug.org/texlive/tlmgr.html}.  If you installed
MiK\TeX, there is a \emph{MiK\TeX\ update wizard} tool to maintain
your installation: see
\url{http://docs.miktex.org/2.9/manual/updating.html}.

\section{Running \LaTeX}

A \LaTeX\ document is a plain text document.  It is interpreted by a
\TeX\ processor that outputs a formatted document (here a \textsc{pdf}
file).

\subsection{The Basics}

\begin{example}[A Minimal Document]
  A minimal \LaTeX\ document looks like the following:
  \begin{lstlisting}[gobble=4]
    \documentclass{article}
    \begin{document}
      Hello, world!
    \end{document}
  \end{lstlisting}
  The \cmdlink[article]{documentclass} instruction selects the type of
  document we want to generate (here an
  ``\lstinline{article}\index{document classes!article}''), while the \envlink{document} environment
  delimits the main contents of our document.

  If this is the content of a file named \texttt{example1.tex}, then
  running\index{tools!pdflatex@\texttt{pdflatex}}
  \begin{lstlisting}%
    [language=,gobble=4,basicstyle=\footnotesize\ttfamily,numbers=none]
    # pdflatex example1.tex
  \end{lstlisting}
  on the command line will result in a \textsc{PDF} file
  \texttt{example1.pdf} (among others).
\end{example}

\subsubsection{Basic Typesetting}

Paragraphs are simply separated by blank lines:
\begin{lstlisting}
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut molestie
  nunc ac erat consectetur scelerisque. Lorem ipsum dolor sit amet,
  consectetur adipiscing elit. Sed ante velit, aliquam at faucibus
  sed, varius in metus. Curabitur ut libero et ligula mollis fermentum
  a sit amet urna. Etiam eu sapien leo, ac lobortis odio. Donec
  tempor, magna vel lobortis blandit, quam mi fringilla neque, nec
  sagittis nisi leo eu tortor. Nunc rhoncus urna a lectus auctor eu
  fringilla nulla accumsan.

  Aliquam erat volutpat. Vivamus euismod consectetur
  ultricies. Maecenas vel sem sed lectus consequat vulputate. Etiam in
  orci massa. Sed eros nisl, aliquam vitae pulvinar nec, lobortis sit
  amet nisl. Donec metus leo, accumsan et ultricies vel, elementum id
  leo. Donec tempus nibh sed est interdum cursus.
\end{lstlisting}
will result in two paragraphs:
\begin{outex}%
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut molestie
  nunc ac erat consectetur scelerisque. Lorem ipsum dolor sit amet,
  consectetur adipiscing elit. Sed ante velit, aliquam at faucibus
  sed, varius in metus. Curabitur ut libero et ligula mollis fermentum
  a sit amet urna. Etiam eu sapien leo, ac lobortis odio. Donec
  tempor, magna vel lobortis blandit, quam mi fringilla neque, nec
  sagittis nisi leo eu tortor. Nunc rhoncus urna a lectus auctor eu
  fringilla nulla accumsan.

  Aliquam erat volutpat. Vivamus euismod consectetur
  ultricies. Maecenas vel sem sed lectus consequat vulputate. Etiam in
  orci massa. Sed eros nisl, aliquam vitae pulvinar nec, lobortis sit
  amet nisl. Donec metus leo, accumsan et ultricies vel, elementum id
  leo. Donec tempus nibh sed est interdum cursus.
\end{outex}
Paragraph jumps can also be triggered through the \cmdlink{par}
directive.

\LaTeX\ handles rather well line breaks, page breaks, hyphenation,
etc., thus there is usually no need to use the commands
``\lstinline!\\!'', ``\lstinline!\newline!'', or
``\lstinline!\linebreak!'' for line breaks, ``\lstinline!\clearpage!''
or ``\lstinline!\pagebreak!'' for page breaks, nor ``\lstinline!\-!''
for hyphenations.

Text styles, to be used sparingly in the main text, are set through
the commands \cmdlink[...]{textbf} for \textbf{bold face},
\cmdlink[...]{textit} for \textit{italics},
\cmdlink[...]{textsl} for \textsl{slanted},
\cmdlink[...]{textsf} for \textsf{sans-serif},
\cmdlink[...]{texttt} for \texttt{typewriter}, and
\cmdlink[...]{textsc} for \textsc{Small Caps}.  Much more
useful: the \emph{emphasis} \cmdlink[...]{emph}, which
emphasizes depending on the surrounding font.

Finally, a number of symbols are reserved and should be escaped or
replaced: ``\lstinline!\%!'' because ``\lstinline!%!'' is used to
start a comment line, ``\lstinline!\$!'' because ``\lstinline!$!'' %$
delimits inline maths, ``\lstinline!\~{}!'' because ``\lstinline!~!''
denotes an unbreakable space, ``\lstinline!\#!'', ``\lstinline!\_!'',
``\lstinline!\&!'', ``\lstinline!\{!'', ``\lstinline!\}!'', and
\cmdlink{textbackslash} (or \cmdsymb{backslash} in math mode) for
``\lstinline!\!'', and
``\lstinline!\textasciicircum{}!''\cmdidx{textasciicircum}
(or ``\lstinline!\hat{}!''\cmdsymb[~]{hat} in math
mode) for ``\lstinline!^!''.


\subsubsection{Sectioning}

The text can be sectioned into parts, chapters etc.\ using
\cmdlink[Title of the Part]{part}, \cmdlink[Title of the
  Chapter]{chapter} (parts and chapters are not available in the
``\lstinline{article}''\index{document classes!article} class of
documents; one needs to use e.g.\ ``\lstinline{book}''\index{document classes!book} or ``\lstinline{report}''\index{document classes!report}), \cmdlink[Title of the Section]{section},
\cmdlink[...]{subsection}, \cmdlink[...]{subsubsection},
\cmdlink[...]{paragraph}, and \cmdlink[...]{subparagraph}.  Appendices
start after the \cmdlink{appendix} command.

A table of contents is automatically inserted by the
\cmdlink{tableofcontents} command; this requires \emph{two}
compilations by the
\texttt{pdflatex}\index{tools!pdflatex@\texttt{pdflatex}} program: a
first pass generates an auxiliary file which is read during the second
pass to construct the table.

Here is for instance the table of contents for this document:
\begin{outex}\vspace{-2em}
  \tableofcontents
\end{outex}

\subsubsection{Lists}

\begin{example}[Lists]
  \LaTeX\ provides three predefined list environments:
  \envlink{itemize}, \envlink{enumerate}, and
  \envlink{description}:\cmdidx{item}
  \begin{lstlisting}[gobble=4]
    \begin{description}
    \item[itemize] lists are used for generic, ``bulleted lists'':
      \begin{itemize}
      \item something
        \begin{itemize}
        \item that we can refine into something else
        \item or yet another thing
        \end{itemize}
      \item something different.
      \end{itemize}
    \item[enumerate] lists are for numbered lists:
      \begin{enumerate}
      \item a first point
        \begin{enumerate}
        \item a first subpoint
        \item a second subpoint
        \end{enumerate}
      \item a second point.
      \end{enumerate}
    \item[description] lists are for lists of entries along with their
      descriptions.
    \end{description}
  \end{lstlisting}
  results in 
  \begin{outex}
    \begin{description}
    \item[itemize] lists are used for generic, ``bulleted lists'':
      \begin{itemize}
      \item something
        \begin{itemize}
        \item that we can refine into something else
        \item or yet another thing
        \end{itemize}
      \item something different.
      \end{itemize}
    \item[enumerate] lists are for numbered lists:
      \begin{enumerate}
      \item a first point
        \begin{enumerate}
        \item a first subpoint
        \item a second subpoint
        \end{enumerate}
      \item a second point.
      \end{enumerate}
    \item[description] lists are for lists of entries along with their
      descriptions.
    \end{description}
  \end{outex}
\end{example}

\subsubsection{Mathematics}

\paragraph{Math Mode}
\LaTeX\ has two main modes of interpretation: \emph{text mode} for
plain text, and \emph{math mode} for mathematics.  The latter can be
used \emph{inline} by delimiting with ``\lstinline!$...$!'', for
instance to write $(x+y)^n=\sum_{k=0}^n\binom nk
x^{n-k}y^k$\cmdsymb{sum}\cmdsymb[xy]{binom}, or as a \emph{block} using
``\lstinline!$$...$$!'' or ``\lstinline!\[...\]!'', as
in $$(x+y)^n=\sum_{k=0}^n\binom nk x^{n-k}y^k\;,$$ which was typeset by
\begin{lstlisting}[numbers=none]
  $$(x+y)^n=\sum_{k=0}^n\binom nk x^{n-k}y^k\;,$$
\end{lstlisting}
(the \cmdlink{binom} command requires \pkglink{amsmath}; see
\autoref{pack-math}).

\paragraph{Equations}
When writing maths in blocks, the environments proposed by
\AmS-\LaTeX\ are recommended instead of the plain
``\lstinline!$$...$$!''; for instance,\index{environments!\lstinline ?equation?}
\begin{lstlisting}
  \usepackage{amsmath}
  ...
  \begin{document}
  \begin{equation}
    (x+y)^n=\sum_{k=0}^n\binom nk x^{n-k}y^k\;.
  \end{equation}
  \end{document}
\end{lstlisting}
results in
\begin{framed}
  \begin{equation}
    (x+y)^n=\sum_{k=0}^n\binom nk x^{n-k}y^k\;.
  \end{equation}
\end{framed}
See \autoref{pack-math} for more on \AmS-\LaTeX.

\paragraph{Symbols}
\LaTeX\ comes with a lot of commands for mathematical symbols: see
\url{http://www.ctan.org/tex-archive/info/symbols/comprehensive/symbols-a4.pdf}
for a \emph{big} list.  Beware that latin characters in math mode are
interpreted as \emph{variables} and typeset accordingly;
contrast\index{environments!\lstinline ?align?}\cmdidx{mathit}\cmdidx{mathrm}
\cmdidx{mathop}\cmdsymb{in}
\begin{lstlisting}
  \begin{align}
    argmax_{i\in I} &\mathit{argmax}_{i\in I} &\mathop{\mathrm{argmax}}_{i\in I}
  \end{align}
\end{lstlisting}
\begin{framed}
  \begin{align}
    argmax_{i\in I}&&\mathit{argmax}_{i\in I}&&\mathop{\mathrm{argmax}}_{i\in I}
  \end{align}
\end{framed}

\subsubsection{Tables}

Tables tend to be annoying in \LaTeX.  The basic environment
\envlink{tabular} is rather poor, and one often has to resort to
the more advanced environments provided by the packages
\pkglink{array} (for fine-grained column specifications),
\pkglink{multirow} (for cells spanning multiple rows),
\pkglink{tabularx} (for extensible columns), and
\pkglink{longtable} (for tables spanning over several pages).

\begin{example}%[Table using \pkglink{booktabs}]
  The table layouts found in \LaTeX\ documents tend to overuse
  vertical lines, and to have improper spacing.  Here is an example of
  the nice results one obtains with the \pkglink{booktabs} package
  (see \autoref{sec-pack} on the use of packages):\cmdidx{toprule}\cmdidx{multicolumn}\cmdidx{cmidrule}\cmdidx{midrule}\cmdidx{bottomrule}\cmdsymb{langle}\cmdsymb{rangle}\cmdidx{mathcal}\cmdsymb{emptyset}\cmdidx{textsc}
  \begin{lstlisting}[gobble=4]
    \usepackage{booktabs}
    ...
    \begin{tabular}{lccc} % 4 columns: 1 left-aligned and 3 centered
      \toprule
      \multicolumn{3}{c}{Problems}\\
      \cmidrule(r){1-3}
      Name       & Input & Question & Complexity \\
      \midrule
      Membership & $\langle\mathcal{A},w\rangle$ &
        $w\in L(\mathcal{A})$? & \textsc{NLogSpace}-complete\\
      Emptiness  & $\langle\mathcal{A}\rangle$ &
        $L(\mathcal{A})=\emptyset$? & \textsc{NLogSpace}-complete\\
      Universality & $\langle\mathcal{A}\rangle$ &
        $L(\mathcal{A})=\Sigma^\ast$? & \textsc{PSpace}-complete\\
      \bottomrule
    \end{tabular}
  \end{lstlisting}
  results in
  \begin{outex}
    \begin{tabular}{lccc} % 4 columns: 1 left-aligned and 3 centered
      \toprule
      \multicolumn{3}{c}{Problems}\\
      \cmidrule(r){1-3}
      Name       & Input & Question & Complexity \\
      \midrule
      Membership & $\langle\mathcal{A},w\rangle$ &
        $w\in L(\mathcal{A})$? & \textsc{NLogSpace}-complete\\
      Emptiness  & $\langle\mathcal{A}\rangle$ &
        $L(\mathcal{A})=\emptyset$? & \textsc{NLogSpace}-complete\\
      Universality & $\langle\mathcal{A}\rangle$ &
        $L(\mathcal{A})=\Sigma^\ast$? & \textsc{PSpace}-complete\\
      \bottomrule
    \end{tabular}
  \end{outex}
\end{example}

\subsection{Further Tools}
\label{sub-tools}

\paragraph{More \TeX\ Dialects}
\LaTeX\ is by no means the only text processor built on top of \TeX.
One alternative is \ConTeXt\ \url{http://wiki.contextgarden.net/},
which emphasizes the ease of modifying the layout and typography, or
\XeTeX\ \url{http://scripts.sil.org/xetex}\index{tools!xelatex@\texttt{xelatex}},
which provides an improved support of fonts and typography.  Both are
distributed along with your \TeX\ environment.

\subsubsection{\BibTeX}

\LaTeX\ manages bibliographies as provided by a
\envlink{thebibliography} environment.  It is however much easier
to rely on \BibTeX\ to generate the contents of this environment,
based on the citations found in the main text.

\begin{example}[Bibliography]\label{ex-bib}
  A citation in the main text is triggered by the \cmdlink[key]{cite}
  command, where ``\lstinline!key!'' is the key of the bibliographic
  entry of interest.  When running
  \texttt{pdflatex}\index{tools!pdflatex@\texttt{pdflatex}},
  information about citations is saved to an \texttt{.aux} auxiliary
  file, which \BibTeX\ reads to generate a \texttt{.bbl} file
  containing the desired \envlink{thebibliography} environment.
  This file is read on a next pass of \texttt{pdflatex}, and one more
  pass is necessary before the links between the citations and the
  bibliography are made:\cmdidx{bibliographystyle}\cmdidx{bibliography}
  \begin{lstlisting}[gobble=4,title=Contents of \texttt{example2.tex}]
    \documentclass{article}
    \begin{document}
    See \cite{TeX,LaTeX}, but really look at \cite{latexcomp}.

    \bibliographystyle{plain}
    \bibliography{mybib}
    \end{document}
  \end{lstlisting}
  \begin{lstlisting}[language=,gobble=4,columns=fixed,title=Contents of \texttt{mybib.bib}]
    @book{TeX,
      author    = {Donald E. Knuth},
      title     = {The \TeX{}book},
      publisher = {Addison-Wesley},
      year      = 1984,
    }
    @book{LaTeX,
      author    = {Leslie Lamport},
      title     = {\LaTeX\string: {A} Document Preparation System},
      publisher = {Addison-Wesley},
      edition   = 2,
      year      = 1994,
    }
    @book{latexcomp,
      author    = {Frank Mittelbach and Michel Goossens},
      title     = {The \LaTeX\ Companion\string: Tools and Techniques
                   for Computer Typesetting},
      publisher = {Addison-Wesley},
      edition   = 2,
      year      = 2004,
    }
  \end{lstlisting}
  Then running\index{tools!bibtex@\texttt{bibtex}}
  \begin{lstlisting}[language=,gobble=4,basicstyle=\ttfamily\footnotesize]
    # pdflatex example2
    # bibtex example2
    # pdflatex example2
    # pdflatex example2
  \end{lstlisting}
  will result in:
  \begin{outex}%
    See [\citenum{TeXplain,LaTeXplain}], but really look at
    [\citenum{latexcompplain}].
    \input{plain}
  \end{outex}
\end{example}
\subsubsection{Indexing}
An index for the document can be generated automatically using the
\pkglink{makeidx} package.  The command \cmdlink{makeindex} must also
be used in the preamble of the document, and a command
\cmdlink[key]{index} each time an index entry should be created for
the provided \emph{key}.

\LaTeX\ then generates a \texttt{.idx} file, which is processed by the
\texttt{makeindex}\index{tools!makeindex@\texttt{makeindex}} program
to generate a \texttt{.ind} file, which is included in the document
thanks to the \cmdlink{printindex} command.

\begin{example}[Indexing]\hfill\cmdidx{emph}\cmdidx{usepackage}
  \begin{lstlisting}[gobble=4,title=Contents of \texttt{example3.tex}]
    \documentclass{article}
    \usepackage{makeidx}
    \makeindex
    \begin{document}
    Here is an \emph{entry}\index{entry}.
    
    \printindex
    \end{document}
  \end{lstlisting}
  can be processed by
  \begin{lstlisting}[language=,basicstyle=\ttfamily\footnotesize,numbers=none]
    # pdflatex example3
    # makeindex example3
    # pdflatex example3
  \end{lstlisting}
  It will not result in anything fancy; here is rather the index of
  this document:
  \begin{framed}\noindent
    %\begin{minipage}{-1em+\textwidth}
      \footnotesize
      \subsection*{\indexname}
      \begin{multicols}{3}
        \printindex
      \end{multicols}
    %\end{minipage}
  \end{framed}
\end{example}

\section{Packages}
\label{sec-pack}

There are many, \emph{many} packages available for \LaTeX, \BibTeX\ and other
\TeX -related software (the current file index of CTAN weights more than
10MiB).  \LaTeX\ packages gather sets of macro definitions and
redefine some of the \LaTeX\ internals.

\begin{example}[Loading Packages]
  A package a loaded through a command of the form
  ``\lstinline!\usepackage[!\emph{options}\lstinline!]{!\emph{package}\lstinline!}!''\cmdidx{usepackage}.
  The command has to appear in the \emph{preamble}, i.e.\ after the
  \cmdlink{documentclass} directive and before the beginning of the
  \envlink{document} environment.  Here is an example that loads
  \pkglink{babel} and \pkglink{inputenc}:\cmdidx{bsc}\cmdidx{mathcal}\cmdsymb{langle}\cmdsymb{Sigma}\cmdsymb{Gamma}\cmdsymb{delta}\cmdsymb{rangle}\cmdsymb{subseteq}\cmdsymb{times}\cmdsymb{leftarrow}\cmdsymb{rightarrow}\cmdsymb{in}\cmdsymb{dots}
\begin{lstlisting}
  \documentclass{article}
  \usepackage[francais]{babel}  % French typography and hyphenation
  \usepackage[latin1]{inputenc} % Allow accented chars from iso-8859-1
  \begin{document}
    Une machine de \bsc{Turing} est un tuple $\mathcal{M}=\langle
    Q,\Sigma,\Gamma,\delta,q_0,F\rangle$: $Q$ est un ensemble fini
    d'états, $\Sigma$ et $\Gamma$ sont des alphabets d'entrée et de
    travail respectivement, $\delta\subseteq Q\times\Gamma\times
    Q\times\Gamma\times\{\leftarrow,\rightarrow\}$ est une relation de
    transition associant à un état courant et un symbole de l'espace
    de travail un nouvel état, un symbole de remplacement, et une
    direction de la tête de lecture, $q_0\in Q$ est un état initial,
    et $F\subseteq Q$ est un ensemble d'états finaux.

    Une configuration \dots
  \end{document}
\end{lstlisting}
results in a document
\begin{outex}\selectlanguage{francais}%
  Une machine de \bsc{Turing} est un tuple $\mathcal{M}=\langle
  Q,\Sigma,\Gamma,\delta,q_0,F\rangle$: $Q$ est un ensemble fini
  d'états, $\Sigma$ et $\Gamma$ sont des alphabets d'entrée et de
  travail respectivement, $\delta\subseteq Q\times\Gamma\times
  Q\times\Gamma\times\{\leftarrow,\rightarrow\}$ est une relation de
  transition associant à un état courant et un symbole de l'espace de
  travail un nouvel état, un symbole de remplacement, et une direction
  de la tête de lecture, $q_0\in Q$ est un état initial, et
  $F\subseteq Q$ est un ensemble d'états finaux.

  Une configuration \dots
\end{outex}
Note for instance the inserted unbreakable space before the colon
``:''.  The \cmdlink{bsc} command enforces the French usage of
small-caps with no line breaks for last names.
\end{example}

Let us finish this document with a short overview of a few useful
\LaTeX\ packages.  The purpose of the section is not to explain in
details how each package can be used (read the documentation for
that!), but to show a few handy ones along with examples of use.

\subsection{Bibliographies}
\label{pack-biblio}

The \pkglink{natbib} package is very useful for author-year
references and more flexible citation commands.
\begin{example}
  Contrast Example~\ref{ex-bib} with the following:\cmdidx{Citeauthor}\cmdidx{TeX}\cmdidx{LaTeX}\cmdidx{citep}\cmdidx{citet}\cmdidx{bibliographystyle}\cmdidx{bibliography}
  \begin{lstlisting}[gobble=4]
    \usepackage{natbib}
    ...
    \Citeauthor{TeX} is the inventor of \TeX, but the text processing
    tool most people use in mathematics and computer science is the
    higher-level \LaTeX~\citep{LaTeX}, built on top of \TeX.

    An excellent reference for the management of bibliographies in
    \LaTeX\ is given by \citet[Chapters~12 and~13]{latexcomp}.

    \bibliographystyle{plainnat}
    \bibliography{mybib}
  \end{lstlisting}
  which results in:
  \begin{outex}%
    \Citeauthor{TeX} is the inventor of \TeX, but the text processing
    tool most people use in mathematics and computer science is the
    higher-level \LaTeX~\citep{LaTeX}, built on top of \TeX.

    An excellent reference for the management of bibliographies in
    \LaTeX\ is given by \citet[Chapters~12 and~13]{latexcomp}.

    \bibliography{mybib}
  \end{outex}
  Note that we used a different, \pkglink{natbib}-compliant style for
  the bibliography, namely ``\lstinline{plainnat}'' instead of
  ``\lstinline{plain}''.
\end{example}

\subsection{Mathematics}
\label{pack-math}

The main package for mathematics is \AmS-\LaTeX, loaded through the
package \pkglink{amsmath}.  More packages should be loaded along with
it, notably \pkglink{amssymb} for additional symbols, and possibly
\pkglink{mathtools} for some interesting additions to \AmS-\LaTeX.

Among the many features of \AmS-\LaTeX, let us mention the
environments for equations: \envlink{equation}, \envlink{align},
\envlink{gather}, \envlink{multline}, and their starred versions
(which do not display equation numbers).  Also handy are the commands
\cmdlink{tag} (to add text between parenthesis instead of the equation
number) and \cmdlink{text} (to go back to text mode inside math mode).

Theorems and other similar environments are usually defined using the
\pkglink{amsthm} package (a more recent but also less stable
alternative is \pkglink{thmtools}).

\begin{example}
  A typical declaration for theorem-like environments would
  be\cmdidx{theoremstyle}\cmdidx{newtheorem}
  \begin{lstlisting}[gobble=4]
    \theoremstyle{break}
    \newtheorem{theorem}{Theorem}
    \newtheorem{lemma}{Lemma}
    \newtheorem{proposition}{Proposition}
    \newtheorem{corollary}{Corollary}
    \theoremstyle{definition}
    \newtheorem{definition}{Definition}
    \newtheorem{example}{Example}
    \theoremstyle{remark}
    \newtheorem{remark}{Remark}
  \end{lstlisting}

  These newly-created environments can then be used:
  \cmdidx{mathcal}\cmdsymb{langle}\cmdsymb{Sigma}\cmdsymb{rangle}\cmdsymb{uplus}\cmdsymb{geq}\cmdsymb{alpha}\cmdsymb{beta}\cmdsymb{gamma}\cmdsymb{ast}\cmdsymb{cdots}\cmdsymb{Rightarrow}\cmdsymb{rightarrow}\cmdidx{mathbb}\cmdsymb{dots}\cmdsymb{leq}\cmdsymb{neq}\index{environments!\lstinline ?align*?}
\index{environments!\lstinline ?equation*?}
  \begin{lstlisting}[gobble=4]    
    \begin{lemma}[Context-Freeness]
      Fix a context-free grammar
      $\mathcal{G}=\langle\Sigma,N,P,S\rangle$, $V=N\uplus\Sigma$, and
      let $m\geq 1$.  If $\alpha_1,\dots,\alpha_m,\beta$ are sequences
      in $V^\ast$
      s.t.\ $\alpha_1\cdots\alpha_m\Rightarrow_{\mathcal{G}}^n\beta$
      for some $n\geq 0$, then there exist $n_1,\dots,n_m$ in
      $\mathbb{N}$ and $\beta_1,\dots,\beta_m$ in $V^\ast$ s.t.\ 
      $n=n_1+\cdots+n_m$, $\beta=\beta_1\cdots\beta_m$, and for all
      $1\leq i\leq m$, $\alpha_i\Rightarrow^{n_i}_{\mathcal{G}}\beta_i$.
    \end{lemma}
    \begin{proof}
      By induction on $n$.

      For the base case $n=0$, the lemma holds when defining $n_i=0$
      and $\beta_i=\alpha_i$ for each $1\leq i\leq m$.

      For the induction step, given a derivation in $n+1$ steps, we can
      isolate a first step:
      \begin{align*}
        \alpha_1\cdots\alpha_m
        &=\alpha_1\cdots\alpha'_jA\alpha''_j\cdots\alpha_m
          \tag{by setting $\alpha_j=\alpha'_jA\alpha''_j$}\\
        &\Rightarrow_{\mathcal{G}}\alpha_1\cdots\alpha'_j\gamma\alpha''_j
         \cdots\alpha_m
         \tag{by some rule $A\rightarrow\gamma$ in $P$}\\
        &\Rightarrow^n_{\mathcal{G}}\beta\;.
      \end{align*}
      By induction hypothesis, these last $n$ steps imply the
      existence of $n'_1,\dots,n'_m$ in
      $\mathbb{N}$ and $\beta_1,\dots,\beta_m$ in $V^\ast$ s.t.\ 
      $n=n'_1+\cdots+n'_m$, $\beta=\beta_1\cdots\beta_m$, and for all
      $1\leq i\leq m$,
      $\alpha_i\Rightarrow^{n'_i}_{\mathcal{G}}\beta_i$; in particular,
      \begin{equation*}
        \alpha_j=\alpha'_jA\alpha''_j
        \Rightarrow_{\mathcal{G}}\alpha'_j\gamma\alpha''_j
        \Rightarrow^{n'_j}_{\mathcal{G}}\beta_j\;.
      \end{equation*}
      Then, setting $n_j=n'_j+1$ and $n_i=n'_i$ for all $i\neq j$
      proves the lemma.
    \end{proof}
  \end{lstlisting}
  As you can see, \pkglink{amsthm} also provides a \envlink{proof}
  environment:
  \begin{framed}\noindent\footnotesize%
    \begin{lemma}[Context-Freeness]
      Fix a context-free grammar
      $\mathcal{G}=\langle\Sigma,N,P,S\rangle$, $V=N\uplus\Sigma$, and
      let $m\geq 1$.  If $\alpha_1,\dots,\alpha_m,\beta$ are sequences
      in $V^\ast$
      s.t.\ $\alpha_1\cdots\alpha_m\Rightarrow_{\mathcal{G}}^n\beta$
      for some $n\geq 0$, then there exist $n_1,\dots,n_m$ in
      $\mathbb{N}$ and $\beta_1,\dots,\beta_m$ in $V^\ast$ s.t.\ 
      $n=n_1+\cdots+n_m$, $\beta=\beta_1\cdots\beta_m$, and for all
      $1\leq i\leq m$, $\alpha_i\Rightarrow^{n_i}_{\mathcal{G}}\beta_i$.
    \end{lemma}
    \begin{proof}
      By induction on $n$.

      For the base case $n=0$, the lemma holds when defining $n_i=0$
      and $\beta_i=\alpha_i$ for each $1\leq i\leq m$.

      For the induction step, given a derivation in $n+1$ steps, we can
      isolate a first step that exercises some production
      $A\rightarrow\gamma$ in $P$:
      \begin{align*}
        \alpha_1\cdots\alpha_m
        &=\alpha_1\cdots\alpha'_jA\alpha''_j\cdots\alpha_m
          \tag{by setting $\alpha_j=\alpha'_jA\alpha''_j$}\\
        &\Rightarrow_{\mathcal{G}}\alpha_1\cdots\alpha'_j\gamma\alpha''_j
         \cdots\alpha_m
         \tag{by some rule $A\rightarrow\gamma$ in $P$}\\
        &\Rightarrow^n_{\mathcal{G}}\beta\;.
      \end{align*}
      By induction hypothesis, these last $n$ steps imply the
      existence of $n'_1,\dots,n'_m$ in
      $\mathbb{N}$ and $\beta_1,\dots,\beta_m$ in $V^\ast$ s.t.\ 
      $n=n'_1+\cdots+n'_m$, $\beta=\beta_1\cdots\beta_m$, and for all
      $1\leq i\leq m$,
      $\alpha_i\Rightarrow^{n'_i}_{\mathcal{G}}\beta_i$; in particular,
      \begin{equation*}
        \alpha_j=\alpha'_jA\alpha''_j
        \Rightarrow_{\mathcal{G}}\alpha'_j\gamma\alpha''_j
        \Rightarrow^{n'_j}_{\mathcal{G}}\beta_j\;.
      \end{equation*}
      Then, setting $n_j=n'_j+1$ and $n_i=n'_i$ for all $i\neq j$
      proves the lemma.
    \end{proof}
  \end{framed}
\end{example}


\subsection{Layout}
\label{pack-layout}

The \pkglink{geometry} package features a very comprehensive set of
commands for fine-tuning the layout of your pages.

\subsection{Drawing}
\label{pack-draw}

Ti\textit{k}Z (standing for ``Ti\textit{k}Z ist \textit{kein}
Zeichenprogram'', with package name \pkglink{tikz}) is a highly
versatile package for drawing.
\begin{example}[Drawing with Ti\textit{k}Z]
  Let us draw an automaton with
  \pkglink{tikz}:\cmdidx{usetikzlibrary}
  \index{environments!\lstinline ?tikzpicture?}\cmdidx{node}\cmdidx{path}\cmdidx{footnotesize}\cmdsymb{varepsilon}
  \begin{lstlisting}[gobble=4]
    \usepackage{tikz}
    \usetikzlibrary{positioning}
    \usetikzlibrary{arrows}
    \usetikzlibrary{automata}
    ...
    \begin{tikzpicture}
      [->,>=stealth',shorten >=1pt,initial text=,%
      node distance=2cm,on grid,semithick,auto,
      inner sep=2pt]
      \node[state,initial](0) {0};
      \node[state]        (1) [right=of 0] {1};
      \node[state]        (2) [right=of 1] {2};
      \node[state]        (m) [right=of 2] {$n-1$};
      \node[state,accepting] (n) [right=of m] {$n$};
      \path[every node/.style={font=\footnotesize}]
      (0) edge[bend left] node{1} (1)
      (0) edge[bend right] node{$\varepsilon$} (1)
      (1) edge[bend left] node{2} (2)
      (1) edge[bend right] node{$\varepsilon$} (2)
      (2) edge[bend left,dotted] (m)
      (2) edge[bend right,dotted] (m)
      (m) edge[bend left] node{$n$} (n)
      (m) edge[bend right] node{$\varepsilon$} (n);
    \end{tikzpicture}
  \end{lstlisting}
  \begin{outex}
    \begin{tikzpicture}
      [->,>=stealth',shorten >=1pt,initial text=,%
      node distance=1.4cm,on grid,semithick,auto,
      inner sep=1pt,every node/.style={font=\footnotesize}]
      \node[state,initial](0) {0};
      \node[state]        (1) [right=of 0] {1};
      \node[state]        (2) [right=of 1] {2};
      \node[state]        (m) [right=of 2] {$n-1$};
      \node[state,accepting] (n) [right=of m] {$n$};
      \path[every node/.style={font=\tiny}]
      (0) edge[bend left] node{1} (1)
      (0) edge[bend right] node{$\varepsilon$} (1)
      (1) edge[bend left] node{2} (2)
      (1) edge[bend right] node{$\varepsilon$} (2)
      (2) edge[bend left,dotted] (m)
      (2) edge[bend right,dotted] (m)
      (m) edge[bend left] node{$n$} (n)
      (m) edge[bend right] node{$\varepsilon$} (n);
    \end{tikzpicture}
  \end{outex}
\end{example}

\subsection{Graphics}
\label{pack-graph}

Use the \pkglink{graphics} package for loading external images into
your document.  Whenever possible, use \emph{vector graphics}: except
at very high resolutions (and then the size of your document blows
up), bitmaps have a very poor rendering when printed.

\begin{example}\hfill\cmdidx{includegraphics}
  \begin{lstlisting}[gobble=4]
    \includegraphics[height=3cm]{tlc2e.jpg}
  \end{lstlisting}
  \begin{outex}
    \includegraphics[height=3cm]{tlc2e.jpg}
  \end{outex}
  (As you can see from the ``\texttt{.jpg}'' extension, this is an
  instance of the ``do as I say, not as I do'' principle.)
\end{example}

\subsection{Source Code}
\label{pack-source}
The \pkglink{listings} package provides an extensive support for
pretty-printing source code in a number of languages (this is what I
used throughout this document).

\subsection{References and Hyperlinks}
\label{pack-ref}
The \pkglink{url} package provides an \cmdlink[address]{url} command
for printing URLs.  The \pkglink{hyperref} package redefines much of
\LaTeX\ internals in order to benefit from some of the capabilities of
the \textsc{PDF} format: hyperlinks within the document, a navigation
table, meta-information, etc.


\end{document}
