LaTeX has undergone incredible changes during the last decade. Unfortunately, many LaTeX users still use it as if we were still in the 90's! Here are some tips to use LaTeX to its maximum.
Tip
In this post I give links to some of the manuals for the packages described. It is however much more convenient to check the manuals offline with
texdoc somedoc
Some keywords to use here are tikz
, hyperref
, biblatex
, etc.
Basics
First, some standard practice, that are supported by most journals and also by the arXiv preprint server.
Forget latex, use pdflatex
First, use the pdf engine:
> pdflatex yourfile.tex
In fact, this is now the standard engine for LaTeX developing, as the command latex
is just an alias to pdflatex
.
When using pdflatex
, make sure that all the picture that you include with includegraphics
are of supported format, that is, either pdf
, jpg
or png
.
Use modern LaTeX commands
LaTeX introduced lots of safer commands.
For instance, you should use \newcommand
, instead of \def
, \textbf
instead of \bf
, etc.
These commands have been around for a while now, but you might still be using the old TeX commands.
To make sure that this does not happen, put this in the beginning of the file
\usepackage[l2tabu, orthodox]{nag}
Accents in Unicode
In the former days of Tex/LaTeX, you would input an accented letter via a TeX command such as \"o
instead of ö
.
Don't do that!
The unicode standard supports practically all the glyphs of all the languages in the world.
The only thing you have to do is to make sure that you are using unicode (specifically the UTF-8 encoding), and tell LaTeX that you are doing so by using
\usepackage[utf8]{inputenc}
Then you may simply use accents directly in the text:
It was \emph{Lars Hörmander} who...
Pictures with TikZ
For practically all purposes, TikZ is the preferred way to draw nice looking pictures in LaTeX.
The documentation (remember texdoc tikz
) is huge, and there are lots of examples on TeXample.
A good place to find advice, answers and example is also TeX stackexchange.
In particular, with TikZ you will advantageously draw:
and much more.
Use Hyperref
This one is a must have!
The hyperref
package takes care of adding hyperlinks in your document, and, just as importantly, adding a table of content in the pdf.
You would typically load hyperref
with
\usepackage[pdfusetitle]{hyperref}
If you are not happy with the aspect and colour of the links, you may configure hyperref
with something along these lines:
\usepackage[svgnames]{xcolor}
\colorlet{linkcolour}{DarkBlue}
\hypersetup{colorlinks=true, linkcolor=linkcolour, citecolor=linkcolour, urlcolor=linkcolour,}
Automatic Links
When using hyperref
, all the references are automatically transformed into hyperlinks.
However, one still uses to enter the name of the referenced object manually (for instance a Theorem, a Figure, etc.)
So the standard usage is
\section{My Section}
\label{my-section-label}
...
See section \ref{my-section-label}...
The disadvantage is obvious: one has to know that my-section-label
is a section (and not a chapter, or a subsection), and the name is then hardcoded in the text.
If the section is changed to a chapter or a subsection, one has to change the name manually.
The hyperref
package has a nice solution: the command \autoref
.
Usage is as follows:
See \autoref{my-section-label}...
The result is the same as above, except that
- The hyperlink area is both the section name and section number
- The name is determined automatically by
hyperref
There is a catch, though!
Some custom LaTeX environment will not be registered in hyperref
, and a warning will be displayed on typesetting.
One remedy for that is to create new hyperref
names for each custom environment which can be referenced (typically, Lemmas, Propositions, and the like).
It is done in the following way, for a custom lemma
environment:
\newcommand*\lemmaautorefname{Lemma}
It is also possible to change the default autoref names. For instance, I typically redefine the sectioning autoref names as follows:
\renewcommand*\sectionautorefname{\S\!}
\renewcommand*\subsectionautorefname{\S\!}
\renewcommand*\subsubsectionautorefname{\S\!}
Creating new commands
The package xparse can be used to create more flexible LaTeX macros.
For instance, instead of the usual newcommand
, one may use the following syntax
\usepackage{xparse}
\NewDocumentCommand\mycommand{}{somecodehere}
There is nothing new so far, but the xparse
version is much more versatile than the standard LaTeX newcommand
when it comes to arguments, and especially, to default arguments.
For instance, a default argument is handled as:
\NewDocumentCommand\varpower{mO{n}}{#1^{#2}}
Now a call to \varpower{x}
would print $x^n$, and a call \varpower{x}[2]
would print $x^2$. Neat, huh?
So far I've just scratched the surface, so feel free to take a look at this blog post for a comprehensive introduction.
Delimiter Size
Delimiter size has always been a headache in LaTeX.
The standard solution is either to set the size manually with the macros \bigl
and \bigr
, etc.
Another solution is to use \left
and \right
.
However, the latter often results in too big delimiter size.
Anyway, both methods are cumbersome because one has to set the size of both the left and the right delimiter.
The excellent mathtools
package saves the day.
It allows to create delimiter macros, for which the size setting is very flexible.
Here is an example with parenthesis delimiters:
\DeclarePairedDelimiter\paren{\lparen}{\rparen}
One uses that macro with
f\paren{\int{a}^{b}g(x) \mathrm{d} x}
Now it's much easier to change the size of the parentheses, though!
In order to use the \left
/right
commands, one uses the starred version of paren
, that is \paren*
.
What if it's too big (it often is)?
Well, then you can simply fine tune the size with an optional argument such as \paren[\big]
, or \paren[\Big]
.
See for instance this tex SE answer for a nice way to display set comprehension.
Equation Numbering
In standard LaTeX, a common habit is to manually decide which equations are to be numbered.
What I mean is that the standard practice is to use the unnumbered environment equation*
(or the pair \[\]
) for unlabeled equations, and to use the numbered environment equation
for labeled equation.
This not only cumbersome, but also unpractical since at the drafting stage it may be more convenient to number all the equations.
Therefore, it is easier to use the numbered environment equation
everywhere, and to activate a switch to automatically number only the referenced equations.
This is possible with the mathtools
package as follows:
\usepackage{mathtools}
\mathtoolsset{
showonlyrefs=true % or false in draft mode
}
In the rest of the document, one uses only the equation
environment (without star), and mathtools
will automatically detect which equations to number.
Advanced
Here are some advanced usage of LaTeX, some of which supersede the pieces of advice above. Be warned though, that some of the changes introduced are not always supported by all journals, not even by arXiv!
Forget about pdflatex, use xelatex
Instead of compiling your document with pdflatex
, you may use instead:
> xelatex yourfile.tex
There is practically nothing else you should do except remove the inclusion of the inputenc package which is not needed anymore.
The huge advantage of xelatex
is that you can now use any font in your system.
The days of painstakingly installing the zillions of TeX specific font files are over!
The simplest way to use a system font in this case is just to use
\usepackage{fontspec}
\setmainfont{Times}
\setsansfont{Helvetica}
Now the main text is typeset in Times, and the sans serif parts are typeset in Helvetica. Of course you should proceed to the fontspec manual for more details.
Forget about BibTeX, use Biblatex
The venerable BibTeX was practically impossible to customise unless you wanted to learn a whole new language.
Note that the bib file format is still the same, so, again, there is not much to change here.
The big difference is that you can't use bst
files anymore, but decide on the appearance of the bibliography section directly with LaTeX commands.
This is done either by using biblatex with options, or by redefining some of its macros.
The call is
\usepackage{biblatex}
Here is a quick example of the typical configuration file I am using
\usepackage[backref=true, isbn=true, url=true, firstinits=true, maxnames=20, style=numeric,]{biblatex}
This tells biblatex
to use back reference (references to where the entry is cited), print urls, only use first initials and index the references with numbers.
A lot more can be customised this way, as indicated in the biblatex manual.