# Top Tips for New LaTeX Users

This article is aimed at relatively new $\LaTeX$ users. It is written particularly for my own students, with the aim of helping them to avoid making common errors. The article exists in two forms: this WordPress blog post and a PDF file generated by $\LaTeX$, both produced from the same Emacs Org file. Since WordPress does not handle $\LaTeX$ very well I recommend reading the PDF version.

## 1. New Paragraphs

In $\LaTeX$ a new paragraph is started by leaving a blank line.

Do not start a new paragraph by using \\ (it merely terminates a line). Indeed you should almost never type \\, except within environments such as array, tabular, and so on.

## 2. Math Mode

Always type mathematics in math mode (as $..$ or $$..$$), to produce “ $y = f(x)$” instead of “y = f(x)”, and “the dimension $n$” instead of “the dimension n”. For displayed equations use $$, $..$, or one of the display environments (see Section 7). Punctuation should appear outside math mode, for inline equations, otherwise the spacing will be incorrect. Here is an example. • Correct: The variables x, y, and z satisfy x^2 + y^2 = z^2. • Incorrect: The variables x, y, and z satisfy x^2 + y^2 = z^2. For displayed equations, punctuation should appear as part of the display. All equations must be punctuated, as they are part of a sentence. ## 3. Mathematical Functions in Roman Mathematical functions should be typeset in roman font. This is done automatically for the many standard mathematical functions that $\LaTeX$ supports, such as \sin, \tan, \exp, \max, etc. If the function you need is not built into $\LaTeX$, create your own. The easiest way to do this is to use the amsmath package and type, for example, \usepackage{amsmath} ... % In the preamble. \DeclareMathOperator{\diag}{diag} \DeclareMathOperator{\inert}{Inertia}  Alternatively, if you are not using the amsmath package you can type \def\diag{\mathop{\mathrm{diag}}}  ## 4. Maths Expressions Ellipses (dots) are never explicitly typed as “…”. Instead they are typed as \dots for baseline dots, as in x_1,x_2,\dots,x_n (giving $x_1,x_2,\dots,x_n$) or as \cdots for vertically centered dots, as in x_1 + x_2 + \cdots + x_n (giving $x_1 + x_2 + \cdots + x_n$). Type ith instead of i'th or i^{th}. (For some subtle aspects of the use of ellipses, see How To Typeset an Ellipsis in a Mathematical Expression.) Avoid using \frac to produce stacked fractions in the text. Write $n^3/3$ flops instead of $\frac{n^3}{3}$ flops. For “much less than”, type \ll, giving $\ll$, not <<, which gives $<<$. Similarly, “much greater than” is typed as \gg, giving $\gg$. If you are using angle brackets to denote an inner product use \langle and \rangle: • incorrect: <x,y>, typed as <x,y>. • correct: $\langle x,y \rangle$, typed as \langle x,y \rangle ## 5. Text in Displayed Equations When a displayed equation contains text such as “subject to $x \ge 0$”, instead of putting the text in \mathrm put the text in an \mbox, as in \mbox{subject to x \ge 0}. Note that \mbox switches out of math mode, and this has the advantage of ensuring the correct spacing between words. If you are using the amsmath package you can use the \text command instead of \mbox. ### Example $$
\min\{\, \|A-X\|_F: \mbox{$X$ is a correlation matrix} \,\}.
$$ ## 6. BibTeX Produce your bibliographies using BibTeX, creating your own bib file. Note three important points. • “Export citation” options on journal websites rarely produce perfect bib entries. More often than not the entry has an improperly cased title, an incomplete or incorrectly accented author name, improperly typeset maths in the title, or some other error, so always check and improve the entry. • If you wish to cite one of my papers download the latest version of njhigham.bib (along with strings.bib supplied with it) and include it in your \bibliography command. • Decide on a consistent format for your bib entry keys and stick to it. In the format used in the Numerical Linear Algebra group at Manchester a 2010 paper by Smith and Jones has key smjo10, a 1974 book by Aho, Hopcroft, and Ullman has key ahu74, while a 1990 book by Smith has key smit90. ## 7. Spelling Errors and $\LaTeX$ Errors There is no excuse for your writing to contain spelling errors, given the wide availability of spell checkers. You’ll need a spell checker that understands $\LaTeX$ syntax. There are also tools for checking $\LaTeX$ syntax. One that comes with TeX Live is lacheck, which describes itself as “a consistency checker for LaTeX documents”. Such a tool can point out possible syntax errors, or semantic errors such as unmatched parentheses, and warn of common mistakes. ## 8. Quotation Marks $\LaTeX$ has a left quotation mark, denoted here \lq, and a right quotation mark, denoted here \rq, typed as the single left and right quotes on the keyboard, respectively. A left or right double quotation mark is produced by typing two single quotes of the appropriate type. The double quotation mark always itself produces the same as two right quotation marks. Example: $hello''$ is typed as \lq\lq hello \rq\rq. ## 9. Captions Captions go above tables but below figures. So put the caption command at the start of a table environment but at the end of a figure environment. The \label statement should go after the \caption statement (or it can be put inside it), otherwise references to that label will refer to the subsection in which the label appears rather than the figure or table. ## 10. Tables $\LaTeX$ makes it easy to put many rules, some of them double, in and around a table, using \cline, \hline, and the | column formatting symbol. However, it is good style to minimize the number of rules. A common task for journal copy editors is to remove rules from tables in submitted manuscripts. ## 11. Source Code $\LaTeX$ source code should be laid out so that it is readable, in order to aid editing and debugging, to help you to understand the code when you return to it after a break, and to aid collaborative writing. Readability means that logical structure should be apparent, in the same way as when indentation is used in writing a computer program. In particular, it is is a good idea to start new sentences on new lines, which makes it easier to cut and paste them during editing, and also makes a diff of two versions of the file more readable. ### Example: Good: $$
U(\zbar) = U(-z) =
\begin{cases}
-U(z),   & z\in D, \\
-U(z)-1, & \mbox{otherwise}.
\end{cases}
$$ Bad: $$U(\zbar) = U(-z) =
\begin{cases}-U(z),   & z\in D, \\
-U(z)-1, & \mbox{otherwise}.
\end{cases} ## 12. Multiline Displayed Equations For displayed equations occupying more than one line it is best to use the environments provided by the amsmath package. Of these, align (and align* if equation numbers are not wanted) is the one I use almost all the time. Example: \begin{align*} \cos(A) &= I - \frac{A^2}{2!} + \frac{A^4}{4!} + \cdots,\\ \sin(A) &= A - \frac{A^3}{3!} + \frac{A^5}{5!} - \cdots, \end{align*}  Others, such as gather and aligned, are occasionally needed. Avoid using the standard $\LaTeX$ environment eqnarray, because it doesn’t produce as good results as the amsmath environments, nor is it as versatile. For more details see the article Avoid Eqnarray. ## 13. Synonyms This final category concerns synonyms and is a matter of personal preference. I prefer \ge and \le to the equivalent \geq \leq\ (why type the extra characters?). I also prefer to use .. for math mode instead of $$..$$ and .. for display math mode instead of $..$. My preferences are the original $\TeX$ syntax, while the alternatives were introduced by $\LaTeX$. The slashed forms are obviously easier to parse, but this is one case where I prefer to stick with tradition. If dollar signs are good enough for Don Knuth, they are good enough for me!

