自动生成包含完整定义和页码引用的附录

自动生成包含完整定义和页码引用的附录

我有一份包含大量定义的大型文档。为了方便起见,我想创建一个附录,将所有定义都放在一个地方。理想情况下,附录应打印定义在文档中的显示方式,包括原始引用编号和定义的完整内容;此外,附录还应提供定义的页码参考。我的文档目前使用 ntheorem 来定义类定理环境(包括定义),如果可能的话,我希望保持这种方式。我的想法如下所示。

\documentclass{report}
    \usepackage[standard]{ntheorem}

\begin{document}

\begin{definition}
The body of my first definition.
\end{definition}

\begin{definition}
The body of my second definition.
\end{definition}

\appendix

\begin{description}
\item[Definition 1 (page 1)] The body of my first definition.
\item[Definition 2 (page 1)] The body of second definition.
\end{description}

\end{document}

答案1

简单:收集定义文本并在最后打印出来。;-)

我们需要处理\label主体中可能的命令以避免重复。因此,如果\label找到命令,则将其参数用于\pageref,否则将自动生成标签。

这些主体与相关命令一起被收集在令牌寄存器中\item

\documentclass{report}
\usepackage[standard]{ntheorem}
\usepackage{environ}

\makeatletter
\let\ntheorem@definition\definition
\let\ntheorem@enddefinition\enddefinition
\newtoks\definition@toks
\newcounter{definitioncount}

\RenewEnviron{definition}[1][]{%
  \@tempswafalse
  \expandafter\catch@definitionlabel\BODY\label\@@nil\@nil
  \if\relax\detokenize{#1}\relax
    \ntheorem@definition
  \else
    \ntheorem@definition[#1]%
  \fi
  \if@tempswa
    \refstepcounter{definitioncount}%
    \edef\definition@label{\romannumeral\value{definitioncount}}%
    \label{\definition@label}%
  \fi
  \BODY
  \ntheorem@enddefinition
  \edef\@tempa{%
    \noexpand\item[Definition\noexpand~\thedefinition\noexpand~%
    (page\noexpand~\noexpand\pageref{\definition@label})]%
    \unexpanded\expandafter{\BODY}%
  }
  \global\definition@toks=\expandafter{\the\expandafter\definition@toks\@tempa}%
}
\def\catch@definitionlabel#1\label#2#3\@nil{%
  \if\relax\detokenize{#3}\relax
    % no \label
    \@tempswatrue
  \else
    % \label
    \def\definition@label{#2}%
  \fi
}
\def\printdefinitions{%
  \begin{description}\let\label\@gobble
  \the\definition@toks
  \end{description}
}
\makeatother

\begin{document}

\begin{definition}\label{first}
The body of my first definition.
\end{definition}

\begin{definition}[Something]
The body of my second definition.
\end{definition}

\appendix

The collection of definitions follows.

\printdefinitions

\end{document}

在此处输入图片描述

ntheorem没有必要。这是 的版本amsthm

\documentclass{book}
\usepackage{amsthm}
\usepackage{environ}

\theoremstyle{definition}
\newtheorem{defnINN}{Definition}[chapter]

\makeatletter
\newtoks\definition@toks
\newcounter{definitioncount}

\NewEnviron{defn}[1][]{%
  \@tempswafalse
  \expandafter\catch@definitionlabel\BODY\label\@@nil\@nil
  \if\relax\detokenize{#1}\relax
    \defnINN
  \else
    \defnINN[#1]%
  \fi
  \if@tempswa
    \refstepcounter{definitioncount}%
    \edef\definition@label{\romannumeral\value{definitioncount}}%
    \label{\definition@label}%
  \fi
  \BODY
  \enddefnINN
  \edef\@tempa{%
    \noexpand\item[Definition\noexpand~\thedefnINN\noexpand~%
    (page\noexpand~\noexpand\pageref{\definition@label})]%
    \unexpanded\expandafter{\BODY}%
  }
  \global\definition@toks=\expandafter{\the\expandafter\definition@toks\@tempa}%
}
\def\catch@definitionlabel#1\label#2#3\@nil{%
  \if\relax\detokenize{#3}\relax
    % no \label
    \@tempswatrue
  \else
    % \label
    \def\definition@label{#2}%
  \fi
}
\def\printdefinitions{%
  \begin{description}\let\label\@gobble
  \the\definition@toks
  \end{description}
}
\makeatother

\begin{document}

\chapter{Title}

\begin{defn}\label{first}
The body of my first definition.
\end{defn}

\begin{defn}[Something]
The body of my second definition.
\end{defn}

\appendix

\chapter{Definitions}

The collection of definitions follows.

\printdefinitions

\end{document}

相关内容