Some hints for Latex
Template
Structure of an empty latex file
\documentclass[11pt,a4paper]{article}
\begin{document}
%
% [...]
%
\end{document}
Include a file when a large document is split into different files
\include{filename}
Margins
Change the margins
\addtolength{\textheight}{2.4cm}
\addtolength{\hoffset}{-1.2cm}
\addtolength{\voffset}{-2cm}
\addtolength{\textwidth}{2.3cm}
These are the margins I use more often, as the page becomes larger but not too crammed.
Draft
Tell latex that the document is in a draft state
\documentclass[draft]{article}
Latex will print a black square beside the lines that are too long.
Print 'Draft' across all pages of the document in a light grey colour
\usepackage[light,all]{draftcopy}
Spacing
Doublespace the text
\usepackage{setspace}
\doublespacing
One-and-half spacing
\usepackage{setspace}
\onehalfspacing
Itemize and enumerate
Change the simbol of the second level of an itemize to be a circle
\renewcommand{\labelitemii}{$\circ$}
Use alphabetic letters in enumerations instead of numbers
\renewcommand{\theenumi}{\alph{enumi}}
Change the alignment of the bullet points
\setlength{\leftmargini}{1em}
Theorems and such
Give a title to a theorem
\begin{theorem}[Fermat's theorem]
Define a new theorem-type environment
\newtheorem{lemma}{Lemma}
To write a proof, it's possible to create a new environment
\newenvironment{proof}
{\noindent \textit{\textbf{Proof.}}~ } % begin proof
{\hfill\rule{2mm}{2mm} \vspace{\parskip} } % end proof
Otherwise, the package amsthm provides a proof environment.
Table of contents
Create a table of contents
\tableofcontentsThe table of contents will be positioned where the command has been used.
Rename the table of contents from the default 'Contents'
\renewcommand{\contentsname}{Table of Contents}
Provide and optional title to be used in the table of contents
\section[optionaltitle]{sectiontitle}
Add a line to the table of contents
\section*{sectiontitle}
\addcontentsline{toc}{section}{sectiontitle}
Abstracts and such
Define an abstract
\begin{abstract}
%
% [...]
%
\end{abstract}
Rename the abstract from the default 'Abstract'
\renewcommand{\abstractname}{Acknowledgements}
Page numbering
Use roman page numbers
\pagenumbering{roman}
Return to use arabic numbers
\pagenumbering{arabic}
Bibliography
Write a bibliography
\begin{thebibliography}{99}
\bibitem{wright} Wright, S.J. (1997),
{\em Primal-dual interior-point methods},
SIAM, Philadelphia.
\end{thebibliography}
The parameter {99} is used when there are 10 or more references. If there are less than 10, then use {9}.
Include a bibliography in bibtex format, saved in bibliographyfile.bib
\bibliographystyle{plain}
\bibliography{bibliographyfile}
Other available styles
\bibliographystyle{alpha}
\bibliographystyle{unsrt}
\bibliographystyle{abbrv}
Cite a reference
\cite{wright}
Specify some pages in a reference
\cite[pp. 120--125]{wright}
Tables
To draw a table, use the tabular environment. The parameters of the environment specify the alignment of the column: centered (
c), left aligned (l) or right-aligned (r). A vertical bar (|) can be used to print a column separator. A row separator is printed with the command \hline.
\begin{tabular}{|l|c|c|c|} \hline
Sales & 1 & 2 & 3 \\ \hline
North & 2552 & 2770 & 2518 \\
South & 2548 & 2600 & 2522 \\
West & 2569 & 2805 & 2511 \\
East & 2569 & 3571 & 2712 \\ \hline
\end{tabular}
To have an entry that spans more than one column, use the command
\multicolumn, specifying how many columns to span, the alignment, and the text.
\begin{tabular}{|l|c|c|c|} \hline
& \multicolumn{3}{c|}{Sales} \\ \cline{2-4}
Total & 6738 & 8954 & 7512 \\ \hline
\end{tabular}
To print a row separator only between certain columns, use the command
\cline, specifying the columns.
\begin{tabular}{|l|c|c|c|} \hline
Sales & 1 & 2 & 3 \\ \cline{2-4}
Total & 6738 & 8954 & 7512 \\ \hline
\end{tabular}
The
\raisebox command can be used to move up or down some text.
\begin{tabular}{|l|c|c|c|} \hline
\raisebox{-1.5ex}{Sales} & 1 & 2 & 3 \\ \cline{2-4}
& 6738 & 8954 & 7512 \\ \hline
\end{tabular}
This may enlarge the row in which it appears, creating some unwanted white space. To avoid it, it's possible to give some optional parameters to \raisebox.
\begin{tabular}{|l|c|c|c|} \hline
\raisebox{-1.5ex}[0ex][-1.5ex]{Sales} & 1 & 2 & 3 \\ \cline{2-4}
& 6738 & 8954 & 7512 \\ \hline
\end{tabular}
To center horizonally the table, enclose the tabular environment inside a center environment.
\begin{center}
\begin{tabular}
[...]
\end{tabular}
\end{center}
To change the spacing of the columns, and shrink them
\setlength{\tabcolsep}{4.2pt}
Graphs
To draw a graph (nodes and arcs), use the package pst-node.
\[
\begin{psmatrix}[colsep=4em,rowsep=2ex,mnode=circle,framesep=2ex]
& [name=I]{\bf 2} \\ [0pt]
[name=A]{\bf 1} & & [name=C]{\bf 3}
\psset{arrows=->,arrowscale=2,nodesep=1pt}
\ncarc[arcangle=20]{A}{I} \Aput{\frac{1}{2}}
\ncarc[arcangle=-20]{C}{I} \Bput{\frac{1}{2}}
\end{psmatrix}
\]
Here is another example
\[
\begin{psmatrix}[colsep=3em,rowsep=4ex,mnode=circle,framesep=2ex]
& [name=A]{\bf 1} & [name=B]{\bf 2} \\
& [name=F,colsep=-2em]{\bf 6} & & [name=C,colsep=0]{\bf 3} \\
& [name=E]{\bf 5} & [name=D]{\bf 4}
\psset{arrows=->,arrowscale=2,nodesep=2pt}
\ncline{A}{B}
\ncline{B}{C}
\ncline{C}{A}
\ncline{D}{E}
\ncline{E}{F}
\ncline{F}{D}
\ncline[linestyle=dashed]{A}{F}
\ncline[linestyle=dashed]{E}{B}
\end{psmatrix}
\]
Slides
Use the seminar class
\documentclass[a4]{seminar}
\input{seminar.bug}
Define a slide
\begin{slide}
%
% [...]
%
\end{slide}
To define the frame style
\usepackage{fancybox}
\slideframe{shadow}
Other available styles
\slideframe{double}
\slideframe{plain}
\slideframe{oval}
\slideframe{none}
A macro to create the title of the slide
\newcommand{\slidetitle}[1]
{
\begin{center}
\textbf{#1}\vspace{-3ex}
\end{center}
\rule{\linewidth}{0.3mm}%\\[0.2ex]
}
This macro centers the title and adds a horizontal line underneath it, and can be used like this
\slidetitle{title}
To create a pdf file with good fonts and correct paper orientation
dvips -Ppdf slides.tex -o ps2pdf slides.ps slides.pdf
Fonts and sizes in math environment
Use calligraphic letters in the math environment
\mathcal{N}
To change the size in the math environment, use one of these commands
\displaystyle \textstyle \scriptstyle \scriptscriptstyle
Miscellaneous
Write a Linear Programming problem in standard form
\[ \begin{array}{rl}
\min & c^Tx \\
\mbox{s.t.} & Ax = b \\
& x \ge 0.
\end{array}
\]
Write a matrix
\[
A = \left[ \begin{array}{cc}
\tilde{A} & 0 \\ Q & T \\
\end{array} \right]
\]
Insert section number in equations
\usepackage{amsmath}
\numberwithin{equation}{section}
Add automatically quotation marks around a quotation
\renewenvironment{quotation}
{\begin{quote}\singlespace
``\sl \ignorespaces}
{\unskip \rm '' \end{quote}}
Rotate a figure
\includegraphics[angle=45]{file.eps}
Create a framed box
\newcommand{\fb}[1]{
\begin{center}
\fbox{\parbox{.9\textwidth}{#1}}
\end{center}
}
To use it
\fb{Text in the framed box}
For multiline comments use the package verbatim
\usepackage{verbatim}
To use it
\begin{comment}
%
% [...]
%
\end{comment}
To set the label and separation to the left of the caption text
\usepackage[hang]{caption}
To set the depth of the numbering in the sections
\setcounter{secnumdepth}{2}
To set the depth of the numbering in the table of contents
\setcounter{tocdepth}{2}
Change the .emacs file in the home directory to run latex commands directly from emacs
(setq tex-default-mode 'latex-mode tex-run-command "latex" tex-dvi-view-command "xdvi" tex-dvi-print-command "dvips")Now, using C-c C-f will compile the latex file.
Two graphs can appear in one figure by using the subfig package
\begin{figure}
\subfloat[First figure caption]{\includegraphics{file1.eps}}
\hfill
\subfloat[Second figure caption]{\includegraphics{file2.eps}}
\caption{Caption for both figures}
\end{figure}