Understanding Tex errors 

http://makingtexwork.sourceforge.net/mtw/ch03.html

When you write TeX documents, you will occasionally make mistakes and as a result, TeX won't be able to process your document. There are six broad classes of mistakes you're likely to make:

  1. Naming documents or files that TeX cannot find.
  2. Misspelling the name of a TeX control sequence.
  3. Failing to close an environment or forgetting to insert a closing brace after an opening brace.
  4. Using math operators outside of math mode or forgetting to close an opened math environment.
  5. Requesting a font that TeX cannot find.
  6. Everything else.

TeX is legitimately criticized for having error messages that are very difficult to understand. TeX frequently provides far more information than you really need or want, and the excess information often obscures the actual cause of the error.

Nevertheless, understanding what TeX does when it encounters an error will help make error messages easier to understand. Let's begin with an example. Example Example 3.1 shows a simple LaTeX document using the New Font Selection Scheme (NFSS), which contains an error.[33] The error is that the control sequence \Large, which has been redefined to request a 17pt

Example 2. Example 3.1. The Document BADFONT.TEX

00001: \documentstyle{article}
00002: % The following definition changes the font that LaTeX
00003: % uses for the 'Large' font.  I have introduced a typo
00004: % into the definition, "\fontsiz" should be "\fontsize".
00005: % The  first time a \Large font is requested, an error will occur.
00006: \renewcommand{\Large}{\fontsiz{17}{20pt}\selectfont}
00007: \begin{document}
00008:
00009: This text precedes the first section header.
00010:
00011: % Note: LaTeX uses the \Large font in section
00012: % headers...this will fail in a confusing way
00013: % because the error is deep within the definition
00014: % of \section where \Large is used...
00015: \section{First Section}
00016:
00017: This is the first and only sentence of the first section.
00018:
00019: \end{document}

When TeX processes this file, using LaTeX with the New Font Selection Scheme, it produces these error messages:

