3

I'd like to create a ToC (table of contents)-like list for all the theorems in a bookdown pdf book. It seems a similar question to how to Create index of definitions / theorems at end of bookdown book, but I want to insert the ToC of theorems right after the ToC of the chapters and before the main body. Moreover, page numbers are needed.

There are some clues in the TeX community:

Using the closest answer among them, I could get an imperfect ToC-like list of theorems. My reproducible mini example is as follows, which includes three files.

index.Rmd:

--- 
title: "A Minimal Book Example"
site: bookdown::bookdown_site
documentclass: book
output:
  bookdown::pdf_book:
    includes:
      in_header: preamble.tex
      before_body: before_body.tex
    latex_engine: xelatex
    keep_tex: yes
---

# Prerequisites

```{block, type='tip', name = 'My Important Tip'}
This is Tip One.
```

```{lemma}
This is Lemma One.
```

```{example}
This is Example One.
```

preamble.tex:

% format of tips
\usepackage{amsthm}
\usepackage{etoolbox}
\newtheoremstyle{mystyle}
{\topsep}{\topsep}{}{}{\bfseries}{:}{\newline}
{\thmname{#1}\thmnumber{ #2}\thmnote{ (#3)}%
    \ifstrempty{#3}%
    {\addcontentsline{def}{subsection}{#1~\thetip}}%
    {\addcontentsline{def}{subsection}{#1~\thetip~(#3)}}}


\theoremstyle{mystyle}
\newtheorem{tip}{Tip}[chapter]

% toc of tips
\makeatletter
\newcommand\tipname{Tip}
\newcommand\listtipname{List of Tips}
\newcommand\listoftips{
    \section*{\listtipname}\@starttoc{def}}
\makeatother

before_body.tex:

\listoftips

After the book was built, I was happy to see a List of Tips with page numbers in the pdf output:

enter image description here

However, there were two problems:

  1. Not only the 'tip' blocks but also the lemmas in my book were listed in the table. How could I exclude the lemmas from the table?

  2. The title should be Tip 1.1: My Important Tip. How could I display the title My Important Tip in the list?

Another unimportant question is: I have the 'Example' environment as well in the mini example. Why is Lemma 1 in the list of tips, but Example 1 not? What should I do if I want to create a list of tips, a list of lemmas, and a list of examples, separately?

I guess I am quite close to the solution, but I just do not know how to move a step forward.

Update:

I just found that the following codes in a .tex file can pass the tip title 'My Important Tip' to the ToC:

\begin{tip}[My Important Tip]
This is Tip One.
\end{tip}

This will result in Tip 1.1 (My Important Tip) in the ToC of the tips. How could I specify it in a knitr block? name = 'My Important Tip' does not work.

pzhao
  • 335
  • 1
  • 10

0 Answers0