17

I am using the extbook environment to create a solutions manual.

I would like two table of contents on this document: one of them created the "traditional way," i.e., \tableofcontents with \section{} used for every question with hyperref used to make the table of contents clickable; and, another one with the sections sorted by topic. The problem with this set of questions that I am writing solutions for is that the questions are in numerical order, but questions with similar numbers usually are not of similar topics.

As an example, here is what I mean: let's say I have 3 questions: question 1 is on addition, question 2 is on multiplication, and question 3 is on addition. On the traditional table of contents, as I have it right now, it will sort them by page number: question 1 first, question 2 second, and question 3 third. However, I would like to create another table of contents that lets me group them by topic: perhaps something like:

Addition

Question 1..... (page number)

Question 3..... (page number)

Multiplication

Question 2.... (page number)

Furthermore, I would like both of these table of contents to exist in the same file. Is this possible using LaTeX? Apologies if this is a duplicate of a previous question - I am new here.

2 Answers 2

14
+100

This answers provides several possible solutions: the first one uses indices via the imakeidx package to produce the different divisions by topics; the second and third options use ToC-like mechanisms: the second one uses \@starttoc to produce multiple ToCs (one for each topic), and the third one shows how to use the features provided by the titletoc package to achieve the desired result.

Solution using indices

Since the questions must be sorted according to different categories, this suggests using one of the available indexing facilities.

Below I show an option using the imakeidx package. The example considers four different categories and shows how the method easily generalizes. Since we want a special style for the index entries (similar to ToC entries), we need a custom style, such as the myright.ist style below.

The style file myright.ist:

% MakeIndex style file; page numbers flushed right and dot leaders
% between entries and page numbers
delim_0  "\\dotfill "
delim_1  "\\dotfill "
delim_2  "\\dotfill "

The document itself:

\documentclass{article}
\usepackage{amsthm}
\usepackage[
  noautomatic
]{imakeidx}

\makeatletter
\xpatchcmd{\imki@putindex}
  {\immediate\closeout}
  {\ifimki@disableautomatic\else\immediate\closeout}
  {}{}
\xpatchcmd{\imki@putindex}
  {\endcsname}
  {\endcsname\fi}
  {}{}
\makeatother

\indexsetup{level=\subsection*,noclearpage,othercode={\parskip=5pt}}

\makeindex[
  name=add,
  title=Addition,
  columns=1,
  options=-s myright
]  %% Define new index of addition
\makeindex[
  name=mul,
  title=Multiplication,
  columns=1,
  options=-s myright
]  %% Define new index of multiplication
\makeindex[
  name=exp,
  title=Exponentiation,
  columns=1,
  options=-s myright
]  %% Define new index of exponentiation
\makeindex[
  name=sub,
  title=Subtraction,
  columns=1,
  options=-s myright
]  %% Define new index of subtraction

% Definition of the auxiliary environment for the questions
\theoremstyle{definition}
\newtheorem{que}{Question}

