1

I'm using the code I found in Collect contents of lists at beginning of document with modified names so that it produces a list of questions

\documentclass[a4paper,11pt]{book}

\usepackage[T1]{fontenc}
\usepackage[english]{babel}

\let\originalcontentsline\contentsline
\usepackage{hyperref}

\makeatletter
\newcommand{\printquestions}{%
  \section*{Questions}                     
  \begin{enumerate}
  \let\contentsline\originalcontentsline
  \def\@noitemerr{\@latex@warning{Empty questions list}}%
  \@starttoc{qst}
  \end{enumerate}
}
\newcommand{\l@qst}[3]{#1}
\makeatother

\NewEnviron{questions}
 {%
  \addcontentsline{qst}{qst}{%
    \noexpand\unexpanded{\unexpanded\expandafter{\BODY}}%
  }%
 }


\begin{document}
\chapter{one}
\begin{questions}
\item some question
\end{questions}

\chapter{two}
\begin{questions}
\item some other question
\end{questions}


\end{document}

but I would like to adapt it in a few ways:

  1. The way the code is at the moment, it takes the items in the list, removes them from its location, and places them wherever my $\printquestions$ is located. I would like it to instead copy the items to that location without removing them, sort of like a table of contents (questions in this case).

  2. I would like to be able to click on the collected item so that it takes me to the original location of the question within the file, so I can add an answer to the question later on without having to look through the document.

  3. Since ideally I would have one question at a time within the $\begin{questions}... \end{questions}$ it would be better to have something like $\question{some question in here}$ in the fashion of a subsubsection, which I managed by using $\newcommand{\question}{\subsubsection}$; and it actually solves most of the above, provided I add

    \setcounter{tocdepth}{3}
    \setcounter{secnumdepth}{3}

in the preamble, except that it does not collect all the questions together, but rather places them in the table of contents within their hierarchy, and that defeats the purpose of not having to look too hard for them in the document.

So, basically I want to create a command that allows me to insert questions, in a sort of "subsection" or "theorem" like style; and that all such instances are collected in a $\listofquestions$ that I can display at the beginning of the document, and that works with hyperref.

I also tried to implement a solution based on Making a list of hypothesis and references to the hypothesis

% *************** Question ***************
\newtheorem{questionin}{Question}[chapter]
\newtheorem{questionaux}{Question}[chapter]
\usepackage{environ,etoolbox}
\makeatletter
\def\addtoquestionlist{\addtheoremline{questionaux}}
\NewEnviron{question}{%
  \refstepcounter{questionaux}%
  \expandafter\addtoquestionlist\expandafter{\expandafter\ignorespaces\BODY}
  \begin{questionin}\BODY\end{questionin}
}
\patchcmd\listtheorems
  {\begingroup}
  {\begingroup\let\label\@gobble}
  {}{}
\makeatother

but I do not understand enough of how it works to make the proper changes other than the names.

When I use $\listtheorems$ it prints questionaux.1.1 questionin.1.1,

without even a header, and it's not exactly what I have in mind. I would rather it printed the question itself as would both of the above solutions (the one with $\begin{question}\item \end{question}$, and the one where I just turn it into a subsection.

2

Here is a solution using etoc package.

Note one need to protect fragile commands.

\documentclass{book}

\usepackage{chngcntr}%   counter subsubsection(questions) within chapter
\usepackage{etoc}
\usepackage{hyperref}

\let\question\subsubsection
\counterwithin{subsubsection}{chapter}
\setcounter{secnumdepth}{3}
%\setcounter{tocdepth}{2}

\newcommand{\printquestions}{%
\begingroup
\etocsetlevel{subsubsection}{2}%
\etocsetlevel{part}{3}%
%\etocsetlevel{chapter}{3}%
\etocsetstyle{chapter}{}{}{\addvspace{10pt}}{}
\etocsetlevel{section}{3}%
\etocsetlevel{subsection}{3}%
\etocsetstyle{subsubsection}{}{}
{\noindent\etocnumber\hskip.5em\etocname\hfill\etocpage\par}{}
\etocsettocstyle{\section*{Questions}}{}
\tableofcontents
\endgroup}

\begin{document}
\tableofcontents
\printquestions
\chapter{one}
\section{One foo}
\question{some question}

\chapter{Two}
\section{Two foo}
\question{some other question}

\end{document}

Another solution (without chngcntr)

\documentclass{book}

\usepackage{etoc}
\usepackage{hyperref}

\setcounter{secnumdepth}{3}
\newcounter{question}[chapter]
\renewcommand\thequestion{\thechapter.\arabic{question}}
\makeatletter 
\newcommand\question{\@startsection{question}{3}{\z@}%
                                     {-3.25ex\@plus -1ex \@minus -.2ex}%
                                     {1.5ex \@plus .2ex}%
                                     {\normalfont\normalsize}}
\let\questionmark\@gobble
\makeatother
\etocsetlevel{question}{3}%

\newcommand{\printquestions}{%
\begingroup
\etocsetlevel{question}{2}%
\etocsetlevel{part}{3}%
%\etocsetlevel{chapter}{3}%
\etocsetstyle{chapter}{}{}{\addvspace{10pt}}{}
\etocsetlevel{section}{3}%
\etocsetlevel{subsection}{3}%
\etocsetstyle{question}{}{}
{\noindent\etocnumber\hskip.5em\etocname\hfill\etocpage\par}{}
\etocsettocstyle{\section*{Questions}}{}
\tableofcontents
\endgroup}

\begin{document}
\tableofcontents
\printquestions
\chapter{one}
\section{One foo}
\question{some question}

\chapter{Two}
\section{Two foo}
\question{some other question}

\end{document}

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.