这是对问题的后续回答参考文献列表在下面的 MWE 中,索引按字母顺序列出了目标,其中第二个标记需要数字:
我找到的解决方案(在 SE 上 - 抱歉,我弄丢了链接)似乎对子索引的章节和节号不起作用,不适用于主标题。我尝试了其他几个方法,但无济于事。
编辑:解决方案将引用结果转换为常规数字看起来好像应该可以工作但我尝试了类似的方法但失败了。
\documentclass[12pt]{book}
\usepackage{amsthm}
\usepackage{environ}
\usepackage{imakeidx}
\usepackage{refcount}
\usepackage{hyperref}
\makeindex[intoc]
\makeindex[name=goals,title=Goal References,columns=1,intoc]
\newcommand{\gref}[1]{%
[Goal~\ref{goal:#1}]
\index[goals]{Goal~\getrefnumber{goal:#1}!Section
\ifnum\value{chapter}<10 0\fi\arabic{chapter}.%
\ifnum\value{section}<10 0\fi\arabic{section}}
}
\makeatletter
\newcommand\l@goal[1]{%
\addpenalty{\@highpenalty}%
\vskip \z@ \@plus \p@
\begingroup
\parindent\z@
\rightskip\@pnumwidth
\parfillskip-\@pnumwidth
\leavevmode #1\nobreak\hfil\nobreak\null\par
\penalty\@highpenalty
\endgroup}
\newcommand{\egregaddtocontents}[1]{%
\addtocontents{toc}{\protect\l@goal{#1}}}
% arrange goal numbering by chapter
\newtheorem{Goal}{Goal}[chapter]
% define a shorthand to be able to get at \BODY with \expandafter
\newcommand\indexgoal[1]{\goalindex[goals]{Goal~\theGoal! #1}}
% save a copy of \index
\let\goalindex\index
\NewEnviron{goal}[1]{%
\begin{Goal}
\label{goal:#1}%
% here we neutralize \index so that it won't do damages
\begingroup
\renewcommand\index[2][]{}%
% but the main command uses \goalindex, so it's safe
\expandafter\indexgoal\expandafter{\BODY}%
\egregaddtocontents{\BODY}
\endgroup
%{\em label:} #1 %comment in/out to restore/suppress printing label
\normalfont{}\noindent
\BODY
}
[\end{Goal}]
\makeatother
\newenvironment{goals}
{%
{\textbf Chapter goals:}
\egregaddtocontents{Chapter goals:}
}
{}
\begin{document}
\tableofcontents{}
\setcounter{chapter}{1}
\chapter{TWO}
\tracingmacros=1
\begin{goals}
\begin{goal}{twofirst}
learn more \LaTeX
\end{goal}
\end{goals}
\tracingmacros=0
\begin{goal}{twosecond}
spend time on content, not \LaTeX
\end{goal}
\section{two-one}
stuff\index{stuff}
Reference to a chapter 11 goal: \gref{elevenfirst}
\setcounter{chapter}{10}
\chapter{ELEVEN}
\begin{goal}{elevenfirst}
have fun in any case\index{fun}
\end{goal}
Chapter contents here, referring to some goals:
\setcounter{section}{1}
\section{whatever}
Reference to a chapter 2 goal: \gref{twosecond}
Reference to a chapter 11 goal: \gref{elevenfirst}
\setcounter{section}{10}
\section{another}
Second reference to a chapter 2 goal: \gref{twofirst}
\printindex[goals]
\printindex
\end{document}
答案1
您必须以正确的字母顺序格式化条目。因此我使用了一个技巧:
\documentclass[12pt]{book}
\usepackage{amsthm}
\usepackage{environ}
\usepackage{imakeidx}
\usepackage{refcount}
\usepackage{hyperref}
\makeindex[intoc]
\makeindex[name=goals,title=Goal References,columns=1,intoc]
\makeatletter
\newcommand{\gref}[1]{%
[Goal~\ref{goal:#1}]
\edef\@goal@number{\getrefnumber{goal:#1}}%
\edef\@the@section{\thesection}%
\index[goals]{Goal~\expandafter\sortgref\@[email protected]\sortgref!%
\expandafter\sortgref\@[email protected]\sortgref @%
Section~\thesection}%
}
\def\sortgref#1.#2#3\sortgref{%
\ignoresort{\ifnum#1<10 0\fi#1.\ifnum#2<10 0\fi#2}#1.#2%
}
\protected\def\ignoresort#1{}
\newcommand\l@goal[1]{%
\addpenalty{\@highpenalty}%
\vskip \z@ \@plus \p@
\begingroup
\parindent\z@
\rightskip\@pnumwidth
\parfillskip-\@pnumwidth
\leavevmode #1\nobreak\hfil\nobreak\null\par
\penalty\@highpenalty
\endgroup}
\newcommand{\egregaddtocontents}[1]{%
\addtocontents{toc}{\protect\l@goal{#1}}}
% arrange goal numbering by chapter
\newtheorem{Goal}{Goal}[chapter]
% define a shorthand to be able to get at \BODY with \expandafter
\newcommand\indexgoal[1]{\edef\@goal@number{\theGoal}%
\goalindex[goals]{Goal~\expandafter\sortgref\@[email protected]\sortgref! #1}}
% save a copy of \index
\let\goalindex\index
\NewEnviron{goal}[1]{%
\begin{Goal}
\label{goal:#1}%
% here we neutralize \index so that it won't do damages
\begingroup
\renewcommand\index[2][]{}%
% but the main command uses \goalindex, so it's safe
\expandafter\indexgoal\expandafter{\BODY}%
\egregaddtocontents{\BODY}
\endgroup
%{\em label:} #1 %comment in/out to restore/suppress printing label
\normalfont{}\noindent
\BODY
}
[\end{Goal}]
\makeatother
\newenvironment{goals}
{%
{\textbf Chapter goals:}
\egregaddtocontents{Chapter goals:}
}
{}
\begin{document}
\tableofcontents{}
\setcounter{chapter}{1}
\chapter{TWO}
\tracingmacros=1
\begin{goals}
\begin{goal}{twofirst}
learn more \LaTeX
\end{goal}
\end{goals}
\tracingmacros=0
\begin{goal}{twosecond}
spend time on content, not \LaTeX
\end{goal}
\section{two-one}
stuff\index{stuff}
Reference to a chapter 11 goal: \gref{elevenfirst}
\setcounter{chapter}{10}
\chapter{ELEVEN}
\begin{goal}{elevenfirst}
have fun in any case\index{fun}
\end{goal}
Chapter contents here, referring to some goals:
\setcounter{section}{1}
\section{whatever}
Reference to a chapter 2 goal: \gref{twosecond}
Reference to a chapter 11 goal: \gref{elevenfirst}
\setcounter{section}{10}
\section{another}
Second reference to a chapter 2 goal: \gref{twofirst}
\printindex[goals]
\printindex
\end{document}
你goals.idx
会发现
\indexentry{Goal\nobreakspace {}\ignoresort {02.01}2.1! learn more \LaTeX |hyperpage}{3}
\indexentry{Goal\nobreakspace {}\ignoresort {02.02}2.2! spend time on content, not \LaTeX |hyperpage}{3}
\indexentry{Goal\nobreakspace {}\ignoresort {11.01}11.1!\ignoresort {02.01}2.1@Section\nobreakspace {}2.1|hyperpage}{3}
\indexentry{Goal\nobreakspace {}\ignoresort {11.01}11.1! have fun in any case\index {fun}|hyperpage}{5}
\indexentry{Goal\nobreakspace {}\ignoresort {02.02}2.2!\ignoresort {11.02}11.2@Section\nobreakspace {}11.2|hyperpage}{5}
\indexentry{Goal\nobreakspace {}\ignoresort {11.01}11.1!\ignoresort {11.02}11.2@Section\nobreakspace {}11.2|hyperpage}{5}
\indexentry{Goal\nobreakspace {}\ignoresort {02.01}2.1!\ignoresort {11.01}11.1@Section\nobreakspace {}11.11|hyperpage}{5}
以便正确进行排序:这里是goals.ind
\begin{theindex}
\item Goal\nobreakspace {}\ignoresort {02.01}2.1
\subitem Section\nobreakspace {}11.11, \hyperpage{5}
\subitem learn more \LaTeX , \hyperpage{3}
\item Goal\nobreakspace {}\ignoresort {02.02}2.2
\subitem Section\nobreakspace {}11.2, \hyperpage{5}
\subitem spend time on content, not \LaTeX , \hyperpage{3}
\item Goal\nobreakspace {}\ignoresort {11.01}11.1
\subitem Section\nobreakspace {}2.1, \hyperpage{3}
\subitem Section\nobreakspace {}11.2, \hyperpage{5}
\subitem have fun in any case\index {fun}, \hyperpage{5}
\end{theindex}
该\ignoresort
宏对其参数不执行任何操作,因此打印的条目将不填充零。