% Definition of the mian environment for the questions
% it writes an entry to the ToC; 
% it also writes an entry in the index specified in the mandatory argument 
\let\tmpa\relax
\newenvironment{question}[2][]
  {\begin{que}[#1]
  \gdef\tmpb{#2}
  \if\relax\detokenize{#1}\relax
    \gdef\tmpa{\relax}  
  \else
    \gdef\tmpa{~#1}
  \fi
  \addcontentsline{toc}{section}{Question~\theque.\tmpa}\ignorespaces
  }
  {\index[\tmpb]{Question~\theque.\tmpa}\end{que}}

% Simply produces a heading ''List of Topics'' formatted as unnumbered section
\newcommand\listoftopics{
  \section*{List of Topics}
}

\begin{document}

\tableofcontents
\listoftopics
\printindex[add]
\printindex[mul]
\printindex[exp]
\printindex[sub]

\clearpage

\begin{question}[Commutativity]{add}
Is addition of real numbers commutative?
\end{question}

\begin{question}[Commutativity]{sub}
Is subtraction of real numbers commutative?
\end{question}

\begin{question}[Associativity]{mul}
Is multiplication of quaternions associative?
\end{question}

\begin{question}[Inverse]{mul}
Does inverses for non-negative quaternions always exist?
\end{question}

\begin{question}[General properties]{exp}
Which are the general properties for exponentiation in the field of complex numbers?
\end{question}

\begin{question}{add}
Is addition of real numbers modulative?
\end{question}

\begin{question}[Modulative]{sub}
Is subtraction of real numbers modulative?
\end{question}

\end{document}

The output showing the general ToC and the new list of questions divided by topics:

enter image description here

Explanation and instructions

The code contains some general explanatory comments.

The idea is to generate the special divisions in the list of topics using the imakeidx package.

To produce the questions I used a question environment defined with an auxiliary que theorem environment defined using amsthm, but the same idea applies (mutatis mutandis) to any other implementation of the question environment.

Compilation instructions:

The document can be processed in (at least) three different ways; in all cases, make sure the file myright.ist is somewhere TeX can find it (the current working directory, for example):

1- Running makeindex: process your document (let's call it topics.tex) in the following way:

    pdflatex topics
    makeindex -s myright.ist add.idx 
    makeindex -s myright.ist mul.idx 
    makeindex -s myright.ist exp.idx 
    makeindex -s myright.ist sub.idx 
    pdflatex topics

2- Using arara: add the following lines to the document and let arara do its job (thanks to Paulo Cereda for suggesting this):

    % arara: pdflatex
    % arara: makeindex: { style: myright, files: [ add.idx, mul.idx ] }
    % arara: makeindex: { style: myright, files: [ exp.idx, sub.idx ] }
    % arara: pdflatex

3- From the editor itself: in this case, the code changes a little:

    \documentclass{article}
    \usepackage{amsthm}
    \usepackage[
      noautomatic% <---- A
    ]{imakeidx}

    \makeatletter
    \xpatchcmd{\imki@putindex}
      {\immediate\closeout}
      {\ifimki@disableautomatic\else\immediate\closeout}
      {}{}
    \xpatchcmd{\imki@putindex}
      {\endcsname}
      {\endcsname\fi}
      {}{}
    \makeatother

    \indexsetup{level=\subsection*,noclearpage,othercode={\parskip=5pt}} % <-----A

    \makeindex[
      name=add,
      title=Addition,
      columns=1,
      options=-s myright
    ]  %% Define new index of addition
    \makeindex[
      name=mul,
      title=Multiplication,
      columns=1,
      options=-s myright
    ]  %% Define new index of multiplication
    \makeindex[
      name=exp,
      title=Exponentiation,
      columns=1,
      options=-s myright
    ]  %% Define new index of exponentiation
    \makeindex[
      name=sub,
      title=Subtraction,
      columns=1,
      options=-s myright
    ]  %% Define new index of subtraction

    % Definition of the auxiliary environment for the questions
    \theoremstyle{definition}
    \newtheorem{que}{Question}

    % Definition of the mian environment for the questions
    % it writes an entry to the ToC; 
    % it also writes an entry in the index specified in the mandatory argument 
    \let\tmpa\relax
    \newenvironment{question}[2][]
      {\begin{que}[#1]
      \gdef\tmpb{#2}
      \if\relax\detokenize{#1}\relax
        \gdef\tmpa{\relax}  
      \else
        \gdef\tmpa{~#1}
      \fi
      \addcontentsline{toc}{section}{Question~\theque.\tmpa}\ignorespaces
      }
      {\index[\tmpb]{Question~\theque.\tmpa}\end{que}}

    % Simply produces a heading ''List of Topics'' formatted as unnumbered section
    \newcommand\listoftopics{
      \section*{List of Topics}
    }

    \begin{document}

    \tableofcontents
    \listoftopics
    \printindex[add]% <---- A
    \printindex[mul]% <---- A
    \printindex[exp]% <---- A
    \printindex[sub]% <---- A

    \clearpage

    \begin{question}[Commutativity]{add}
    Is addition of real numbers commutative?
    \end{question}

    \begin{question}[Commutativity]{sub}
    Is subtraction of real numbers commutative?
    \end{question}

    \begin{question}[Associativity]{mul}
    Is multiplication of quaternions associative?
    \end{question}

    \begin{question}[Inverse]{mul}
    Does inverses for non-negative quaternions always exist?
    \end{question}

    \begin{question}[General properties]{exp}
    Which are the general properties for exponentiation in the field of complex numbers?
    \end{question}

    \begin{question}{add}
    Is addition of real numbers modulative?
    \end{question}

    \begin{question}[Modulative]{sub}
    Is subtraction of real numbers modulative?
    \end{question}

    \printindex[add]% <---- B
    \printindex[mul]% <---- B
    \printindex[exp]% <---- B
    \printindex[sub]% <---- B

    \end{document}
  • First, comment out the lines marked % <---- A and uncomment the lines marked % <---- B. Process the document.

  • Comment out the lines marked % <---- B and uncomment the lines marked % <---- A. Process the document.

The code between \makeatletter, \makeatother possibilitates the first compilation method described above and was kindly suggested by egreg to correct a bug in imakeidx; in future versions of the package, the bug will be fixed and the code won't be necessary.

Solutions using lists

Using \@starttoc

This solution is much simpler than the one I gave before in this answer.

This time the idea is to use the same command \@starttoc used to produce the ToC, LoF and LoT.; the compilation process is simpler then in my other answer: process the document twice (as it is required for any of the standard ToC, LoF or LoT).

\documentclass{article}
\usepackage{amsthm}
\usepackage{pgffor}
\usepackage{etoolbox}

% Definition of the auxiliary environment for the questions
\theoremstyle{definition}
\newtheorem{que}{Question}

% Definition of the mian environment for the questions
% it writes an entry to the ToC (optional argument); 
% it also writes an entry in the topic list specified in the mandatory argument 
\makeatletter
\let\tmpa\relax
\def\addtotoc#1{\addcontentsline{#1}{subsection}{Question~\theque.\tmpa}}
\newenvironment{question}[2][]
  {\begin{que}[#1]
  \if\relax\detokenize{#1}\relax
    \gdef\tmpa{\relax}  
  \else
    \gdef\tmpa{~#1}
  \fi
  \addcontentsline{toc}{section}{Question~\theque.\tmpa}%
  \if\relax\detokenize{#2}\relax
  \else
    \forcsvlist\addtotoc{#2}
  \fi\ignorespaces
  }
  {\end{que}}

% Produces a heading ''List of Topics'' formatted as unnumbered section
% and it also generates the list of questions sorted by topics
\newcommand\listoftopics[1]{
  \section*{List of Topics}
  \foreach \Title/\Name in {#1}
  {
    \subsection*{\Title}
    \@starttoc{\Name}
  } 
}
\makeatother

\begin{document}

\tableofcontents
\listoftopics{Addition/add,Multiplication/mul,Exponentiation/exp,Subtraction/sub}

\clearpage

\begin{question}[Commutativity]{add}
Is addition of real numbers commutative?
\end{question}

\begin{question}[Commutativity]{sub}
Is subtraction of real numbers commutative?
\end{question}

\begin{question}[Associativity]{mul}
Is multiplication of quaternions associative?
\end{question}

\begin{question}[Inverse]{mul}
Does inverses for non-negative quaternions always exist?
\end{question}

\begin{question}[Common properties]{mul,add,sub}
Which properties do addition, subtraction and multiplication of real numbers have in common?
\end{question}

\begin{question}[General properties]{exp}
Which are the general properties for exponentiation in the field of complex numbers?
\end{question}

\begin{question}{add}
Is addition of real numbers modulative?
\end{question}

\begin{question}[Modulative]{sub}
Is subtraction of real numbers modulative?
\end{question}

\begin{question}[Bonus]{}
Can you give an example of non-associative binary operation on the real numbers? 
\end{question}

\end{document}

enter image description here

Explanation and remarks

  • The main command to generate the list by topics now is \listoftopics with a mandatory argument; this mandatory argument is a comma separated list of pairs of the form <title>/<name>, where <title> is the string used as the title for the topic and <name> is the string used to generate the list (and the extension of the associated auxiliary file). For example, in the code above all one has to do to produce the list of topics is to call

    \listoftopics{Addition/add,Multiplication/mul,Exponentiation/exp,Subtraction/sub}
    
  • The environment used to typeset> the questions has the syntax

    \begin{question}[<note>]{<partial tocs>}
    contents
    \end{question}
    

    where <note> is an optional annotation for the question that will be typeset in the document and also in the general ToC. <partial tocs> is a comma separated list of some of the <name>s used in \listoftopics; the question will be listed in the topics associated to each of the declared <name>s. For example, Question 5 "Common properties" in the example above was added to three of the topic lists using

    \begin{question}[Common properties]{mul,add,sub}
    Which properties do addition, subtraction and multiplication of real numbers have in common?
    \end{question}
    

    If the mandatory argument is empty, as in

    \begin{question}[<note>]{}
    contents
    \end{question}
    

    then the question is added only to the general ToC (see Question 9 "Bonus" in the example code).

  • The additional packages used were pgffor used to easily produce the loop for the generation of the new topic list and etoolbox for the loop used to write to several of the topic entries.

Using the titletoc package:

This approach, using titletoc, is similar to the one above; however, since the auxiliary file used by titletoc for all partial ToCs is the same, some addition work has to be done here to stop and resume the partial ToCs when necessary:

\documentclass{article}
\usepackage{amsthm}
\usepackage{pgffor}
\usepackage{titletoc}

% Definition of the auxiliary environment for the questions
\theoremstyle{definition}
\newtheorem{que}{Question}

% Definition of the mian environment for the questions
% it writes an entry to the ToC (optional argument); 
% it also writes an entry in the topic list specified in the mandatory argument 
\makeatletter
\let\tmpa\relax
\let\tmpb\relax
\newenvironment{question}[2][]
  {\if\relax\detokenize{#2}\relax
     \gdef\tmpb{\relax}  
   \else
     \resumecontents[#2]
     \gdef\tmpb{#2}  
   \fi  
   \begin{que}[#1]
   \if\relax\detokenize{#1}\relax
     \gdef\tmpa{\relax}  
   \else
     \gdef\tmpa{~#1}
   \fi
   \addcontentsline{toc}{section}{Question~\theque.\tmpa}\ignorespaces
  }
  {\end{que}\if\relax\tmpb\relax\else\stopcontents[\tmpb]\fi}

% Produces a heading ''List of Topics'' formatted as unnumbered section
% and it also generates the list of questions sorted by topics
\newcommand\listoftopics[1]{
  \section*{List of Topics}
  \foreach \Title/\Name in {#1}
  {
    %\subsection*{\Title}
    \startcontents[\Name]
    \printcontents[\Name]{l}{1}{\subsection*{\Title}}
    \stopcontents[\Name]
  } 
}
\makeatother

\titlecontents{lsection}
  [3.8em]
  {}
  {\contentslabel{2.3em}}
  {\hspace*{-2.3em}}
  {\titlerule*[1pc]{.}\contentspage}

\begin{document}

\tableofcontents
\listoftopics{Addition/add,Multiplication/mul,Exponentiation/exp,Subtraction/sub}

\clearpage

%\resumecontents[add]
\begin{question}[Commutativity]{add}
Is addition of real numbers commutative?
\end{question}
%\stopcontents[add]

\begin{question}[Commutativity]{sub}
Is subtraction of real numbers commutative?
\end{question}

\begin{question}[Associativity]{mul}
Is multiplication of quaternions associative?
\end{question}

\begin{question}[Inverse]{mul}
Does inverses for non-negative quaternions always exist?
\end{question}

\begin{question}[General properties]{exp}
Which are the general properties for exponentiation in the field of complex numbers?
\end{question}

\begin{question}{add}
Is addition of real numbers modulative?
\end{question}

\begin{question}[Modulative]{sub}
Is subtraction of real numbers modulative?
\end{question}

\begin{question}[Bonus]{}
Can you give an example of non-associative binary operation on the real numbers? 
\end{question}

\end{document}

enter image description here

2

To create two TOCs in a single document use the titletoc package. Here is a minimum example :

\documentclass{book}
\usepackage{titletoc}

\begin{document}

\begin{titlepage}
        This is the Title page.
\end{titlepage}

\startlist{toc}
\printlist{toc}{}{\section*{My  toc}}

\tableofcontents

\chapter{My 1st chapter}
\chapter{My 2nd chapter}
\chapter{My 3rd chapter}

\end{document}

Regarding SORTING BY TOPICs, try to read this two posts:

1
  • 1
    I'm sorry but I don't see how your example helps to solve the problem posed in the question. Apr 24, 2014 at 4:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .