Some standard tricks for LaTeX

There is a bunch of things I have repeatedly been looking up over the last months. Here they are - as a kind of easy-to-find repository of snippets.

Input encoding

\usepackage[utf8]{inputenc}

In general, it is nice to have the source files in UTF-8, since that is a very common standard nowdays. That is what overleaf use, for example. If you use pdfLaTeX to typeset the document, you should use the inputenc package. As you might read at some places on the internet - this makes less sense if youuse xeLaTeX or LuaLaTeX, since these tools always use UTF-8.

Output encoding

\usepackage[T1]{fontenc}

Here you have a simliar issue, but it is worse. HEre is why: A common problem is that copy-paste of the character ⟨ö⟩ yields ⟨”o⟩. That is a typical mistake in font enconding. What looks like a ⟨ö⟩ is actually not encoded as the character ⟨ö⟩. To remedy this, you select a good font encoding. But to make things a little bit worse, you then also need fonts that goes with that encoding. The good simple thing to do is just to use T1 font encoding, and use very standard fonts, and skip all the crazy unicode-emoji-stuff. If you do that, you should be fine.

This post TeX.SE: “Font encoding in LaTeX should elaborate a bit.

Babel

\usepackage[english]{babel}

While were at it, we can comment on babel. It is preferable to choose an english dialect and stick with it to get proper hyphenation etc. If you don’t choose manually, you will get system defaults, and those will probably be wrong.

Page size

I really want a4 paper. And latex default is typically not a4. So I use the article class with a4 paper option.

\documentclass[a4paper]{article}

Code exerpts

\usepackage{minted}
Here is some code: \mintline{python}{x=np.max(X@w}

Here is code from a file:
\inputminted[breaklines,linenos]{python}{main.py}

Use minted. That is simply the best.

It does require shell-escape, since the actual formatting is done by pygments, run in a separate command. Do when using pygments, my current compiler command is

latexmk -pdf -pvc -pdflatex="pdflatex -synctex=1 --shell-escape"

Equation numbering

\usepackage{mathtools}
\mathtoolsset{showonlyrefs}

and then use \eqref{...} instead of \ref{...}.

Updated: