10

For example, if I use amsbook document class and have the section Problems, how to include problem environments created like:

\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}

The chapters and sections appear in ToC and are clickable too. I want it for other environments, too.

How can I do that?

Thanks :)

2
  • what do you mean by "this part" the whole problem? or do you intend there to be a title of some sort cf the sectionheads that normally go in the tabke of contents? Dec 21, 2012 at 13:18
  • Not the whole problem of course, I want it like Problem 1...123 (page number)
    – Vahid
    Dec 21, 2012 at 13:23

2 Answers 2

10

Define the problem environment using an inner theorem-like environment and adding the entry to the TOC.

\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}

enter image description here

enter image description here

4

enter image description here

\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}
2
  • 2
    With this all theorem-like environments will be listed as problems in the TOC.
    – egreg
    Dec 21, 2012 at 14:30
  • @egreg, yes I know:-), I actually started to code it as in your answer with an inner environment but thought I'd keep it simpler to highlight the addcontentsline bit. It may be there are no other theorem environments, or you do want all such environments in the toc, in which case this is simpler, if you need individual control then your version is the way to go... Dec 21, 2012 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.