# Tips and tricks for LaTeX and friends

• Published 2005-04-01 (8 years, 1 month ago)
• Updated 2006-11-29 (6 years, 5 months ago)

TeX, LaTeX, BibTeX and friends, are a constant source of joy and frustration. This growing collection of tips and tricks is an attempt to document some of the things I have learned working with these wonderful tools. This page is just one of many similar LaTeX tips and tricks pages on the net. Hopefully you'll find something useful here.

## Numbering

### Disabling section numbering without removing them from the table of contents

LaTeX enumerates by default all sections. It is possible to disable numbering of a specific section by adding an asterisk (*) after the section command like this:

\section*{Introduction}
\subsection*{Test}


However, when you do this, the section is not added to the table of contents. Sometimes you want to disable section numbering but still need to generate a table of contents, or you want to have bookmarks in the generated PDF file. Add this code at the start of your document, and your problem is solved:

% disables chapter, section and subsection numbering
\setcounter{secnumdepth}{-1}


## Margins

### Marginal notes

Notes in the page margins are easily created with the command

\marginpar{marginal note}


### Putting stars in the margin

Here's a handy command for displaying a user-specified number of stars in the margin. Useful for ratings and such.

% nstar is a command for drawing n stars in the margin. Useful for ratings.
% Requires the 'ifthen' package
\newcounter{scount} % create counter
\newcommand{\nstar}[1]{%
\setcounter{scount}{#1}% initialize counter
\marginpar{\hfill      % move content closer to the main text
\whiledo{\value{scount}>0}
{{\Large{$\star$}}\addtocounter{scount}{-1}}
}
}


The stars are not always placed where expected, so some experimentation may be necessary. Here's an example on how to use the \nstar command:

\paragraph{Item A} Regulari quam ti del coalescent \nstar{3}
\paragraph{Item B} Li nov lingua franca va \nstar{2}
\paragraph{Item C} A un Angleso it va \nstar{4}


The final result will look like this:

## Graphics

### How to include graphics in a LaTeX document

Many newcomers to LaTeX find it difficult and confusing to include graphics in LaTeX documents. Common questions are: What format should I use? Why won't my graphics work with PDFLaTeX? What is EPS? Below are a few links to some excellent resources that will answer most of the common questions.

### Using LaTeX in Matlab graphics

Matlab is a superb tool for advanced plotting of data. It can save graphics in the EPS format, which is perfect for inclusion in LaTeX documents. Matlab has some support for TeX syntax in annotations. However, the result does not look that good and the typography is different from LaTeX's. Fortunately there is a solution to this problem.

LaPrint is a neat Matlab script for saving figures in a LaTeX-friendly format. The script replaces all labels, text and annotations with LaTeX markup, which are subsequently processed by LaTeX. Read all about LaPrint's features in the documentation. Here's a small example of LaPrint in use:

theta = -pi:.1:pi;
y = sin(theta)./cos(theta);
plot(theta,y)
xlabel('$\theta$'); ylabel('$y$')
text(-1,20,'\LaTeX')
set(gca,'XTick',-pi:pi/2:pi)
set(gca,'XTickLabel',{'$-\pi$','$-\frac{\pi}{2}$','0', ...
'$\frac{\pi}{2}$','$\pi$'})
title('$y(\theta) = \frac{\sin \theta}{\cos \theta}$')
laprint(1, 'demofig') % Use LaPrint to save figure

The Matlab code above produces the following figure after some processing by LaTeX.

An interesting alternative to LaPrint is fig2tex. This Matlab script replicates Matlab figures using LaTeX and macros from the PSTricks package.

### LaTeX friendly drawing programs

My definition of a LaTeX friendly drawing program is a program that lets me create and manipulate vector graphics and easily annotate them using LaTeX markup.

xfig
A classic. Well known among Unix and Linux users. Also available for Windows using Cygwin.
jfig
Similar to xfig. Written in Java.
Ipe
Recommended! An extensible drawing editor available for most platforms. Has a nice set of snapping and align tools for creating accurate drawings. A practical feature is that Ipe can store all figure data in the PDF or EPS file itself. Read more about Ipe in a recent issue of the PracTeX Journal.
TpX
A lightweight editor for the Windows platform. Can import EMF/WMF pictures.

The above list is not complete.

### How to save a page in your document as a standalone EPS/PDF file

Sometimes it's handy to save a page of your LaTeX document as a standalone EPS or PDF graphics file. This may for instance be when you use psfrag, use a drawing package like pstricks or PGF, or you have some complicated table or equation that you want to include in another document.

The first step is to create a page with no page numbering. This makes it easier to create a tight bounding box.

\documentclass{article}
% put necessary packages here ...
\begin{document}
\pagestyle{empty} % Remove any page numbering.
% put content here
% ...
\end{document}


You can now take the latex->dvi->ps route or the pdflatex->pdf route to create a document.

The GSView way. Open you ps file in GSView. Select File->PS to EPS. A dialog box will pop up. Select the Automatically calculate Bounding Box option and press Yes to save as EPS. This will only work for single page documents. If your document has multiple pages, extract the relevant page first using the File->Extract menu.

The dvips and epstool way. The standard dvips program can create EPS files with tight bounding boxes if you use the -E option:

> latex source.tex
> dvips -E -o source.eps source.dvi


However, dvips sometimes calculates an incorrect bounding box. It works for standard math and text content, but not always for graphics. The solution is to add Epstool to our tool chain. Epstool can determine the correct bounding box and you can also use it to add bitmap previews.

> epstool --copy --bbox source.eps dest.eps


The PDF way. If you have access to Adobe Acrobat (the full version, not the Reader) you can use the crop tool. Another option is the excellent pdfcrop tool. It's available in the MikTeX distribution. It's usage is simple:

> pdfcrop input.pdf output.pdf


Note that pdfcrop will crop every page in the document.

Using the preview package. This is my favourite. The package provides several ways of previewing parts of your document. For example, adding this to your document preamble:

\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}