I don’t think many people use $\LaTeX$‘s verbose

\begin{math}..\end{math}


or

\begin{displaymath}..\end{displaymath}


Also note that \begin{equation*}..\end{equation*} (for unnumbered equations) exists in the amsmath package but not in in $\LaTeX$ itself.

# More Tips on Book and Thesis Writing

Following my earlier post Top Five Tips on Book Writing, here are seven more tips. These apply equally well to writing a thesis.

## 1. Signpost Citations

In academic writing we inevitably include a fair number of citations to entries in the bibliography. In a book, even more so than in a paper, we do not want the reader to have to turn to the bibliography every time a citation is reached in order to understand what is being cited. So a sentence such as

The matrix logarithm appears in a wide variety of applications
, , .


The matrix logarithm appears in a wide variety of applications,
such as reduced-order models , image registration ,
and computer animations .


Versions of the algorithm have been developed by several authors
, , .


I would write

Versions of the algorithm have been developed by Chester ,
Hughes [3, Sec. 2], and Smith and Jones .


Even that example lacks information about the date of publication. In my books I have used my own version of the $\LaTeX$ \cite macro that allows me to include the year:

Versions of the algorithm have been developed by Baker and
Chester [1, 2006], Hughes [3, 2001, Sec. 2],
and Smith and Jones [7, 2004].


