LaTex报"Missing $ inserted"的解决方法

hxy    2017-12-27 00:28

Latex中加入了一个多图,编译出现这样的错误。原因是在图的caption里使用了下标线“_”。因为latex会把它作为有特殊意义的标号或作为数学模式(math mode)进行编译。

转义需使用 \

How to typeset an underscore character

 

The underscore character ‘_’ is ordinarily used in TeX to indicate a subscript in maths mode; if you type _, on its own, in the course of ordinary text, TeX will complain. The “proper” LaTeX command for underscore is \textunderscore, but the LaTeX 2.09 command \\_ is an established alias. Even so, if you’re writing a document which will contain a large number of underscore characters, the prospect of typing \\_ for every one of them will daunt most ordinary people.

Moderately skilled macro programmers can readily generate a quick hack to permit typing ‘_’ to mean ‘text underscore’ (the answer in “defining characters as macros” uses this example to illustrate its techniques). However, the code is somewhat tricky, and more importantly there are significant points where it’s easy to get it wrong. There is therefore a package underscore which provides a general solution to this requirement.

There is a problem, though: OT1 text fonts don’t contain an underscore character, unless they’re in the typewriter version of the encoding (used by fixed-width fonts such as cmtt). In place of such a character, LaTeX (in OT1 encoding) uses a short rule for the command \textunderscore, but this poses problems for systems that interpret PDF — for example those PDF-to-voice systems used by those who find reading difficult.

So either you must ensure that your underscore characters only occur in text set in a typewriter font, or you must use a more modern encoding, such as T1, which has the same layout for every font, and thus an underscore in every font.

A stable procedure to achieve this is:

% (1) choose a font that is available as T1
% for example:
\usepackage{lmodern}

% (2) specify encoding
\usepackage[T1]{fontenc}

% (3) load symbol definitions
\usepackage{textcomp}
which will provide a command \textunderscore which robustly selects the right character. The underscore package, mentioned above, will use this command.
underscore.sty
underscore

This answer last edited: 2011-08-17


This question on the Web: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=underscore

Last Modified: 2019-07-15 15:30
Views: 3.9K

[[total]] comments

Post your comment
  1. [[item.time]]
    [[item.user.username]] [[item.floor]]Floor
  2. Click to load more...
  3. Post your comment