will extract every tikzpicture environment. If you use pdflatex, every tikzpicture will be put on a separate page in the resulting pdf document. See the documentation for more details. To add a margin around the figure add:

\setlength\PreviewBorder{10pt}%


### Creating graphics and illustrations programmatically

LaTeX users have many options for creating drawings, diagrams and illustrations. An overview of some of the many specialized packages can be found in the TeX Catalogue. Most of them have no GUIs. Instead you have to create the drawing by writing code. This is probably a bit intimidating to some users, but it is actually a very powerful and elegant way of creating graphics.

I have put together a list of some of the available general purpose tools. The list below is by no means complete.

These tools work within LaTeX.

PSTricks
An extremely powerful tool. Take a look at some examples to see some of the possibilities. Relies heavily on Postscript specials, so you have to do some post processing if you want to use your graphics with pdflatex.
XYPic
Nice for typesetting matrix-like diagrams.
PGF and TikZ
PGF is similar to pstricks and the standard picture environment. Unlike pstricks, it produces both PostScript and PDF output and works together with latex, dvips, pdftex, and pdflatex. PGF is not as advanced as PSTricks, but it has a clean interface and a nice manual. It also supports true transparency in PDF output.
mfpic
A LaTeX front end to Metapost. Works with both latex and pdflatex.

The following tools use are more or less standalone tools with syntax quite different from LaTeX's. However, they all use LaTeX to typeset text and math.

Asymptote
Quoting from the Asymptote web site: Asymptote is a powerful descriptive vector graphics language that provides a natural coordinate-based framework for technical drawing. Labels and equations are typeset with LaTeX, for high-quality PostScript output.
Metapost
A classic. Derived from Donald Knuth's Metafont. The language is powerful and elegant, but many find it a bit arcane.
PyX
PyX is a Python package for the creation of PostScript and PDF files. It combines an abstraction of the PostScript drawing model with a TeX/LaTeX interface.
Sketch
A small, simple and elegant system for producing line drawings of two- or three-dimensional solid objects and scenes. Generates pstricks code.

### How to edit PDF and EPS graphics

IPE comes bundled with a great command line tool called pdftoipe. The tool converts arbitrary PDF files to a format that can be edited with IPE. This is very practical for:

• doing minor edits to an existing illustrations
• adding LaTeX markup
• removing unwanted labels and other clutter

After you have edited the file with IPE, you can save drawing as a PDF file again. You can also edit PS/EPS files by using ps2pdf first and then saving the modified drawing as EPS.

