# Documents with typical LaTeX errors

I am giving a LaTeX course and have the idea to let the students find and solve problems within a few documents. Basically they shall learn how to create minimal examples and read error messages.

However currently I have no document with 'typical' errors around.

If someone has such examples from himself or within this forum I would like to have the code or link. It does not matter if its short or long. It should just be enough to let the students play with it for some minutes.

-
One of the community polls on meta dealt with 'often made' errors. You can find it here: meta.tex.stackexchange.com/questions/1564/tex-community-polls/… –  Roelof Spijker Nov 2 '11 at 16:14
unfortunately that list is too special and has no examples. I was rather thinking about incompatible packages, missing braces, missing length, undefined controll sequences and such. –  Matthias Pospiech Nov 2 '11 at 16:20
tex.stackexchange.com/questions/14739/… and offer a reward for the one that can hit the Hercules Poirot one, hehe... I see you having fun. –  Yiannis Lazarides Nov 2 '11 at 16:23
related blog post: johndcook.com/blog/2010/02/15/top-latex-mistakes –  doncherry Nov 4 '11 at 18:09
In this case the counter errorcontextlines is very important. –  Marco Daniel Dec 30 '11 at 10:53

Here is a list:

1. When intermixing text and math, some people use \mbox to box the text-related stuff. The reason being that \mbox necessarily switches to text mode. So, if you have math content within \mbox, you need to explicitly restate its use. Here's a short example (causing a Missing $inserted error): \documentclass{article} \begin{document} $f(x) = ax^2 + bx + c, \mbox{where x \geq 0}$ \end{document}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​  The symbol \geq requires math mode. Moreover, the spacing is off if the math content is not set in math mode (but that's typographic in nature). This is error is fixed by using something like \mbox{where$x\geq 0$}. Better yet, the amsmath package provides \text which scales the text to the appropriate font size based on its context while \mbox does not. 2. Code readability is encouraged, which is sometimes synonymous with leaving white space within your code. In the following elementary example, however, leaving (too much) white space causes an error (Missing$ inserted):

\documentclass{article}
\begin{document}
$f(x) = ax^2 + bx + c$
\end{document}​​​​​​​​​​


(Sure, any one blank line removed in the above would also cause the problem. But it seems more hidden due to a symmetric blank line around the equation.)

This can be avoided by either removing the blank lines, or introducing comment characters %. See Blank lines in align environment.

3. When typesetting regular delimiters, there is no need to match the pairs. For example, you can have expressions like (a,b] or even |a,b. However, when using extensible delimiters you need to define a pair using \left and \right. If you want to only use a single extensible delimiter, then you still have to define a paired delimiter using ., as in the following example:

