Contents:

LaTeX is a great system for typesetting reports, articles, letters, and even posters and presentations, but it takes a while to find a set of useful packages and tune it to your personal style. This page is a collection of useful tips, tricks, and advice that I’ve learned while working with LaTeX. I’m assuming that you’re familiar with the basics of LaTeX; if you’re not, I recommend starting with the LaTeX Wikibook.

## Getting Help

For common problems, doing a simple web search is often the quickest way to find a solution. For problems related to a specific package, the documentation of most packages is very comprehensive and provides usage instructions and solutions to common problems (especially conflicts with other packages). These are some other good resources:

## Project Structure

For small to mid-size documents, I recommend the following file/directory structure:

project/
├── .git/
├── .gitignore
├── fig/
│   ├── tikz_figure.tex
│   ├── svg_figure.svg
│   └── …
├── Makefile
├── mystyle.sty
├── document.tex
└── …


Notice, in particular, that figures are confined to their own directory, fig/. Also, the style of the document (i.e. what would normally go in the preamble) is in a separate mystyle.sty file. This keeps from cluttering up the .tex document and makes it easy to copy your standard style between projects.

## Reference Management

I recommend JabRef for reference management because it’s easy to use, is quite comprehensive, and has nice features like group management, advanced searching, linking to downloaded full-text articles (which is really convenient!), integrating with external tools, etc. It is compatible with both BibTeX and BibLaTeX.

It’s especially convenient to set up automatic import of .bib files from your web browser. Then, you can click on the “download citation” link available on most journal article pages and directly import the citation information into JabRef.

## Compilation and SyncTeX

I typically use GNU Emacs with AUCTeX for compilation, but if you use a Makefile, then latexmk makes it easy to run all of the appropriate commands to compile the document.

It’s possible to synchronize AUCTeX and the Okular document viewer such that when you type C-c C-v (TeX-view) in Emacs, Okular will open the document centered at the cursor location, and if you Shift-click in Okular, Emacs will open the LaTeX source file with the cursor at the corresponding location. To set up the Emacs → Okular link, assuming AUCTeX is already installed and configured, add the following to your .emacs:

;; Enable SyncTeX correlation.
(setq TeX-source-correlate-mode t)
(setq TeX-source-correlate-start-server t)
(setq TeX-source-correlate-method 'synctex)

;; Use Okular as the PDF viewer.
(setq TeX-view-program-selection '((output-pdf "PDF Viewer")))
;; Define %(dir) expansion for use in TeX-view-program-list' because the expanded
;; path needs to match exactly the path listed in the *.synctex.gz file.
(setq TeX-expand-list '(("%(dir)" (lambda (arg) default-directory) nil)))
(setq TeX-view-program-list '(("PDF Viewer" ("okular --unique file://%(dir)%o#src:%n%(dir)./%b"))))


To set up the Okular → Emacs link, open Okular’s configuration dialog with SettingsConfigure Okular…, click on the Editor tab, and then select Emacs client as your Editor. The corresponding Command is:

emacsclient -a emacs --no-wait +%l %f


If you’re using latexmk to build your document, make sure you tell your LaTeX engine to generate the SyncTeX file. For example, if you’re using a Makefile with latexmk and the XeLaTeX engine, you can use the following rule (note the -synctex=1 option):

%.pdf: %.tex
latexmk -pdf -pdflatex="pdflatex -shell-escape %O %S" "\$<"


The tikz package is great for drawing complex technical diagrams that would be difficult to draw by other means. As a nice bonus, the source of the diagram is plain text, so it works well with version control. For example, I drew this image with tikz:

You can find many additional examples at TeXample.net.

Often, the best way to include a tikz image in your document is to create it in a separate file, and then include it with the standalone package. This way, you can render the image by itself quickly while you’re editing it, and your main document stays clean.

### Captions

I prefer the captions to be smaller than the body text. You can achieve this with the caption package. It also provides the \captionof command, which is useful for cover pages. Include the package like this to get small captions:

\usepackage[font=small]{caption}


Often, it’s useful to have multiple subfigures/subtables make up an overall figure/table and be able to label/cross-reference the subfigures/subtables individually. The subcaption package works well for this, in particular the subfigure/subtable environments and the \subcaptionbox command. Include the package like this:

\usepackage{subcaption}


Here’s an example of using the subfigure environment:

Note that if you use the subcaption package with the svg package, you need to add the following to your preamble (before including svg) to avoid conflicts between the packages:

\usepackage{scrlfile}


### Table Formatting

The booktabs package makes it easy to generate professional-looking tables with a minimum of effort (only three commands for most tables), such as this one:

Here’s a more complex example from the documentation:

In some cases, the tabu package is easier to use than the built-in LaTeX tabular environment, such as when creating tables inside of equations.

### Code Listings

Use the listings package. It allows you to include external files as code listings, easily cross-reference the listings, and format the listings nicely. This is how I like include the package:

\usepackage{listings}
\lstset{frame=single,basicstyle=\small\ttfamily,showstringspaces=false}


and here’s an example of the output:

Use the hyperref package to automatically create links for cross-references, to allow you create external links to URLs, and to allow you to edit the metadata of the output PDF. For example, I include the hyperref package like this (I define \thetitle and \theauthor elsewhere):

\usepackage[unicode,xetex]{hyperref}
\hypersetup{
citecolor = black,
filecolor = black,
urlcolor = black,
pdftitle = \thetitle,
pdfauthor = \theauthor,
pdfdisplaydoctitle = true
}


You can create a hyperlinked URL like this: \url{https://archive.org}, and you can create a named external link like this: \href{https://archive.org}{Internet Archive}.

### Cross-Referencing

The cleveref package makes it easy to correctly format cross references. For example, you can write \cref{fig:foo,fig:bar} instead of figures~\ref{fig:foo} and~\ref{fig:bar}. I change the \creflastjunction from the default to add a serial comma:

\usepackage{cleveref}
\newcommand{\creflastconjunction}{, and~}


Note that you must include the cleveref package after you include the hyperref package.

I like to have a fancy header that gives the current section, document title, and author’s name. I use the fancyhdr package:

\usepackage{fancyhdr}
\fancyhf{}
`