latex tips.
Like many other scientists, I typeset my document using LaTeX. In this post, I give some useful hints to make your documents nicer, covering protected whitespaces, abbreviations and math formula tweaks!
Avoiding linebreaks#
LaTeX’s hyphenation and linebreaking is great. But sometimes you explicitly want to avoid a linebreak between two objects in a sentence, for example
when citing a work as in Reference \cite{abc}
. To achieve this, we need to use a protected whitespace, in LaTeX typeset via ~
.
As the name suggests, it represents a whitespace LaTeX will not touch, contrary to regular whitespaces that are often ignored.
A protected whitespace will make LaTeX refrain from separating Reference
from the actual citation: Reference~\cite{abc}
. The same reasoning applies to figures, tables, sections and so on, e.g. as in Section~\ref{sec:abc}
or Sec.~\ref{sec.abc}
.
Avoiding large space after full stop#
One often uses the abbreviations e.g.
and i.e.
that end with a full stop. But LaTeX will normally assume that a full stop ends a sentence
and will therefore create a larger whitespace after e.g.
or i.e.
. To avoid this, we need to add a \
after the abbreviation, as in e.g.\
or i.e.\
. This also applies to any other abbreviation of course.
Note that the protected whitespace will achieve the same effect, so Fig.~\ref{fig:xyz}
will render correctly without the \
. But as we just learned, the protected whitespace will “bind” the two objects on its sides together to avoid breaking them up. This is usually not the behaviour we want with abbreviations. Therefore, while e.g.~the example
achieves the same result as e.g.\ the example
, the latter should be preferred.
Make nicer equations#
This section serves to highlight some commands that are extremely useful to typeset beautiful math formulas. But I warn you, once you’ve seen it, you can’t unsee it anymore!
hphantom
and vphantom
#
These commands allow to insert characters of zero width or height into your code to adjust math displays. As an example, look at the following two ways of typesetting fermionic operators:
The left one is f_j^{\dagger} f_k
. One can see that the index k
sits higher than the index j
because the latter is pushed down
by the \dagger
. To alleviate this, we can introduce a phantom dagger via f_j^{\dagger} f_k^{\vphantom{\dagger}}
and get the right
variant.
There also exists the command \mathstrut
which is defined as \vphantom{(}
. It can be used as a general purpose alignment tool for
math formulas. Consider the square roots in the above example, where the left one was typeset as \frac{1}{\sqrt{d}\sqrt{Q_t^3}}
and the right one as \frac{1}{\sqrt{d\mathstrut}\sqrt{Q_t^3}}
.
Small spaces#
Spaces can make equations far more pleasing to the eye. Especially the small space \,
can do wonders. Consider as an example the following
ways to define a set:
The upper one is given by \{ x | x^2 > 5 \}
whereas the lower one has additional spaces around the vertical bar \{ x \, | \, x^2 > 5 \}
which makes it look a lot nicer.
Typesetting operators#
There are already predefined symbols for common functions like \cos
or \sin
which are set upright instead of italics. To make your own, you
should not use \mathrm{fun}
, but instead operatorname{fun}
. The advantage of the latter is that is also takes care of spacing:
The left hand side is \mathrm{fun} \theta
, the right hand side \operatorname{fun} \theta
.