# Find the number of todonotes (from the previous run)

I would like to have a small '\if' there are todonotes; then show a large warning and list of todonotes on the first page. And otherwise don't.

I've got no trouble accessing the todonotes counter:

\documentclass[11pt]{book}
\usepackage[english]{babel}
\usepackage{todonotes}

\begin{document}
%\ifthenelse{ someCounter > 0}{Warning - todos left! \listoftodos\pagebreak}{}

\listoftodos

It is initially to 0=\arabic{@todonotes@numberoftodonotes}

Hello \todo{One thing to do} World\todo{A second things to do}.

And it is now  2=\arabic{@todonotes@numberoftodonotes}
\end{document}


But the @todonotes@numberoftodonotes is reset to 0 on each run. So I would like to find that count for the listoftodos -- which on the second run is populated.

How to best do this ?

The package totcount is what you're looking for.

\documentclass[11pt]{book}
\usepackage[english]{babel}
\usepackage{todonotes}
\usepackage{totcount}

\regtotcounter{@todonotes@numberoftodonotes}
\AtBeginDocument{%
\ifnum\totvalue{@todonotes@numberoftodonotes}>0
Warning --- todos left! \listoftodos\clearpage
\fi
}

\begin{document}

It is initially to 0=\arabic{@todonotes@numberoftodonotes}

Hello \todo{One thing to do} World\todo{A second things to do}.

And it is now  2=\arabic{@todonotes@numberoftodonotes}

\end{document}


Be sure to run LaTeX twice.

• Yes - that solves it most perfectly & comprehensively -- and thanks for the totcount' hint - most useful for other things as well. – Dirk-Willem van Gulik Jun 30 at 11:56

You can count the number of \contentsline calls in the \listoftodos, this way you know the number of todos, but only after \listoftodos:

\documentclass[11pt]{book}
\usepackage[english]{babel}
\usepackage{todonotes}
\usepackage{xpatch}

\newcounter{totalnumberoftodos}

\newcommand\patchcontentsline
{%
\xpretocmd\contentsline{\stepcounter{totalnumberoftodos}}{}{}%
}
\xpretocmd\listoftodos{\begingroup\patchcontentsline}{}{}
\xapptocmd\listoftodos{\endgroup}{}{}

\begin{document}
%\ifthenelse{ someCounter > 0}{Warning - todos left! \listoftodos\pagebreak}{}

\listoftodos

The total number of todo-notes was \arabic{totalnumberoftodos}.

It is initially to 0=\arabic{@todonotes@numberoftodonotes}

Hello \todo{One thing to do} World\todo{A second things to do}.

And it is now  2=\arabic{@todonotes@numberoftodonotes}

\end{document}


EDIT: A version that doesn't need \listoftodos to be used:

\documentclass[11pt]{book}
\usepackage[english]{babel}
\usepackage{todonotes}

\makeatletter
\AtEndDocument
{%
\immediate\write\@auxout
{\gdef\noexpand\totaltodos{\the\value{@todonotes@numberoftodonotes}}}%
}
\makeatother

\begin{document}
\ifnum\totaltodos>0
Warning - todos left!
\listoftodos\newpage
\fi

The total number of todo-notes was \totaltodos.

It is initially to 0=\arabic{@todonotes@numberoftodonotes}

Hello \todo{One thing to do} World\todo{A second things to do}.

And it is now  2=\arabic{@todonotes@numberoftodonotes}

\end{document}

• So the puzzle I have is that only after \listoftodos'. Because if I run this without showing the list (i.e. if that was in that if-then-else statement) -- the number is 0. Is there perhaps any way one can run the \listsoftodos without actually showing them. So you then can get the count - and include them if the count is > 0 (with large warning banners) and skip it all - if not ? – Dirk-Willem van Gulik Jun 29 at 16:26
• @Dirk-WillemvanGulik you can do a \setbox0\vbox{\listoftodos}, but if you don't want to print the todos anyway, we can get the count in a better way. – Skillmon Jun 29 at 16:58
• @Dirk-WillemvanGulik see my edit for a version that doesn't need \listoftodos to be used. – Skillmon Jun 29 at 17:03
• Oddly enough - I get a Undefined control sequence' on that totaltodos (in pdflatex). – Dirk-Willem van Gulik Jun 30 at 11:59
• @Dirk-WillemvanGulik you have to use it after \begin{document}`, as the aux-file is read in there. If you don't want this, one could also write the counter into another file, not the aux, and read in that one if it exists. – Skillmon Jun 30 at 12:04