Some LaTeX beginners’ mistakes and tips

I didn’t post last week and can’t really guarantee consistent posts for the rest of the month; it’s exam period now. Normal service will resume in June. For today, I’m just going to go over a few things that can make early attempts at LaTeX typesetting look much more professional. At my university, there’s virtually no advice on using LaTeX until the student starts their masters projects, so here’s how to get things looking swish as early as possible.

1. The biggest mistake: trying to micromanage the layout like a Word document

The whole point of the typesetting system is that it relieves you of having to do this. Your job is just to type the document, with a suitable choice of document class, and use simple commands to logically organize your document like \chapter, \includegraphics, and \cite and so on.

You should basically never have to manually control things like line spacing, indentation, or font size. I have seen source files with dozens of new lines (\\\\\\\\\) at the end of each paragraph. Now it is fair enough that you might want the final output of your document to use blank lines to delimit paragraphs instead of indentation. But the solution is still to type out your document as normal, and then search for how you would change this in the final output. In this situation, incidentally, the correct solution is to add

\usepackage{parskip}

to the preamble of your source file.

2. Messy spaces

There are a few little quirks that can make the spacing in your document go a little bit crazy. The first thing is to be aware that in your source file, a single new line is essentially treated by the compiler like a space; indeed some authors write their source files with every sentence on a new line so that it is easier to edit. A double new-line is treated the same as \par; ie, a new paragraph. You should be careful with this. If you are formatting an equation in a “display math” environment, it should be treated as part of the current paragraph, not as a paragraph unto itself. Therefore, don’t insert blank lines before and after the equation in the source file; it may look “right” in the source file because there is a little blank space either side of the equation in the output, but actually, you’re creating new paragraphs. In particular, you’ll have unwanted indentation in the following line.

You should also be aware that there are several ways to insert and remove whitespace in both text and math modes. \! removes some whitespace, inserts a space, \, inserts a short space, and \quad inserts a rather long space. Here are some use-cases:

  • When you complete a sentence in LaTeX, the space following the full stop is longer than a standard space. This has the side effect that if you write something like “i.e.”, the next word will be spaced incorrectly. You an fix this by manually inserting the correct length of space with i.e.\ .
  • I find short spaces are useful in math equations involving differential forms, or any other time you want to multiply entities whose symbols consist of more than one character. If I have a 1-form like u\,\mathrm{d}x, you’ll notice this looks a lot nicer that u\mathrm{d}x. That’s because I inserted a short space \,

3. Quotations, delimiters, and dashes

In LaTeX, we write left and right quotes with `like this'or ``like this'', Not using the character “. It will look wrong if you do. There are also three lengths of dashes available: - for a hyphen, -- for an en-dash and --- for an em-dash (and of course in math mode, - inserts a minus sign. It’s worth reading up on when to use each of these kinds of dash if you’re not sure.

When formatting mathematics, it’s common to want to have brackets, braces, and such in different sizes. For example

    \[( \frac{a}{b} )\]

looks terrible, but

    \[\left( \frac{a}{b} \right)\]

does not. The answer is to use \left( and \right) to get the delimiters to resize depending on what is inside them. You can replace the ( and ) in the previous commands with any other delimiter as you please. Note that to get braces, \{ and \} are required.

So should you just use \left( and \right) all the time? The answer is no. For one, it’ll make your source file harder to read. But more importantly, due to some quirk in how these things work, it can screw up the spacing. Essentially, the TeX compiler works hard to make all your text nice and flush with the margins and so on. This means subtly adding and removing whitespace on every line so that it looks just right, even from math equations. For some reason, any math enclosed in these dynamic delimiters is off-limits in this process; the TeX compiler can’t add or remove whitespace inside \left\right pairs, so there’s a good chance that any line containing these symbols will not look quite right. So, use it only when necessary. If things start looking bad, remember you can manually set the size of a delimiter with commands like \big, \bigg and so on.

I really hope these issues with delimiters are eventually fixed in a later version of LaTeX.

4. sin(x)

You should never, ever, type full words in math-mode because it looks absolutely terrible. However, words do sometimes arise inside mathematical formulas. Usually, just using \text{ your words here } is enough. If your word is a mathematical operator, such as the sin appearing in the titie, then there are two possibilities. Either one of the standard mathematical packages like amsmath will contain that operator as an actual command, in which case you should format it like \sin{(x)}; or you can define the operator yourself (again using amsmath) like this \operatorname{sin}{(x)}. Note the braces around the argument in both cases; this sets the spacing correctly. It is the difference between \sin (x) and \sin{(x)}.

5. Not defining stuff

When writing any large mathematical document, there are certain symbols you will be using again and again. Your LaTeX preamble should be absolutely stuffed with ways to make these easier to type. The three most useful are \newcommand, \renewcommand, and \DeclareMathOperator. The first two work in the same way, except that \renewcommand allows you to overwrite existing commands. I mostly use this to overwrite built in commands, like usually \P inserts the paragraph symbol \P, but I’ve never used that, so I set it to \mathbf{P}, symbolizing a projective space. The \DeclareMathOperator command is like \operatorname except that you can reuse the operator. So for example, I can write \DeclareMathOperator{\trdg}{tr.d} to create a transcendence degree operator, and this means it has the nice font and spacing that you’d expect from an operator whenever I now write \trdg. Almost everybody I know has commands for the common symbols \Z, \R, \C, and so on.

Commands can also take arguments. It’s easy to explain this with an example

\newcommand{\prtl}[2]{\frac{\partial #1}{\partial #2}}

is the command I use for partial derivatives. The [2] means it expects 2 arguments, and they’re referenced by #1 and #2.

6. Using a bad editor

If you’re at university and you’ve been asked to produce a LaTeX document for the first time, the chances are you’ve just been told to launch some software pre-installed on the university machines and given a brief explanation of how to start writing and which button to use to compile the document. You might not bad aware that LaTeX is simply a language, which can be edited using any software you like, and that there are many free TeX compilers out there that can produce your document for you. So don’t just accept whatever software you’ve been given: look around for software that suits you, and configure it to your tastes. You could use a high-performance programming text editor like vim, emacs, or atom (all 3 of these have plug-ins for compiling and previewing your document from inside the editor), or you could use something like TeXstudio, a LaTeX document development environment with every LaTeX-related feature you could possibly want (except perhaps a vim mode). At the moment I’m using vim with the vimtex plugin, but I am flirting with the idea of emacs.

7. Choosing the wrong document class and/or environments

There are different document classes for a reason. Don’t use “article” for everything. There are lots of different classes to choose from, some standard and some provided by additional packages. Writing a report? Use the report class!

Similarly, choose the right environment for the task. There are environments for lots of common task. In particular for mathematics students, don’t just type \textbf{Theorem 2} to start a theorem or something. There are theorem and proof environments provided by the amsthm. I can’t go into what all of these are here, but there will almost certainly be an environment for whatever you want to produce. Just take the time to look for the required packages.

I will plug the wonderful environment IEEEeqnarray provided by the package IEEEtrantools. This is the equation environment to end all equation environments as far as I’m concerned. It’s just an extremely flexible equation array environment, which gives you full control over the alignment and spacing of every column, the numbering of every row, and so on. This is the answer for anyone who has ever tried to wrestle with the align or eqnarray enviroments, only to have their equations spread out horribly across the page. I include a link to a use manual for this enviroment at the bottom.

(Boring one) 8. Not using \usepackage[utf8]{inputenc}

You won’t appreciate this until you’ve tried to type M\"{o}bius transformation or B\'{e}zout's identity a few times. With this input encoding you can just type Möbius transformation.


Links related to this article.