5

While | is the encap character for makeindex, then if I want to use | in \index, e.g. \index{$|F|$}, how to escape it?

When I escape it with double quote, i.e. \index{$"|F"|$}, and use hyperref, the *.idx file will looks like

\indexentry{$"|hyperindexformat{\F"}}{1}

which results an error.

1 Answers1

4

In theory, a character that is special for MakeIndex, for instance !, |, @ should be quoted with ". However, this confuses hyperref when | is used.

The workaround is simple:

\index{$\vert F\vert$}

or, better, loading amsmath and using an alphabetizing string:

\index{Fabs@$\lvert F\rvert$}

Using \lvert and \rvert is recommended anyway.

You can also load mathtools and do

\DeclarePairedDelimiter{\abs}{|}{|}

and use $\abs{F}$ (look at the package documentation for more information).

The \abs strategy is better for another reason: if \index appears in the argument to another command, such as

\newcommand\keyword[1]{\underline{\bfseries #1}\index{#1}}

and then called as

\keyword{$\vert F\vert$}

the \vert command is expanded (see Symbol index sorted by occurrence for a similar case). If you use \abs, this won't happen.

Note: \bf is a deprecated command; I changed the definition you showed in the comment to be in line with the recommended practice.

egreg
  • 1,121,712
  • If I define a new command wrapping \index, why \index will be affected: \newcommand\keyword[1]{\bf\underline{#1}\index{#1}}, e.g. \keyword{$\vert F\vert$} not work – Martin Wang May 13 '14 at 14:41
  • @MartinWang That's another known problem. http://tex.stackexchange.com/questions/41140/symbol-index-sorted-by-occurrence You don't get it with the \abs route. – egreg May 13 '14 at 14:44