如何在目录中添加环境?

如何在目录中添加环境?

例如,如果我使用amsbook文档类并且有部分Problems,如何包含problem如下创建的环境:

\documentclass[a4paper,12pt, reqno, oneside]{amsbook}
\usepackage{hyperref}

\begin{document}
\frontmatter
\setcounter{tocdepth}{4}
\tableofcontents
\mainmatter
\section{Problems}
\begin{problem}
...% I want this part to appear also in the table of contents.
\end{problem}
\end{document}

章节和部分显示在目录中,也可以点击。我也希望它适用于其他环境。

我怎样才能做到这一点?

谢谢 :)

答案1

problem使用内部定理类环境定义环境并将条目添加到目录。

\documentclass{amsbook}
\usepackage{hyperref}

\newtheorem{problemx}{Problem}[section]
\newenvironment{problem}[1][]
 {\expandafter\problemx\if\relax\detokenize{#1}\relax\else[#1]\fi
  \addcontentsline{toc}{subsection}
    {\protect\tocsubsection{}{\theproblemx}{Problem\if\relax\detokenize{#1}\relax\else\ (#1)\fi}}%
 }
 {\endproblemx}

\setcounter{tocdepth}{2}% subsections in toc

\begin{document}

\frontmatter
\tableofcontents
\mainmatter
\chapter{Title}
\section{A}
\subsection{B}
text

\section{Problems}
\begin{problem}
What is $1+1$?
\end{problem}

\begin{problem}[Easy]
Show that every even number greater than $2$ is the sum of two prime numbers, not necessarily distinct.
\end{problem}

\end{document}

在此处输入图片描述

在此处输入图片描述

答案2

在此处输入图片描述

\documentclass[a4paper,12pt, reqno, oneside]{amsbook}
\usepackage{hyperref}

\makeatletter
\newtheorem{problem}{Problem}

\let\saved@begintheorem\@begintheorem
\def\@begintheorem #1#2[#3]{%
\saved@begintheorem{#1}{#2}[{#3}]%
\addcontentsline{toc}{subsection}{Problem \theproblem: #3}}
\makeatother

\begin{document}
\frontmatter
\setcounter{tocdepth}{4}
\tableofcontents
\mainmatter
\section{Problems}

\begin{problem}[fermats last theorem]
\[x^n+y^n=z^n\]
\end{problem}
\end{document}

相关内容