I use LaTeX a lot. And every time I work with other people, I notice things people to weirdly when using LaTeX, not pushing it to its full potential or simply doing things that are “dangerous.” Of course, not dangerous in the sense of physically hurting anybody, but in risking a non-robust document, weird typesetting, or simply an unpleasant document. Thus, I decided to collect this list of “best practices” so that I can refer to it and, of course, to make these things more well-known.
If you want to contribute, feel free to create an issue or submit a pull request. Thanks to Heiko who helped extending this list.
Note that this is a bit different from my LaTeX tips & tricks and a lot more basic, although I will refer to said page from time to time.
But even with these best practices, remember:
You cannot use LaTeX correctly. Use can just use it less wrong.
Best Practices
General
- Compile your document with as few warnings as possible as they actually tell you useful information about your document. Use
silence‘s\WarningFilterif you want to ignore specific warnings to get a cleaner output. Go to full explanation. - Use an acronym package to automatically introduce acronyms appropriately. Use
acronymif you just need the handling (for small documents such as papers) andglossariesif you need a glossary (for large documents such as theses). - Format your source code. Even though LaTeX is not a programming language (yes, it is Turing complete, no, that does not qualify it as a programming language), it is still vital that the document is maintainable. Go to full explanation.
- Read documentation. Most packages have an excellent and extensive written documentation that contains everything you might need. Check out, for instance, the documentation of
glossariesandglossaries-extra. It is extensive (combined, 1766 pages), and extremely well-written. - Use
todonotesand\todoto denote todos in the document. If your document class does not have wide margins, put\setuptodonotes{inline}in the preamble. - Never allow compilation errors to persist in your document. If you are using Overleaf, change
Try to compile despite errorstoStop on first errorin the menu next to theRecompilebutton to remind you of this. Go to full explanation. - Do a clean compile (i.e., delete all auxiliary files) from time to time to check everything is working. Even better, set up continuous integration.
- Use a build tool such as
araraorlatexmkfor building large documents to prevent forgetting crucial steps (e.g., generating glossaries, running BibLaTeX, etc.). Makefiles might also work for small documents. - Know the difference between
\inputand\include, and use both of them appropriately. Go to full explanation. - Instead of formatting dates and times manually, use
datetime2. It respects the language and locale of the document and ensures consistent formatting.
General Typesetting
- Handle inter-word and inter-sentence spaces correctly. Use
\and\@if necessary. Go to full explanation. - Know the difference between hyphen (-), en- (–) and em-dash (—) and use all three appropriately. Go to full explanation.
- Decide whether to reference equations every time (and treat them like non-floating figures) or embedding them into the text (treating them as inline math). In any case, be consistent (and respect the journal‘s guidelines).
- Configure custom hyphenation for words unknown to
babelif necessary. - Use
csquotesfor placing quotation marks. Only use"and'in the code, never‚,‘,’,„,“,”, or similar. - Use
\textsuperscriptand\textsubscriptfor super- and subscripts within text, respectively. Do not use ‘sppontaneous math mode.’ - Use
\emphinstead of\textit,\textbf, or similar for emphasizing.\emphis made for emphasizing, can be configured globally, and toggles the font style rather than setting it (i.e., withing an italic text, emphasized words will become straight). - Place a
~between\citeand the previous word to produce a no-break blank (i.e., a blank where no line break will take place). Otherwise, teh citation might be on the next line which looks weird. - Use
microtypefor microtypography (protrusion and expansion). It enhances your document a lot while being barely noticable.
Layouting
- Do not ignore over-/underfull h-/vbox warnings. That warning is literally telling you that your document looks shitty somewhere. Go to full explanation.
- Touch layouting only if absolutely necessary. Avoid
\vspace,\hspace,minipage, etc. In almost all cases, LaTeX knows what it is doing and you should rather adjust text length/figure size/etc. to get the desired result.
Math
- Do not use
\fracin text/inline math mode. It gets too compressed, increases the line height, and makes the document look horrible. Instead, simply use a/. - Use
\prescript(or so) instead of^2T_1; the latter binds to the wrong symbol - Do not use
^2T_1and similar to typeset²T₁, instead use\prescript{superscript}{subscript}{arg}for super-/subscripts before a symbol, e.g.,\prescript{2}{}{T}_1. Go to full explanation. - Be aware of cool features such as
\underbrace,\mathclap,\phantom, etc. Go to full explanation. - (Debatable) Prefer
\( … \)over$ … $and prefer\[ … \]over$$ … $$. The dollar notation is a TeX primitive and not meant to be used in LaTeX documents. - Use
\DeclareMathOperatorrather than retyping an operator over and over again as this is prone to induce typos. Go to full explanation. - Use
siunitxfor units, numbers, ranges, uncertainties, etc. Got to full explanation. - Use
physicspackage for easier calculus and all kinds of physics typesettings. Go to full explanation. - Do not use
\textfor “operators,” instead use\mathrm. Example: Use\( D_\mathrm{KL} \), not\( D_\text{KL} \). Go to full explanation. - Use left/right versions of symbols, e.g.,
\lvertand\rvertinstead of\vertwhenever semantically reasonable. Go to full explanation. - Use
mathtoolsinstead of plainamsmathas it fixes lots ofamsmath‘s bugs and adds extra functionality. - Do not rely on paranthesis auto-scaling (
\leftand\right), it looks horrible most of the time as the scaling is quite primitive. Instead, use\big,\Big,\bigg, and\Biggand their respective left/right versions\[lr]big,\[lr]Big,\[lr]bigg, and\[lr]Bigg. It is also possible to create bigger versions. - Do not use “plain symbols” such as
||/.../etc.. Instead use\Vert/\dots/etc. Go to full explanation. - Do not use
\[ … \]or$$ … $$, use anequationenvironment instead. - know the differences between
equation,gather, andalign
Referencing
- Label equations only when they are references. This can be done automatically.
- Do not use plain
\refs, usezref-cleverand\zcrefor at least\autoref. Go to full explanation.
Figures
- Figures belong in
figureenvironment, tables intableenvironments, listings in the respective listing environment, etc. Go to full explanation. - Never place figures within text, they should float to the top (or bottom) of the page. Let LaTeX handle this. Go to full explanation.
- Figures, tables, algorithms, listings, etc. are floating objects. Do not enforce figure placement, LaTeX knows what it is doing. Go to full explanation.
- If a small image is tightly bound to a piece of text and does not carry meaning in itself, it is not a figure. Place it in a
centerenvironment and keep it where it is. - Center captions if they are below one line long and left-align them if they are more than one line long. Do so by adding
\captionsetup{singlelinecheck=on}in the preamble.
Tables
- Use
booktabsinstead of standard tabulars. Go to full explanation. - Use
threeparttableto add notes to a table (similar to footnotes in normal text, but withing the floating environment). Go to full explanation. - Use
siunitxfor formatting numbers in tables (such that they align at the decimal point, the uncertainty, or both). Go to full explanation.
Full Explanations
Some of the above items need a longer explanation, which is contained in this section. Note that this section does not contain all best practices and for an exhaustive read, go here.
General
Compile With as Few Warnings as Possible
TODO
Handling Acronyms: acronym
TODO
Handling Acronyms: glossaries
TODO
Code Formatting
TODO
Never Allow Errors to Persist
TODO
Difference Between Input And Include
TODO
General Typography
Handling Inter-Word And Inter-Sentence Spaces
TODO
Hyphen, En-, and Em-Dash
TODO
Layouting
Do Not Ignore Over-/Underfull hbox And vbox Warnings
TODO
Math
Use \prescript
Classic super- and subscript, i.e., ^ and _, bind to the symbol before them such that A ^1T_2 would be rendered as A¹ T₂, not A ¹T₂.
\prescript is from the mathtools package and there are other packages available that do a similar thing, but mathtools is often loaded anyways.
Cool Math Features
TODO
Use \DeclareMathOperator
TODO
Use siunitx For Units
TODO
Use physics Package
TODO
Do Not Use \text For Operators
Using \text changes the font family to the document’s text family while \mathrm retains the math font but removes spacing that would otherwise be placed between letters.
Note that for actual text, \text can still be used and \mathrm removes all spaces (which should never be used in math operators anyway).
Use Left/Right Versions of Symbols
TODO
Do Not Use Plain Symbols
TODO
Referencing
Do Not Use Plain \ref
TODO
Figures
Figure Placement
TODO
Tables
Use booktabs
TODO
Use threeparttable For Notes
TODO
Use siunitx For Tables
TODO
Notes
This is an unstructured list of things that will be added to list of best practices, but they are not yet formulated well enough. For completeness and transparency, I will keep them here.
- a multi-line equations that should get a single number should be an
alignedenvironment inside oneequation - prefer
tikzplotliboverpgfoverpdfoverpngwhen building figures- really never use
png tikzrenderings can be cached to speedup compilation time
- really never use
- place every sentence on a new line to make finding compiler errors easier (opinionated)
\/- Use the biblatex package with the Biber backend instead of natbib (or even bibtex directly)
- use xspace for proper spacing after a stop
- Typeset numbers in math font (using siunitx) off they are ‘technical.’ E.g., `we used a learning rate of \num{0.12} and we are 14 people in our lab.``
- Know that you can tell latex to just compile some
\includeswhile still reading the aux file for labels, saving a ton of compilation time during writing - don’t define labels multiple times (ideally create labels in the moment you want to reference them)
- make sure no figures is left unreferences
- make sure no unresolved references remain (read the warnings)
- whenever you feel like you need a
[vh]space, think again; you probably do not need it- want to space out two parts of an equation? use align
\quadand\qquadare also great alternatives to provide typographically sound spacing
- use linewidth not textwidth