Hints and tips on working with Latex
I work with a lot of people who use latex day to day to write documents. I see a lot of the same errors happening over and over again and this is my attempt to try and get those fixed. Therefore, this is not an article about how to do all the fun exciting things in Latex (like build diagrams with TikZ), but instead how to avoid common issues and get the most out the tool. Note that this is written predominantly as a guide to students submitting BSc/MSci projects.
Overleaf
Obviously, Overleaf is great and a revolution. However, it has one feature that has lost many a student marks on their final report: by default it will Try to compile despite errors. As a result, new users often make mistakes which simply appear as a little red flag at the top. But, it still produces output. So no worries. However, that red flag is telling you your latex code is broken in a fundamental way. Overleaf is very clever and even tries to fix it. However, these fixes are usually mistaken and end up with your output containing nonsense. Please make sure that you fix errors in your OverLeaf.
The grammar of equations
Equations are part of sentances (see, e.g. this style guide). As such, they should be treated as part of the paragraph. For example, your latex source code may look like:
Newton's law of gravitation is
$$ F = G\frac{m_1 m_2}{r^2} \, , $$
where $m_1$ and $m_2$ are the mass of the two objects, $G$ is the gravitational constant, and $r$ is the separation between the masses.
A few notes on this:
- After the equation, we add
\, ,
. The first part\,
is adding a space, the second is adding a comma. - the
where
continues the sentance. Do not add a space between the equation and the next line otherwise latex will treat this as a new paragraph (usually ending up with an indentation).
Alternatively, the equation may be the end of the sentance, in which case it would finish as a full stop like this:
Given two masses $m_1$ and $m_2$ separated by $r$, Newton's law of gravitation can be expressed as
$$ F = G\frac{m_1 m_2}{r^2} \, . $$
Text-mode subscripts
Often, we use subscripts to denote some property or other. For example, let's say you want to define the initial velocity as $v_{init}$
. The init
here will be in math mode (i.e. italics). Formally, it should be intepreted as i
times n
times i
(etc), which is not what you mean. Therefore you should either:
- Use text mode
$v_\text{init}$
(or equivalent) - Use the subscript package
Use packages
There are a number of very useful packages that I highly recommend everyone use by default whenever they write a latex document. These are:
- acronym. Use this to ensure that you correctly introduce acronyms on their first usage and only use them once. Never handle acronyms manually, instead include
\usepackage{acronym}
. Then define the acronymThen whenever you want to use the acronym, inster\newacro{GW}{Gravitational Wave}
\ac{GW}
. On the fist use this will resolve toGravitational Wave (GW)
. - cleverref. Use this to avoid writing
In Fig.~\ref{fig:X} we see..
(which is hard to keep consistent). Instead simply writeIn \cref{fig:X} we see
. You can set the choice of capitalization with an optioni on import:\usepackage[capitalise]{cleveref}
- hyperref. Use this to turn links on. Here is some setup to control the color:
\usepackage{hyperref}% add hypertext capabilities \hypersetup{ colorlinks, linkcolor={red!50!black}, citecolor={blue!90!black}, urlcolor={blue!80!black} }
Use macros
If you have a compicated peice of latex that you are using in multiple places, create a macro. For example, if I wanted to keep referring to the software sklearn
but I needed to use texttt
to match the journal style, I would define
\newcommand{\sklearn}{\texttt{scikit-learn}}
You can also do the samething for mathematical symbols. This is expecially useful if you keep changing your mind on which symbol to use: define it once and then you can change it whenever you like. If you want to use it in the text as well, you can use \ensuremath
like this:
\newcommand{\pastro}{\ensuremath{p_{\rm astro}}\xspace}
\pastro
in an equation or in the text directly.
It is typically a good idea to group together all your macro's and acro's at the top of the document in a single block of definitions.
Textual and parenthetic citations
There are two different types of citations, textual and parenthic. Please this, e.g. this guide. To understand the difference.
Then, if your bibliography style allows it, use the write one by using \citep
for parenthetic and \citet
for textual (assuming you are using natbib
).