# How can I add Subtitles to ToC with ExSheets?

I know how to add questions to the Table of Contents using exsheets key questions-totoc.

I would like the Table of Contents to have question subtitles, so my students (and I) know what the questions are about at a glance.

How can I add subtitles to the ToC?

\documentclass{article}
\usepackage{exsheets}

\SetupExSheets{
questions-totoc=true,
question/name=Question
}

\begin{document}

\tableofcontents

\end{question}

\begin{question}[subtitle=Optimization]
\end{question}

\begin{question}[subtitle=Optimization with Constraints]
\end{question}

\begin{question}[subtitle={Proving Second Derivative Test for $f(x,y)$}]
\end{question}

\end{document}


One idea I had was to use \IfQuestionSubtitleTF to conditionally add to the current ToC line. However, I don't know how to add to the current ToC line.

Would this work for you?

\documentclass{article}
\usepackage{exsheets}

\def\questionName{Question}

\SetupExSheets{
%questions-totoc=true,
question/name=\questionName
}
\newcommand{\QtoToC}[2]{
\begin{question}[subtitle=#1]
#2
\end{question}
}

\begin{document}

\tableofcontents

{} % <- the Q body goes here

\QtoToC{Optimization}
{}

\QtoToC{Optimization with Constraints}
{}

\QtoToC{{Proving Second Derivative Test for $f(x,y)$}}
{}

\QtoToC{}% Q without subtitle
{}

\end{document}


Edit:

Based on his comment, this is what I think @clemens would use: :)

\documentclass{article}
\usepackage{exsheets}

\def\questionName{Question}

\SetupExSheets{
question/name=\questionName,
\questionName\space\thequestion. \IfQuestionSubtitleT{\emph{\GetQuestionProperty{subtitle}{\CurrentQuestionID}}}
}
}

\begin{document}

\tableofcontents

\end{question}

\begin{question}[subtitle=Optimization]
\end{question}

\begin{question}[subtitle=Optimization with Constraints]
\end{question}

\begin{question}[subtitle={Proving Second Derivative Test for $f(x,y)$}]
\end{question}

\begin{question}
\end{question}

\end{document}

• I'd rather use the question hooks instead of putting the environment in a new macro... – clemens Nov 13 '15 at 17:49
• @clemens I edited my answer to include your suggestion, thanks. I admit I tend to hide repeating environment calls (saves screen real estate & highlight distraction => "concentrate on the content"). – alwaysask Nov 13 '15 at 20:24
• Are you saying you never use environments (which not only are core construct of LaTeX but also do serve a very good purpose having semantics in mind)? – clemens Nov 13 '15 at 21:22
• @clemens No, I just hide them in macros (if possible) if I have to call them too often (dozens times) - as the OP MWE suggests. – alwaysask Nov 13 '15 at 21:33
• Thanks both. I certainly prefer the hook version ... I'd rather use question as the environment, to make questions easier to reuse between different exsheets documents. – Chris Chudzicki Nov 14 '15 at 4:14