\documentclass{article}
\begin{document}
$f(x) = \left\{\begin{array}{@{}ll} ax^2 + bx + c, & x < 0 \\ dx^2 + ex + f, & x \geq 0 \end{array}$
\end{document}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​


In the above example, even though no right delimiter is needed, it has to be specified using \right..

On that note, the following is also a common mistake leading to errors when delimiting using { } (as in the above example - forgetting to escape {):

\documentclass{article}
\begin{document}
$f(x) = \left{\begin{array}{@{}ll} ax^2 + bx + c, & x < 0 \\ dx^2 + ex + f, & x \geq 0 \end{array}\right.$
\end{document}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​


The correct usage is \left\{.

Or, if you plan on using a notation like ]a,b[ (for whatever reason) and you want extensible delimiters, using \right] and then \left[ just because the brackets face outward seems logical, but doesn't work:

\documentclass{article}
\begin{document}
$f(x) = \right]a,\frac{b}{c}\left[$
\end{document}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​


Left delimiters use \left and right delimiters \right, regardless of the delimiter orientation.

4. Taken from the UK TeX FAQ entry "No line here to end":

It is definitely intuitive to use \\ to cause a line break. And, this works in some cases. However, in other cases, LaTeX complains about the fact that There's no line here to end. The proposed complication is a purely unexpected line break on LaTeX's end.

\documentclass{article}
\begin{document}
\begin{description}
\item[Very long label] \\
Text...
\end{description}
\end{document}


In the above example, the proposed quick-fix solution is to modify the line break \\ to \hspace*{\fill} \\.

Here is another example:

\begin{center}
First (heading) line \\ \\
body of the centred text...
\end{center}


A solution is provided by modifying the double line breaks \\ into a single line break with an optional length skip \\[\baselineskip].

5. You are learning how to write macros or your own commands and try something like:

\documentclass{article}
\newcommand{\i}{$\iota$}% My macro: \i -> \iota
\begin{document}
This is \verb!\i!:~\i.
\end{document}


only to see LaTeX complain Command \i already defined. But you never defined it in any other way, how can it be defined already?! The reason is that the document class itself can define certain commands, just like any package that is loaded can define its own macros (and environments). Moreover, TeX also defines some macros by default (around 900 or so). Specific to this case, it is possible to see what \i was defined as previously and how to correct for this new definition using

\documentclass{article}
\begin{document}
This is \verb!\i!:~\i. \par
\renewcommand{\i}{$\iota$}% My macro: \i -> \iota
This is \verb!\i!:~\i.
\end{document}


or one can use \show\i and inspect the .log file to see the "formal definition."


6. You find a snippet of LaTeX code online and wrap it into a minimal document:

\documentclass{article}
\newcommand{\my@macro}[1]{-#1-}% This is magic
\begin{document}
\my@macro{hi}
\end{document}


However, LaTeX is not happy and complains about Missing \begin{document}. Obviously it's there... so what's the problem? Well, the @ symbol is a reserved when it comes to macro definitions and can't be used without some precaution.

The category code for @ needs to be changed in order for it to be used in a macro definition. Using \makeatletter will "make @ have 'letter'/11 as a category code" so that it can be used. Usually, it is accompanied by a \makeatother counterpart, which "makes @ have 'other'/12 as a category code".

7. You wish to define a command that does some formatting and decide to define a similarly-named environment that does the same thing, perhaps for the sake of convenience:

\documentclass{article}
\newcommand{\dosomething}[1]{\textbf{#1}}% Make argument bold
\newenvironment{dosomething}{\bfseries}{}% Make contents bold
\begin{document}
\dosomething{test}
\end{document}


LaTeX complains that Command \dosomething already defined at l.3 \newenvironment{dosomething}{\bfseries}{} (line 3) even though you defined \dosomething only once (in line 2).

The problem here is that when one uses \newcommand{myenv}{<beg env>}{<end env>}, LaTeX defines two commands \myenv and \endmyenv, respectively using the <beg env> and <end env> definition. To correct for this, use unique names for commands and environments, even if they perform very similar tasks. For example, in the above case, define the command \boldcmd and environment boldenv, say.

8. You start playing around with graphics and would like to include a picture using the graphicx package:

\documentclass{article}
\begin{document}
\usepackage{graphicx}% http://ctan.org/pkg/graphicx
\includegraphics{tiger}% Rooooaaaarrrrr!!!!!
\end{document}


but LaTeX complains that "Can be used only in preamble." This is not because of the use of \includegraphics (which is correct), but the required/included package (graphicx). The document structure requires certain "things" to be used only in certain places. In this case, you cannot load a package inside the document environment.

Place \usepackage{graphicx} between \documentclass{article} and \begin{document} - the document preamble.

9. You're familiar with passing arguments to macros in the form

\textbf{Here is some \textit{text}.}


Here \textit is passed as an argument to \textbf. However, you attempt to do the same with so-called verbatim content:

\documentclass{article}
\begin{document}
\textbf{Here is some \verb|verbatim text|.}
\end{document}​


and receive the error "\verb illegal in command argument."

\verb and friends are delicate and should be handled with care. It deals with the advanced topic of category codes and is perhaps best described in the UK TeX FAQ entry Why doesn’t verbatim work within …?, together with appropriate solutions/alternatives. Most notably asking yourself the question whether using verbatim is actually necessary.

10. Someone advised you to use amsmath's align environment after reading \eqnarray vs \align and requests a duplication of the Taylor expansion of \sqrt{1-x}:

Ambitious in your attempt, you try:

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\begin{document}
\begin{align*}
\sqrt{1+x} = 1 + x\left(\tfrac{1}{2} - \tfrac{1}{8}x + \tfrac{1}{16}x^2
& - \tfrac{5}{128}x^3 \cdots \\
& + \tfrac{(-1)^i(2i)!}{(1-2i)(i!)^2(4i)}x^{i-1}\cdots\right)
\end{align*}
\end{document}


(La)TeX spits out an error Extra }, or forgotten \right.

\left and \right are delimiter counterparts and should always appear together and in the same (nested) group in math mode. In the above example, \left and \right are in different groups, not only spanning the align columns, but also rows. In instances like these, its much more convenient to use the separable \Bigl and \Bigr delimiter extenders. Alternatives also include using \vphantom to scale the separate groups vertically to the maximum size while using the null delimiters \right. and \left. where needed. See \left/\right across multiline equation.

11. Working with tables, you wish to insert some form of row labelling in the first column using a [..] style notation:

\documentclass{article}
\begin{document}
\begin{tabular}{ll}
Header row & Some content \\
\hline
[a] & Stuff A \\
[b] & Stuff B
\end{tabular}
\end{document}​


LaTeX complains about "Missing number, treated as zero" and "Illegal unit of measure (pt inserted)". These errors are related to the optional argument that can be specified with the control sequence \\; [b] is considered to be the optional argument to \\, effectively trying to evaluate \\[b], where b is expected to be a length/dimension. This is not the case, so it's substituted for zero (as the first error message reports), yet there is no dimension associated with it (as reported by the second error message). One reference to such an instance is given in Allow [ characters in tables without entering math-mode (from R).

The work-around here is to insert a non-[ element at the appropriate location to avoid LaTeX from grabbing them as optional arguments to \\. For example {}[b] would do.

12. Experimenting with headers/footers, you attempt to stack some content in order to provide more information to the reader:

\documentclass{article}
\usepackage{lipsum,fancyhdr}% http://ctan.org/pkg/{lipsum,fancyhdr}
\fancyfoot[C]{\thepage

My Name}
\pagestyle{fancy}
\begin{document}
\lipsum[1-5]
\end{document}


LaTeX complains about a

Runaway argument?
{\thepage
! Paragraph ended before \f@ncyhf was complete.


yet everything seems fine.


In order to get around this, don't use multiple paragraphs inside macros that can't handle them. This particular solution could be avoided using \thepage \\ My Name which technically sets a single paragraph. One could also use a tabular to provide the stacked look.

-
probably too late to be useful, but comments on two points: in 1, \mbox rigidly provides output in the current text size, while \text (requires amsmath) adjusts the size to the current environment. in 5, another compelling argument for not redefining \i is the fact that hyperref will either "throw up" or override your definition since it redefines just about all one-letter commands as well as most "structural" commands like \section. anyhow, very nice list. –  barbara beeton Jan 3 '12 at 17:44
@barbarabeeton: Thanks, I've added your suggestion regarding (1). Even though you're correct about (5), I think as a beginner making "typical LaTeX errors," using hyperref might be a bit out of scope. –  Werner Jan 3 '12 at 17:55
Re (4), be sure to teach your student not to do things like double line breaks unless it's really nessecary (i.e. use parsnip if that's what's intended, and so on). –  You Jan 3 '12 at 19:51

Why is the output of the two lines not identical?

\documentclass{article}
\newcommand{\foo}{bar}

\begin{document}
bar bar

\foo \foo
\end{document}


You could also add a third line \foo\foo, but that might be too much of a clue.

Where is the output of the macro?

\documentclass{article}
\newcommand{\foo}[1]{\textbf{#1}}

\begin{document}
\foo \foo

bar bar
\end{document}


This results in an error:

Paragraph ended before \text@command was complete

\documentclass{article}
\newcommand{\foo}[1]{\textbf{#1}}

\begin{document}
\foo

bar bar
\end{document}


TeX ignores extra spaces and newlines, so why does the output here change if the comment character % is removed?

\documentclass{article}
\newcommand{\foo}[1]{
\textbf{#1}
}

\begin{document}
\foo{bar}%
bar bar
\end{document}

-
Please highlight how to solve the issue too :) –  damned Nov 14 '12 at 9:54
@damned: If you have a question about a specific one, please let me know, or just post a new question. If something is not clear to you, I am sure there are others with the same question. –  Peter Grill Nov 14 '12 at 10:03

two different kind of errors with the same error message

\documentclass{article}
\begin{document}

foo \textt{bar} baz
\rotatebox{45}{45}

\end{document}

-

Leslie Lamport's LaTeX: A Document Preparation System provides a list of common errors. (Same for LaTeX and Friends---it will be available by the end of January.) Another list may be found at the UK TeX FAQ (look for The Joy of TeX Errors).

Bibliography

@Book{Lamport:94,
author     = {Lamport, Leslie},
title      = {\LaTeX: A Document Preparation System},
year       = {1994},
isbn       = {0-021-52983-1},
}

@Book{vanDongen:2012,
author    = {van~Dongen, M.\,R.\,C.},
title     = {{\LaTeX} and Friends},
publisher = {Springer},
year      = {2012},
isbn      = {978-3-642-23815-4},
}

-

I got interesting errors when inserting text from Microsoft Word into LaTeX. Especially characters sch as "-" are encoded differently in Word than in TeXniccenter. Finding those errors is always tidious.

-
If you have a reasonable editor, these errors aren't too difficult. For example, when saving a file with UTF-8 characters as ASCII, Emacs will pause to ask you whether to save as UTF-8 instead, giving you time to click on the provided links (or write a keyboard macro) to replace these characters with their TeX-friendly equivalents. (I'm sure I've seen this functionality out of TeXmaker, so it likely exists in TeXnicCenter as well.) (Also, I'm not really sure these are actual LaTeX errors --- it's more of an encoding nightmare.) –  Sean Allred Jun 5 '13 at 12:18