# How to escape | character in \index?

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. • \index{$"|F"|$} should do. – egreg May 13 '14 at 13:18 • @egreg but when I use hyperref, the *.idx file looks like: \indexentry{$"|hyperindexformat{\F"}}{1}, which results in an error. – Martin Wang May 13 '14 at 13:33

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$}

\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.

• 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. tex.stackexchange.com/questions/41140/… You don't get it with the \abs route. – egreg May 13 '14 at 14:44