The macro is

\def\ycite[#1#2#3#4#5]#6{\cite[$\mit{#1#2#3#4}$#5]{#6}}


(which puts the year in the distinctive math italic font) and the first two citations in the previous sentence would be typed as \ycite{bach06} and \ycite[2001, Sec.~2]{hugh01}.

## 2. Produce a Good Index

A good index is essential, since it is the main way that readers can find content. The vast majority of books that I read have an inadequate index, as I have noted in my post A Call for Better Indexes at SIAM Blogs. Usually the index is too small. Occasionally the index is of about right length but is flawed. The main problems are

• Items that should be indexed are absent from the index.
• An index entry does not point to all (significant) occurrences of the term.
• Related entries are not grouped properly.

Advice on producing an index can be found in Section 13.4 of my Handbook of Writing for the Mathematical Sciences and various other sources (try a Google search), and I intend to wrote a post on indexing soon. $\LaTeX$, through its \index command, used in conjunction with the MakeIndex program, provides an excellent way to produce an index.

## 3. Use the Backref $\LaTeX$ package

Backref.sty is a $\LaTeX$ package that adds to each bibliography entry the text “cited on pages” and then lists the pages on which that item was cited. It costs nothing to use it, but it adds great value to the bibliography, which then functions as a separate index into the book. I started using backref with my book MATLAB Guide (2005). To a large extent it removes the need for an author index, and if I do a third edition of Accuracy and Stability of Numerical Algorithms I will probably use backref and drop the author index.

The backref package is not widely used, though a number of SIAM books have made use of it.

For a book provided in PDF form, hyperlinks from an equation reference to the equation, a citation to the bibliography entry, a URL to the web page, and so on, are a great aid to the reader. In $\LaTeX$ obtaining the hyperlinks is usually just a matter of adding \usepackage{hyperref} in the pre-amble.

## 5. Make Figures Readable and Consistent

It’s very easy nowadays to produce figures containing plots of functions or computational results. But it’s much harder to produce a set of figures that

• are clearly legible,
• have labels, legends, and annotations that are of similar size to the main text,
• are consistent in format (axes, line thicknesses etc.)

All too often I see figures in which the text is so small that I cannot read it at a normal reading distance. My experience (which is mainly with MATLAB, and with the $\LaTeX$ packages TikZ and PGFplots) is that it is a time-consuming process to produce high quality plots. But it is worth the effort.

## 6. Use Short Captions in the List of Figures/Tables

The general form of the $\LaTeX$ caption command is \caption[short caption]{long caption}. The short caption is what is printed in the List of Figures or List of Tables at the front of the book, if you are printing those lists. The short caption will be read in isolation from the figure or table so it should omit all unnecessary detail, such as explaining line or marker types. All too often, the short and long captions are the same, resulting in unnecessarily long and detailed lists of figures or tables.

Here is an example (simplified, with other macros removed) of the caption from a figure in my book Functions of Matrices:

\caption[Illustration of condition (b) of Theorem~11.4.]%
{Illustration of condition (b) of Theorem~11.4,
which requires every eigenvalue of $B$ to lie in the
open half-plane (shaded) of the corresponding eigenvalue
of $A^{1/2}$.}


## 7. Make the Header Contain the Section and Chapter Number and Title

I like to know where I am when I am reading a book, so I expect the page headers to tell me the section number and chapter number, and preferably their titles as well. I cannot understand why some books omit this information. Without it, phrases such as “as discussed in the previous chapter” become harder to follow up, and searching for a particular section is more difficult.

# How To Typeset an Ellipsis in a Mathematical Expression

In mathematical typesetting we often use an ellipsis (three dots) to denote omission in an expression. It’s well known to $\LaTeX$ users that an ellipsis is not typed as three dots, but rather as \dots or \cdots. The vertically centered \cdots is used between operators that sit above the baseline, such as +, -, = and $\le$. Ground level dots are produced by \dots and are used in a list or to indicate a product.

Recently the question arose of whether to write

$a_1$, $a_2$, \dots, $a_n$


or

$a_1, a_2, \dots, a_n$


The difference between these two does not show up well if I allow WordPress to interpret the $\LaTeX$, but as this PDF file shows the first of these two alternatives produces more space after the commas.

I don’t discuss this question in my Handbook of Writing for the Mathematical Sciences, nor does the SIAM Style Guide offer an opinion (it implies that the copy editor should stet whatever the author chooses).

As usual, Knuth offers some good advice. On page 172 of the TeXbook he gives the example

The coefficients $c_1$, $c_2$, \dots, $c_n$ are positive.


the justification for which is that the commas belong to the sentence, not the formula. (He uses \ldots, which I have translated to \dots, as used in $\LaTeX$.) In Exercise 18.17 he notes that this is preferred to $c_1, c_2, \dots, c_n$ because the latter leaves too little space after the commas and also does not allow line breaks after the commas. But he notes that in a more terse example such as

Clearly $a_i<b_i$ \ $(i=1,2,\dots,n)$


the tighter spacing is fine. Indeed I would always write $i=1,2,\dots,n$, because $i=1$, $2$, \dots, $n$ would be logically incorrect. Likewise, there is no alternative in the examples

$D = \diag(d_1,d_2,\dots,d_n)$
$f(x_1,x_2,\dots,x_n)$


Looking back over my own writing I find that when typesetting a list within a sentence I have used both forms and not been consistent—and no copy editor has ever queried it. Does it matter? Not really. But in future I will try to follow Knuth’s advice.

# More LaTeX and Beamer Tips

Following my earlier posts Top 5 Beamer Tips and Fine-Tuning Spacing in LaTeX Equations I offer four further tips that I’ve been using over the last few months.

## 1. Platform-independent specification of paths

I use both Windows and Mac machines and want my LaTeX files to run equally well on both. The only difficulty arises when a file is included from another directory (usually a PDF or jpeg file or a program listing). If the file name is specified relative to the current location then forward slash syntax can be used and works on both Mac and Windows. For example

\includegraphics[width = 8cm]{../figs/fig1.pdf}


However, if files are being pulled in from many different directories it can be tedious to specify relative paths and absolute paths are preferable. The problem is that the home directory is system-dependent. My solution is to use the ifplatform package as follows:

\usepackage{ifplatform}
...
\ifwindows\def\home{d:/}\else\def\home{/Users/nick/}\fi
%              Windows            Mac or Linux
...
\includegraphics[width = 8cm]{\home/tex/figs/fig1.pdf}


The \ifwindows construct sets up \home, which is then used as the prefix to the paths of the files to be included. (I initially tried using \home{~} for the Mac, but could not find a way of making this work, since LaTeX interprets the tilde as a hard space.)

## 2. Spacier Lists

If a Beamer slide consists mostly of a list and has lots of space below the list it can aid readability if the list is opened up a little, by increasing the spacing between items. This can be done as follows:

\begin{itemize}

\item
...

\end{itemize}


## 3. Code listings

The standard verbatim environment does not look very good in Beamer, as the font it uses is rather thin. I prefer to use the Verbatim environment provide by fanycvrb, which allows me to customize the font style and color:

\usepackage{fancyvrb}
\begin{Verbatim}[fontseries=b,formatcom=\blue]
...
\end{Verbatim}


This example uses a bold font in blue. Lots of other options are available for customizing the way the Verbatim environment is rendered.

In Emacs, with the RefTeX package, the command reftex-toc produces a table of contents for a LaTeX document, allowing navigation by sectional unit. By default, this command does not know about Beamer. You can tell reftex-toc about Beamer frames by adding the following to your .emacs, after (require 'reftex):

  (setq reftex-section-levels
(append '(("begin{frame}" . -3) ) reftex-section-levels))


This works even if the frame title is set with the \frametitle command.

# Top 5 Beamer Tips

Here are my top 5 tips for using Beamer, the popular LaTeX package for producing slides.

I’ve seen hundreds of talks with slides prepared in Beamer. I’ve yet to see anyone use the navigation symbols that Beamer puts at the bottom right-hand corner of the screen. These should be turned off with

\setbeamertemplate{navigation symbols}{}


### 2. Extra Slides

If you include extra slides at the end of your talk, you can stop them affecting the page count (usually shown in the footer) as follows:

\usepackage{appendixnumberbeamer}
...
\appendix
\begin{frame}{First Extra slide}
...


The appendixnumberbeamer package might not be included in your LaTeX distribution, in which case you can download it from CTAN.

### 3. References

You can include references in your slides using the following code, best placed after \appendix:

\begin{frame}[allowframebreaks]{References}
\def\newblock{}
\bibliographystyle{plain}
\bibliography{mybib}
\end{frame}


### 4. Sections

In a longer talk it can be useful to break the talk into sections and produce an outline slide at the start of each section. This can be done by putting in the preamble the code

\setbeamerfont{myTOC}{series=\bfseries,size=\Large}
\AtBeginSection[]{\frame{\frametitle{Outline}%
\usebeamerfont{myTOC}\tableofcontents[current]}}


and then typing \section[short_title]{long_title} between frames, where short_title is used for the headline (the line at the top of the screen that can contain various types of information, intended to help the audience know where you are in the talk). In this code I have increased the size of the font used for the outline and made it bold. If you don’t want the headline it can be turned off with

\setbeamertemplate{headline}{}


### 5. Columns

A good way to line up graphics side by side, especially if they are of different heights, is with Beamer’s columns environment:

\begin{columns}[c] % Columns centered vertically.
\column{5.5cm}     % Adjust column width to taste.
\includegraphics ...
\column{5cm}
\includegraphics ...
\end{columns}


# Fine-Tuning Spacing in LaTeX Equations

For several years I’ve been fine-tuning the spacing in LaTeX equations using a tip from Donald Knuth (the same tip works with TeX). He mentioned it in a question and answer session that he gave in 1996 in Holland and which is written up in Digital Typography (1999, p. 641). The idea, which he uses in The Art of Computer Programming, is to write

% Make @ behave as per catcode 13 (active).  TeXbook p. 155.
\mathcode@="8000
{\catcode\@=\active\gdef@{\mkern1mu}}


Then within a formula the @ symbol acts as a macro that inserts 1mu of space. A mu is a math unit. For comparison, a thinspace,

\,


represents 3mu and a \quad 8mu.

Knuth gives the example \sqrt{@\log n} to produce the right amount of space before the l of log.

Some examples of my use of @ from my Functions of Matrices book are

\|@@|A^{-1}|@@\|_F
\sqrt{A}^{@@\pm 1}f(\sqrt{A})
f(uv^*) = f(0)I + f[v^*u,0] @@ uv^*
p_{km}(x) = \sum_{j=0}^k \frac{ (k+m-j)!@@ k! }{ (k+m)!@@ (k-j)! }


There aren’t really any rules; the use of @ is largely a matter of taste as to what looks right.

This level of fine-tuning is not something I do as a matter of course in drafts, or with any rigour in papers, but for a book I think it is well worth the effort.

Knuth also points out that $n^2/3$ looks better with a negative thin space, as $n^2\!/3$, so that there is less space between the superscript and the slash. On checking the LaTeX source I notice that I did this in my book, but had forgotten about it until I wrote this post!