我需要一个列表,例如用于list of figures
将\ref
文本中的条目添加到自定义Theorem
环境的实例。这是一个带有手工构建示例的 MWE。解决方案应该为我使用以下答案构建的此代码添加功能将环境内容写入文档和目录。
\documentclass[12pt]{book}
\usepackage{amsthm}
\usepackage{environ}
\usepackage{hyperref}
\newcommand{\gref}[1]{%
[Goal~\ref{goal:#1}]
}
\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}
%{\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
\chapter{Second}
\begin{goal}{secondfirst}
have fun in any case
\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}
\chapter*{List of Goal References}
Built by hand, should be automated. Format not yet specified.
\begin{itemize}
\item \gref{firstfirst}
learn more \LaTeX
\item \gref{firstsecond}
spend time on content, not \LaTeX
\begin{itemize}
\item Section 2.1, page 5 [link to that page]
\end{itemize}
\item \gref{secondfirst}
have fun in any case
\begin{itemize}
\item Section 2.1, page 5 [link to that page]
\item Section 2.2, page 5 [link to that page]
\end{itemize}
\end{itemize}
\end{document}
这是 MWE 的列表。格式是,\itemize
但这只是暂时的 - 解决方案应该允许我轻松更改它。
也许已经有一个包可以完成这项工作。如果没有,也许可以在创建索引和数字列表的代码之上构建或拆解它们,因为我需要两者的功能。
答案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]
\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}
我已经使用了refcount
和\getrefnumber
,否则\index
会产生不同的条目。
答案2
这非常接近一个完整的解决方案,它是按照 Holle 的索引建议并使用 egreg 的imakidx
包构建的,因此我决定将其作为答案发布,而不是作为对问题的编辑。仍然缺少:
- 能够
\BODY
从environ
包内部的调用扩展宏\index
, - 保证扩展
\BODY
始终在目标引用索引中首先出现在其目标下, 编辑:在索引开头添加空格可以\BODY
解决这个问题。将其纳入 egreg 的已接受答案中。 - 该索引的单列输出。编辑:删除
idxlayout
包可以解决这个问题。
以下是 MWE 的输出,它不是很“最小”,因为我需要确保其他功能仍然有效。
\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}
答案3
我将您的代码与评论中给出的第一个示例的代码合并了。也许这就是您想要的。
\documentclass[12pt]{book}
\usepackage{amsthm}
\usepackage{environ}
\usepackage{tocloft}
\usepackage{hyperref}
% create a new list incl. counter
\newlistof{goalsCounter}{goals}{List of \textbackslash goals}
\AtEndDocument{
\cleardoublepage
\listofgoalsCounter% print the list
}
\newcommand{\gref}[1]{%
[Goal~\ref{goal:#1}]
}
\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]{%
%this is new
\refstepcounter{goalsCounter}% step the counter
\addcontentsline{goals}{goalsCounter}{\thegoalsCounter:\quad#1}% add item to list
\begin{Goal}
\label{goal:#1}
%{\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
\chapter{Second}
\begin{goal}{secondfirst}
have fun in any case
\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}
\end{document}
但我不知道你为什么使用(复杂)\l@goal
。我认为tocloft
包可以做同样的工作。但也许你有特殊的原因。以下示例是仅使用tocloft
包的解决方案。
\documentclass{book}
\usepackage{tocloft}
\usepackage{hyperref}
% create a new list incl. counter
\newlistof{goalsCounter}{goals}{List of \textbackslash goals}
% a new goal reference command
\newcommand{\goalRef}[1]{ [Goal~\ref{goal:#1}]}
\newenvironment{goal}[1]{%
\refstepcounter{goalsCounter}% step the counter
\addcontentsline{goals}{goalsCounter}{\thegoalsCounter:\quad#1}% add item to list
\addcontentsline{toc}{section}{Goal \thegoalsCounter: #1}% add item to the toc
\label{goal:#1}\textbf{Goal \thegoalsCounter\ #1:}\quad%
}
\AtEndDocument{\listofgoalsCounter}% print the list
\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
\chapter{Second}
\begin{goal}{SecondFirst}
have fun in any case
\end{goal}
Chapter contents here, referring to some goals:
\section{whatever}
Reference to a chapter 1 goal: \goalRef{FirstSecond}\\
Reference to a chapter 2 goal: \goalRef{SecondFirst}
\section{another}
Second reference to a chapter 2 goal:\goalRef{SecondFirst}
\end{document}