按页面出现次数对符号索引进行排序

按页面出现次数对符号索引进行排序

SE,我在对符号索引中的项目进行排序时遇到了问题。MWE:

\documentclass[oneside,12pt,a4paper]{memoir}

\renewcommand{\baselinestretch}{1.15}

\let\memoldbibsection\bibsection
\let\bibsection\relax
%\usepackage[nobysame]{amsrefs}
\let\bibsection\memoldbibsection 

\usepackage[right=2.7cm,left=3.5cm, top=2.7cm, bottom=2.5cm,includehead]{geometry}


\usepackage{amssymb, amsmath, amsthm}
\usepackage{imakeidx}

\makeindex[intoc, name=sym,title=Symbol index]

\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent vitae laoreet eros. Nam dapibus ante nec suscipit mattis. Mauris tempus tortor nunc, sed suscipit neque varius vel. Duis semper elit libero, a porttitor tellus vehicula id. Maecenas semper lacinia lectus, et tempor mauris pulvinar vel. Vivamus placerat ipsum enim, sit amet dignissim purus placerat nec. Suspendisse mattis ipsum convallis bibendum mattis. Nullam gravida eros vitae enim aliquam venenatis. Duis faucibus tincidunt velit, id eleifend mauris fermentum malesuada. Nunc blandit metus turpis, nec auctor elit tristique non. Proin ut mattis elit. Nam ac nulla sodales, ullamcorper lacus porta, eleifend urna. Maecenas eget dui vitae turpis pulvinar auctor. Maecenas a porttitor dolor.

%Multiple times Lorem Ipsum so that symbols end up in different pages

\index[sym]{$(E, \mathcal{E})$}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent vitae laoreet eros. Nam dapibus ante nec suscipit mattis. Mauris tempus tortor nunc, sed suscipit neque varius vel. Duis semper elit libero, a porttitor tellus vehicula id. Maecenas semper lacinia lectus, et tempor mauris pulvinar vel. Vivamus placerat ipsum enim, sit amet dignissim purus placerat nec. Suspendisse mattis ipsum convallis bibendum mattis. Nullam gravida eros vitae enim aliquam venenatis. Duis faucibus tincidunt velit, id eleifend mauris fermentum malesuada. Nunc blandit metus turpis, nec auctor elit tristique non. Proin ut mattis elit. Nam ac nulla sodales, ullamcorper lacus porta, eleifend urna. Maecenas eget dui vitae turpis pulvinar auctor. Maecenas a porttitor dolor.

\index[sym]{$abc$}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent vitae laoreet eros. Nam dapibus ante nec suscipit mattis. Mauris tempus tortor nunc, sed suscipit neque varius vel. Duis semper elit libero, a porttitor tellus vehicula id. Maecenas semper lacinia lectus, et tempor mauris pulvinar vel. Vivamus placerat ipsum enim, sit amet dignissim purus placerat nec. Suspendisse mattis ipsum convallis bibendum mattis. Nullam gravida eros vitae enim aliquam venenatis. Duis faucibus tincidunt velit, id eleifend mauris fermentum malesuada. Nunc blandit metus turpis, nec auctor elit tristique non. Proin ut mattis elit. Nam ac nulla sodales, ullamcorper lacus porta, eleifend urna. Maecenas eget dui vitae turpis pulvinar auctor. Maecenas a porttitor dolor.

%Multiple times Lorem Ipsum so that symbols end up in different pages

\index[sym]{$\mu_{X}$}

\index[sym]{$\mu_{Y}$}


Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent vitae laoreet eros. Nam dapibus ante nec suscipit mattis. Mauris tempus tortor nunc, sed suscipit neque varius vel. Duis semper elit libero, a porttitor tellus vehicula id. Maecenas semper lacinia lectus, et tempor mauris pulvinar vel. Vivamus placerat ipsum enim, sit amet dignissim purus placerat nec. Suspendisse mattis ipsum convallis bibendum mattis. Nullam gravida eros vitae enim aliquam venenatis. Duis faucibus tincidunt velit, id eleifend mauris fermentum malesuada. Nunc blandit metus turpis, nec auctor elit tristique non. Proin ut mattis elit. Nam ac nulla sodales, ullamcorper lacus porta, eleifend urna. Maecenas eget dui vitae turpis pulvinar auctor. Maecenas a porttitor dolor.

\clearpage
\chaptermark{Symbol index}
\renewcommand{\indexname}{Symbol index}
\printindex[sym]


\end{document}

现在,通过运行此示例您将看到索引中的顺序并不反映符号出现的顺序。

我如何强制索引按符号首次出现的页码排序?

答案1

您可以记录一个辅助计数器作为排序部分。这假设您有少于 1000 个符号(但如果需要,可以扩展)。

为了应对多种情况,第一个会设置一个key。

\documentclass[oneside,12pt,a4paper]{memoir}
\usepackage{imakeidx}

\makeindex[intoc, name=sym,title=Symbol index]

\newcounter{indexcount}
\newcommand{\threedigit}[1]{\ifnum #1<100 0\fi\ifnum #1<10 0\fi#1}

\newcommand{\symindex}[1]{%
  \ifcsname\detokenize{SYM@@#1}\endcsname
    \index[sym]{\csname\detokenize{SYM@@#1}\endcsname @#1}%
  \else
    \stepcounter{indexcount}%
    \expandafter\xdef\csname SYM@@\detokenize{#1}\endcsname{%
      \expandafter\threedigit\expandafter{\romannumeral-`Q\theindexcount}%
    }%
    \index[sym]{\threedigit{\theindexcount}@\unexpanded{\unexpanded{#1}}}%
  \fi
}

\begin{document}

Some text

% The following two symbols are at page 1
\symindex{$(E, \mathcal{E})$}
\symindex{$abc$}
\symindex{$abc$}

\clearpage

Some text

% this is at page 2
\symindex{$\mu_{X}$}
\symindex{$(E, \mathcal{E})$}

\clearpage

Some text

% this is at page 3
\symindex{$\mu_{Y}$}

\printindex[sym]


\end{document}

enter image description here

相关内容