list of occurences of references
There are some adjustments to be made. Here's something that should work
\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 \arabic{chapter}.\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
\begin{document}
\tableofcontents{}
\chapter{First}
\tracingmacros=1
\begin{goal}{firstfirst}
learn more \LaTeX
\end{goal}
\tracingmacros=0
\begin{goal}{firstsecond}
spend time on content, not \LaTeX
\end{goal}
stuff\index{stuff}
\chapter{Second}
\begin{goal}{secondfirst}
have fun in any case\index{fun}
\end{goal}
Chapter contents here, referring to some goals:
\section{whatever}
Reference to a chapter 1 goal: \gref{firstsecond}
Reference to a chapter 2 goal: \gref{secondfirst}
\section{another}
Second reference to a chapter 2 goal: \gref{secondfirst}
\printindex[goals]
\printindex
\end{document}
I've used refcount
and \getrefnumber
, otherwise \index
would produce different entries.
This is so close to a complete solution, built following Holle's index suggestion and using egreg's imakidx
package, that I decided to post it as an answer rather than as an edit to the question. Still missing:
- the ability to expand the
\BODY
macro from theenviron
package inside the call to\index
, - guarantee that the expanded
\BODY
always appears first under its goal in the Goal References index, Edit: starting the index with a blank space before\BODY
fixes this. Incorporate in in egreg's accepted answer. - single column output for that index. Edit: deleting
idxlayout
package fixes this.
Here's the output from the MWE below, which isn't quite "minimal" since I need to make sure that other features still work.
\documentclass[12pt]{book}
\usepackage{amsthm}
\usepackage{environ}
\usepackage{imakeidx}
\usepackage[totoc]{idxlayout}
\usepackage{hyperref}
\makeindex
%% columns=1 isn't working
\makeindex[name=goals,title=Goal References,columns=1]
\newcommand{\gref}[1]{%
[Goal~\ref{goal:#1}]
\index[goals]{Goal~\ref{goal:#1}!Section \arabic{chapter}.\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]
\NewEnviron{goal}[1]{%
\begin{Goal}
\label{goal:#1}
\index[goals]{Goal~\ref{goal:#1}!BODY here}
%{\em label:} #1 %comment in/out to restore/suppress printing label
\normalfont{}\noindent
\BODY
\egregaddtocontents{\BODY}
}
[\end{Goal}]
\makeatother
\begin{document}
\tableofcontents{}
\chapter{First}
\begin{goal}{firstfirst}
learn more \LaTeX
\end{goal}
\begin{goal}{firstsecond}
spend time on content, not \LaTeX
\end{goal}
stuff\index{stuff}
\chapter{Second}
\begin{goal}{secondfirst}
have fun in any case\index{fun}
\end{goal}
Chapter contents here, referring to some goals:
\section{whatever}
Reference to a chapter 1 goal: \gref{firstsecond}
Reference to a chapter 2 goal: \gref{secondfirst}
\section{another}
Second reference to a chapter 2 goal: \gref{secondfirst}
\printindex[goals]
\printindex
\end{document}