\index 命令中的换行符会导致索引中的相同条目显示为不同的条目

\index 命令中的换行符会导致索引中的相同条目显示为不同的条目

如果有两个\index{}命令包含相同的字符串,但其中一个命令带有换行符,则该项目在索引中显示为两个不同的项目。请参阅下面的 MWE(在第一个\index命令中使用换行符复制),其中条目位于foofoofoo barbarbar(在 中一次占一行.tex,一次跨行)。有没有办法避免这种情况?可以使用 转义所有空格~,但索引中条目的换行符不再理想。此外,条目 foofoofoo~barbarbarfoofoofoo barbarbar也被视为不同,因此 忘记在一个条目中插入波浪号也会导致不同的条目。有趣的是,我找不到太多/任何有关索引命令中的换行符的信息...

\documentclass{scrartcl}

\usepackage{imakeidx}
\makeindex

\begin{document}
With editors that break lines automatically (to adhere to the, say 80, column
rule; for example Emacs), blanks in index commands are often automatically
broken over lines.

As an example to demonstrate this behavior, this leads to \index{foofoofoo
  barbarbar} (broken over lines in `.tex`) to appear as a different\clearpage
entry than the entry \index{foofoofoo barbarbar}. How can this be prevented? One
way would be to always avoid blanks in `index` commands via `~` but then the
items in the index are not broken over lines in an ideal way.

\printindex
\end{document}

答案1

的论点\index是以几乎逐字逐句的方式收集的,因此在.idx文件中你得到

\indexentry{foofoofoo   barbarbar}{1}
\indexentry{foofoofoo barbarbar}{2}

MakeIndex 程序默认尊重多个空格,但您可以使用-c命令行选项告诉它不要这样做。

OPTIONS
 -c  Compress intermediate blanks (ignoring leading and trailing blanks and
     tabs).  By default, blanks in the index key are retained.

您可以将其与可选参数一起传递给\makeindex

\documentclass{scrartcl}

\usepackage{imakeidx}
\makeindex[options=-c]

\begin{document}
With editors that break lines automatically (to adhere to the, say 80, column
rule; for example Emacs), blanks in index commands are often automatically
broken over lines.

As an example to demonstrate this behavior, this leads to \index{foofoofoo
  barbarbar} (broken over lines in `.tex`) to appear as a different\clearpage
entry than the entry \index{foofoofoo barbarbar}. How can this be prevented? One
way would be to always avoid blanks in `index` commands via `~` but then the
items in the index are not broken over lines in an ideal way.

\printindex
\end{document}

在此处输入图片描述

相关内容