This is TeX, Version 3.1415 (C version 6.1)
LaTeX Version 2.09 <25 March 1992> with NFSS2
(badfont.tex (/work/nutshell/texguide/styles/latex/article.sty
Standard Document Style `article' <14 Jan 92>.
(/work/nutshell/texguide/styles/latex/art10.sty)) (badfont.aux)
(/usr/local/lib/tex/inputs/nfss2/T1cmr.fd)
! Undefined control sequence.
\Large ->\fontsiz
                  {17}{20pt}\selectfont
l.15 \section{First Section}

?

These messages exemplify the kind of confusing error messages that TeX produces. Remember the following rule: Always look at the first and last line of the TeX error message when trying to figure out what went wrong and where it went wrong. In this case, the first line is:

! Undefined control sequence.

and the last line is:

l.15 \section{First Section}

The error is that the control sequence \fontsiz is not defined and TeX was processing line 15 of the file when it occurred. The control sequence that is actually undefined is right after the point symbol '->', till the end of line. I.e., '->' points to the actual undefined control sequence.

Interpreting TeX Error Messages 

[Note]

The text about the interpreting the TeX error messages is not strictly copied from the original article, but have been corrected by truly yours for consistency, and amended for clearness.

The first line of a TeX error message begins with an exclamation point followed by the text of the message. The lines that follow it show the context in which the error occurred. In the previous example, the error message indicates an Undefined control sequence. This means that TeX encountered a control sequence which was not previously defined.

The final line, which says

l.15 \section{First Section}

occurs right above the question mark prompt. It identifies the line in your document that TeX was processing when it encountered the error. The error occurred when TeX was at line 15 of the file, and that line began with

Between the first and last lines, TeX prints a detailed description of how the error occurred. This is necessary because the error may have occurred inside the replacement text of a macro that you used. When TeX encounters a control sequence, like \section, it has to look up the definition to figure out how to typeset your document. The definition of a control sequence may contain other control sequences which also have to be interpreted. It is possible for TeX to be several levels deep, as it was in this case, when an error occurs. To give the person who wrote the definition of the control sequence an opportunity to figure out what went wrong, TeX prints out a trace back of what happened before the error. As you gain experience with a particular macro package, you'll find the intervening lines more meaningful.

Correcting an error depends entirely on the nature of the error. In this case, all you need to do is correct the typo.

As stated above, there are six general classes of errors you might encounter when you run TeX. The following sections briefly describe each class.

Log Files 

You don't have to remember or write down the error messages that TeX produces. When TeX processes a document, it produces a transcript of everything that occurs; you can refer to this transcript later if you need to recall what errors occurred when you processed your document. TeX stores this transcript in a file which has the same name as the document and the extension .log. For example, if you process main.tex, TeX produces a transcript in main.log.

[Note]

Log files go in the current directory. TeX always places the log file in the current directory, even if you specify a path when you format your document. For example, if you process lectures/main.tex, TeX produces a transcript in main.log, not in lectures/main.log as you might expect.

Naming a File TeX Cannot Find 

Missing documents are discussed in the section called "the section called "User Files"," earlier in this chapter.

If you request a format file that cannot be found, TeX issues the following warning message:

Sorry, I can't find that format; will try the default

TeX then attempts to typeset your document with Plain TeX.[36] To correct this problem, run TeX with the correct format file name. If the format file isn't available, you will have to build it. Chapter Chapter 4, Chapter 4, describes how to build format files for several common macro packages.

Misspelling a Control Sequence Name 

Misspelling a control sequence name is one of the most common errors. For identify. In many cases, you can simply proceed after encountering this error. Although TeX may not typeset your document correctly, you can continue to look for other errors. The section called "the section called "Interpreting TeX Error Messages"," later in this chapter, describes how to continue after an error.

In some cases, TeX may become badly confused by a misspelled control sequence name, in which case you should give up and fix the spelling error before trying to process your document further. This may happen if you of the control sequences which follow.

Sometimes TeX will complain that a control sequence is undefined when you know that the control sequence is spelled correctly. When this occurs, make sure that you are using the correct format file (see the section "the section called "The Command Line"" earlier in this chapter), loading the correct macro files, and using the correct style options.

Failure to Close an Environment 

Failure to close an environment is another very common error. There are several distinct errors in this category:

  • Failure to insert a closing brace (}) for each opening brace may cause a "TeX capacity exceeded" error when TeX processes your document. This happens because sometimes TeX tries to read everything between braces into memory. If the closing brace is absent, TeX may run out of memory.

    If the braces are supposed to enclose the argument to a macro, you may also get this error:

    ! Paragraph ended before macro was complete.

    In order to help detect errors of this type, TeX doesn't ordinarily allow the argument of a macro to consist of more than one paragraph, so the first blank line after the place where you failed to type the closing brace may produce this error.

  • Failure to close a begin/end environment pair causes LaTeX to complain command.
  • If your document ends with an open environment, TeX will warn you that:

    ! (\end occurs inside a group at level n)

    where n is the number of open groups, usually 1.

  • Failure to close a mathematics environment will result in the error:

    ! Missing $ inserted

    when TeX reaches a macro that does not make sense in mathematics mode (like \section) or when a surrounding group ends.

Math Outside of Math Mode 

TeX has a lot of operators for special treatment of mathematical formulas. These operators must occur inside mathematics mode, which is usually delimited by dollar signs in your document.

If you attempt to use math operators, superscripts, subscripts, or other math-mode control sequences outside of mathematics mode, the following error will occur:

! Missing $ inserted

This is your clue that a mathematics environment has not been closed properly or that you failed to open one before using a math-mode operator.

For example, the underscore character is usually defined to be a math-mode operator which starts a subscript, in other words H${}_2$O produces "H${}_2$O" in your document.[38] If you use the underscore outside of mathematics mode, such as in regular text:

The file "test_one" contains the …

TeX will respond:

! Missing $ inserted
<inserted text>
                $
<to be read again>
                  _
The file "test_
                one" contains the ...
?

In LaTeX, the easiest way around this problem is to enclose the offending text in a "verbatim" macro, like this:

The file "test_one" contains the ...

Note that the argument to the macrois delimited by any two identical characters (in this case, two "+" signs).

In Plain TeX or another format, the problem can be circumvented in similar ways; consult the reference for the format you are using. Also consult Table Table 1.2 in Chapter Chapter 1, Chapter 1, for a list of special characters and how to type them in your documents.

Missing Fonts 

The first time that you use each font, TeX loads font metric information about the font. The font metric information, stored in a TFM file, includes information about the sizes of each character as well as kerning and ligature information. These topics are discussed fully in Chapter Chapter 5, Chapter 5.

If you request a font that does not exist, for example crm10 (a misspelling of cmr10), TeX cannot find a TFM file for the font and therefore displays:

Font \myfont=crm10 not loadable: Metric (TFM) file not found

This means that TeX attempted to associate the font described by the TFM file crm10.tfm with the control sequence \myfont, but the TFM file didn't exist. You must have a TFM file for every font that you use.

A second kind of error---actually, a warning---occurs when you are using the New Font Selection Scheme (NFSS). The NFSS performs font substitution, if possible, when an unknown font is requested. The NFSS is described in Chapter Chapter 4, Chapter 4, in the "the section called "LaTeX"" section.

Everything Else 

There are, unfortunately, lots of other errors that can occur. Some of the errors are directly related to TeX while others are warning and error messages associated with particular macro packages. There is no way to catalog every one of them or suggest what can be done in every case.

The best advice I can offer is to isolate the problem in as small a document as possible, consult the references you have available very carefully, and, if all else fails, forward your problem to one of the electronic forums that deal with TeX (the Info-TeX mailing list and the comp.text.tex newsgroup, for example).

The Question Mark Prompt 

When TeX encounters an error, it displays an error message and a summary of the error, as described in the section called "the section called "What About Errors?"" earlier in this chapter. Following the error, TeX normally stops and displays the question mark prompt.

If you type a question mark of your own at the prompt, TeX displays the actions available to you:

? ? Type <return> to proceed, S to scroll future error messages, R to run without stopping, Q to run quietly, I to insert something, E to edit your file, 1 or … or 9 to ignore the next 1 to 9 tokens of input, H for help, X to quit. ?

You can type any of the following responses at the question mark prompt:

Type Return to proceed
Simply typing Return will cause TeX to ignore the error and proceed. Depending on the nature of the error, this may cause more errors immediately or later on.
Type S to scroll future error messages

Typing S tells TeX to continue and not to stop for most future errors. TeX will continue to print the error messages, both to the terminal and to the log file, but it will not display the question mark prompt again.

TeX will still stop and ask about missing files.

Type R to run without stopping
The R option is just like S except that it tells TeX to ignore missing files as well. TeX will proceed blindly forward as best as it can. You will still see all of the error messages scroll by as TeX proceeds.
Type Q to run quietly
This option is just like R except that error messages are not displayed on the screen. The messages are saved in the log file, however, even though they are not displayed.
Type I to insert something

If you notice a simple typo, you can correct it with the I command. For example, suppose that TeX complains of an undefined control sequence: can insert the correct control sequence by responding i\cite to the question mark prompt. TeX will ignore the misspelled control sequence and insert the (correct) sequence \cite in its place.

Inserting words or commands at the question mark prompt does not change your input file. It simply instructs TeX to pretend that your file contained a different sequence of words and commands. You must change the input file with an editor, or the same error will occur the next time you format the document.

Type E to edit your file
This option terminates TeX. If your system is configured appropriately, an editor will be loaded automatically, and the cursor will be placed at the offending line in your input file. This is not possible in all environments.
Type n, where n is a number between 1 and 9

This option tells TeX to ignore some of the input. After skipping over the number of tokens you request, TeX returns to the question mark prompt so that you can delete more tokens.

What's a token? When TeX reads an input file, it breaks each line down into the smallest, indivisible chunks that have meaning. These are called tokens. For the most part, tokens are individual characters. The exceptions are control sequences, which are single tokens, and white spaces which are also single tokens. There are other exceptions (and more technical definitions of "token"), but that's the gist of it.

Type H for help
Typing H displays a slightly more verbose description of the error that occurred and, usually, suggests the nature of the corrective action that you might take.
Type X to quit
Typing X tells TeX to stop immediately and ends the TeX program. If there are any completed pages (pages processed before the error, in other words), they are written to the DVI file before TeX ends.