This is a follow up question to group list-of-todos by chapter. egreg's solution works great with classicthesis
and scrreprt
, and I've been trying to do the same without classicthesis
and for scrartcl
, i.e. add group the list of todos by section.
The code below is directly based on egreg's code and almost works, although it always adds the title of the last section as well, even when there is no todonote. So this is the first thing I need help fixing, and secondly there is probably an easier way to do this rather than using code based on classicthesis
.
I'm not wed to tocloft
, but I quite like it for customisations (which are not in this MWE).
\documentclass{scrartcl}
\usepackage{todonotes,tocloft,xpatch,hyperref}
% This is based on classicthesis chapter definition
\let\oldsec=\section
\renewcommand*{\section}{\secdef{\Sec}{\SecS}}
\newcommand\SecS[1]{\oldsec*{#1}}%
\newcommand\Sec[2][]{\oldsec[\texorpdfstring{#1}{#1}]{#2}}%
% https://tex.stackexchange.com/a/61267/11984
\makeatletter
\xapptocmd{\Sec}{\addtocontents{tdo}{\protect\todoline{\thesection}{#1}{}}}{}{}
\newcommand{\todoline}[1]{\@ifnextchar\Endoftdo{}{\@todoline{#1}}}
\newcommand{\@todoline}[3]{%
\@ifnextchar\todoline
{}
{\contentsline{section}{\numberline{#1}#2}{#3}{}{}}%
}
\let\l@todo\l@subsection
\newcommand{\Endoftdo}{}
\AtEndDocument{\addtocontents{tdo}{\string\Endoftdo}}
\makeatother
\begin{document}
\listoftodos
\tableofcontents
\section{My first Section}\todo{Section 1 note}
\subsection{A first Subsection}
\subsubsection{A first subsubsection}\todo{Section 1, section 1, subsection note}
\subsection{Another subsection}\todo{Section 1, section 1, subsection note 2}
\section{My second Section}
\subsection{Another subsection}\todo{Section 2, subsection 1 note}
\subsection{Another subsection}\todo{Section 2, subsection 2 note}
\section{A section without notes}
\subsection{A subsection without notes}
\subsubsection{A subsubsection without notes}
\end{document}
Best Answer
Here's a possible solution; only information for sections with
\todo
notes will be written to the List of Todos:Remarks and explanations
This is basically an adaptation of
my answer
to Include chapters in List of Figures with titletoc?The
\todo
command does the job and decide if the section info will be written to the List of Todos; whenever the\todo
command is used in the document, it will examine a boolean flag; if the boolean is false, then it adds the section information to the List of Todos and sets the boolean to true; otherwise, it does not add any information.The
\section
command was also modified to reset the boolean.\@sect
was modified to globally store the information about the corresponding title and number for the section; this information will then be used when writing to the List of Todos.Version using
titlesec
In the comments it has been mentioned that the
titlesec
package is being used (which might produce undesired results with KOMA classes; see Incompatibilities between KOMA-Script and titlesec). In this case, using theexplicit
option the code simplifies considerable, since now there's no need to hook into\@sect
to retrieve the name and number for sections: \documentclass{scrartcl} \usepackage{todonotes,xpatch,hyperref} \usepackage[explicit]{titlesec}