\footnote 打破了 \index 中的 @-constructs

\footnote 打破了 \index 中的 @-constructs

為了索引一個重要的 TeX 表達式expr,我們使用命令\index{str@expr},其中是用於排序的str簡化(通常是純 ASCII)表示。expr

但是,str@expr在註腳中解析不正確:

\documentclass{report}
\usepackage{imakeidx}
\makeindex

\def\gnu{\texttt{Gnu}}
\def\gnat{\texttt{Gnat}}
\begin{document}
Some text\index{Gnu@\gnu}\index{Gnat@\gnat}.

\pagebreak
Some more text\index{Gnu@\gnu}.\footnote
{A footnote.\index{Gnat@\gnat}}

\printindex
\end{document}

這會產生以下.idx文件

\indexentry{Gnu@\gnu}{1}
\indexentry{Gnat@\gnat}{1}
\indexentry{Gnu@\gnu}{2}
\indexentry{Gnat@\texttt  {Gnat}}{2}

「Gnat」的兩個索引條目不同,因此\Gnat在索引中出現兩次,如以下螢幕截圖所示:

在此輸入影像描述

如何\footnote修改\index命令,可以做什麼?

答案1

這與其說是構造問題@,不如說是一個控制序列擴展的問題。基於的解決方法https://groups.google.com/forum/#!topic/comp.text.tex/_Hwo6Hapcng建議提供一個命令,我們可以使用它來保護腳註中索引條目中的反斜杠\def\indexprotect#1{\string#1}。正如 Ulrike Fischer 指出的\string那樣,它本身就已經足夠好:

樣本輸出

\documentclass{report}
\usepackage{imakeidx}
\makeindex

\def\gnu{\texttt{Gnu}}
\def\gnat{\texttt{Gnat}}
\begin{document}
Some text\index{Gnu@\gnu}\index{Gnat@\gnat}.

\pagebreak
Some more text\index{Gnu@\gnu}.\footnote
{A footnote.\index{Gnat@\string\gnat}}

\printindex
\end{document}

答案2

@Andrew Swann 和 @Ulrike Fischer 的解決方案(用於\string轉義 中的反斜杠expr)效果很好,但需要在所有相關腳註或標題中重複努力。因此@egreg在評論中提到的解決方案,

\usepackage{bigfoot}

一勞永逸地解決問題,對我來說似乎比較可取。

相關內容