The pdftoipe tool does a good job converting plain drawings, but gradients are not supported. Font information is lost in the conversion process, but the text itself is often preserved. You can also disable text conversion with a command line switch.

## PDF

### Hyperlinks and bookmarks with the hyperref package

One of the nice things about PDF documents is the ability to include hyperlinks and navigation in the document. Hyperref is a package for making documents with live links in PDF and HTML output formats. Here's an example on how to set up hyperref:

\usepackage{hyperref}
\hypersetup{%
pdftitle = {Document title},
pdfkeywords = {pdf, hyperref, bookmarks},
pdfauthor = {Kjell Propell}
}


Hyperref has many features for manipulating the generated PDF document. My favourite is the ability to automatically generate tree structured bookmarks from the table of contents. It also inserts hyperlinks to figures, tables and references.

Hyperref defines several useful macros for inserting url's:

Take a look at my \href{http://fauskes.net}{web site}!


An important thing to remember is that PDF bookmarks don't understand special LaTeX markup. If you use mathematics in for instance a section title, the hyperref will complain. A special macro \texorpdfstring is provided to solve the problem. Example:

\section{\texorpdfstring{$H$_2\$O}{Water}}


### Using LaTeX and PDF for presentations

LaTeX and PDF can be used to create very impressive presentations. Michael Wiedmann has put together a nice overview of some of the possibilities for creating screen presentations. Here are a few of them:

Which package to use is a matter of personal taste. The Web package is probably the easiest to use for simple presentations. Beamer is very advanced and has excellent documentation. A nice introduction to the HA-prosper package is available in the PracTeX Journal, no. 2 (2005)

Newer versions of the PDF format have rich multimedia capabilities. You can therefore include animations, videos and sounds in your PDF presentation. Beamer comes bundled with a multimedia package, but it to access the latest multimedia capabilities of the PDF format I'll recommend the movie15 package. Jens Nöckel has written a nice step by step example on how to embed a movie in a PDF using movie15.

### A few solutions to the annoying Adobe Acrobat open file lock problem

The Windows version of Adobe Acrobat has a very annoying feature when generating PDF documents with LaTeX: It puts a lock on the file while it's open in the viewer. This requires you to close the document before you can rebuild the document. Editors like TeXnicCenter can do this automatically, but when the document is reopened in the viewer, it often display the first page of your document. This is a bit irritating if you are working on a different page.

There is a workaround to this problem in Acrobat. Press the "Previous View" button or use the Alt - Left arrow shortcut. Acrobat will then reopen the document and navigate to the page you were viewing before the document was closed. The excellent WinEdt editor does this automatically for you. It should therefore be possible to do it from TeXnicCenter as well.

Another solution is to use a different program for viewing PDF files. A good choice is GSView. GSView automatically detects changes to the document and updates its display while staying on the current page. This saves you a few keystrokes. GSView is not as streamlined as Acrobat, but it's good enough for verifying the output from pdflatex while writing and debugging a document.

A tip for TeXnicCenter users. It's quite easy to modify your build profile to use GSView instead of Adobe Acrobat. Take a look at the Viewer tab in Build->Define output profiles. Remember to set the "Close document before running (La)TeX" option to "Do not close". Click on the image on the left to see how I have configured my viewer. The (La)TeX and postprocessor tabs have the same setting as the LaTeX => PDF profile.

## BibTeX

### Tools for managing BibTeX files

The BibTeX format is simple and flexible. However, as the number of entries grows, it is useful to have a GUI for managing them. For some links to such tools, take a look at my notebook entry «Finding the right bibliographic/reference tool». My favourite is JabRef.

### How to convert citations from IEEEXplore and EV2 to BibTeX

BibConverter is an online tool I have written for converting citations from IEEEXplore and Engineering Village (EV2) to the BibTeX format. It extracts data from text copied directly from the web and outputs BibTeX records that contains much more information than citations exported using the export functionality provided by these bibliographic services. Read more about my tool in this notebook entry.

### Compare different bibliographic styles

One of the greatest strengths of BibTeX is that it's easy to switch between different bibliographic styles. There are many styles to choose from. Here's a few links to places where you can compare different styles: