8

I'm using the todonotes package to add notes in my documents. The \listoftodos command adds a ToC for todos.

Instead of the page numbers in this ToC, I would like to display another value, for example \thesection. The todonotes package manipulates a ToC named .tdo, so I'm guessing I can do that with tocloft but so far I've failed to achieve it.

Here is a MWE:

\documentclass{scrbook}

\usepackage{todonotes}
\usepackage{tocloft}
\usepackage{lipsum}

\begin{document}

\chapter{My first chapter}

\section{A first section}

\lipsum

\todo{This is a note}

\section{Another section}

\lipsum

\todo{This is another note}


\listoftodos


\end{document}

The two notes have page numbers 2 and 3 respectively. I would like them to show 1.1 and 1.2 (section numbers) instead. How can I do that?

raphink
  • 31,894

2 Answers2

4

Update!!!

The following solution sets the correct width of \@pnumwidth

\documentclass{scrbook}

\usepackage{todonotes}
\usepackage{tocloft}
\usepackage{lipsum}
\usepackage{showframe}
\makeatletter
\def\myaddcontentsline#1#2#3{%
  \addtocontents{#1}{\protect\contentsline{#2}{#3}{see \thesection\ at p. \thepage}}}
\renewcommand{\@todonotes@addElementToListOfTodos}{%
    \if@todonotes@colorinlistoftodos%
        \myaddcontentsline{tdo}{todo}{{%
            \colorbox{\@todonotes@currentbackgroundcolor}%
                {\textcolor{\@todonotes@currentbackgroundcolor}{o}}%
            \ \@todonotes@caption}}%
    \else%
        \myaddcontentsline{tdo}{todo}{{\@todonotes@caption}}%
    \fi}%
\newcommand*\mylistoftodos{%
  \begingroup
       \setbox\@tempboxa\hbox{see 9.9 at p. 99}%
       \renewcommand*\@tocrmarg{\the\wd\@tempboxa}%
       \renewcommand*\@pnumwidth{\the\wd\@tempboxa}%
       \listoftodos%
  \endgroup
}
\makeatother

\begin{document}
\chapter{My first chapter}
\section{A first section}
\lipsum
\todo{This is a note}
\section{Another section}
\lipsum
\todo{This is another note}

\mylistoftodos
\end{document}

The result is:

enter image description here

Marco Daniel
  • 95,681
  • @Marco: the \protect in lines 6 and 11 do nothing, since they only protect an open brace. I think that a bug report should be filed to the package's author. – egreg May 23 '11 at 07:55
  • @Egreg: You are right. I only copied the peace of code :-) . Do you send the bug greport. – Marco Daniel May 23 '11 at 08:20
  • It doesn't work for me : ! LaTeX Error: \@todonotes@addElementToListOfTodos undefined. – raphink May 24 '11 at 06:31
  • Which version of TL do you have? Maybe you only need an update. – Marco Daniel May 24 '11 at 07:32
  • I have the standard packaged version in Ubuntu Natty, which is 2009-11. – raphink May 25 '11 at 13:36
  • Ok. That`s the problem. You should install TL2010 with all updated packages. – Marco Daniel May 25 '11 at 18:20
  • @MarcoDaniel Thanks for the nice example of how to alter the appearance of the list of todos. May I include the example in the documentation of the todonotes package? – midtiby Oct 22 '11 at 10:13
  • @egreg The extra \protect statements you mention, where are they located? Can you give line numbers and version information of the todonotes.sty file? Best Henrik – midtiby Oct 22 '11 at 10:15
  • @midtiby: I am the author of mdframed. Some examples in my documentation based on examples/question of this site. So I think it is a good idea ;-) – Marco Daniel Oct 22 '11 at 10:16
  • @midtiby All the \protect commands in todonotes.sty are useless, as they simply precede an open brace. – egreg Oct 22 '11 at 10:22
  • 2
    To make it compatible with hyperref it should be \addtocontents{#1}{\protect\contentsline{#2}{#3}{see \thesection\ at p. \thepage}{}}} (extra pair of curly braces at the end), @midtiby: you may want to update the example in the todonotes manual. – Jörg Jun 22 '12 at 10:54
  • I find that solution not completely satisfactory as pagenumbers and or sectionnumbers are not hyperlinks. I guess I'll ask a follow-up question on that (and other issues) soon. – Jörg Jun 22 '12 at 21:18
  • How can I print the section name next to the number? – white_gecko Dec 11 '18 at 12:13
1

Since it is the only ToC in my document, I have found that redifining \addcontentsline works fine and is quite straightforward:

 \renewcommand{\addcontentsline}[3]{%
   \addtocontents{#1}{\protect\contentsline{#2}{#3}{\tododate}{section*.\thesection}}
 }

The only side effect I've seen is that it ruins the bookmarks in the PDF. That is OK for my draft (which is why I use todonotes), but not for my final document, so my solution so far is to make a package that wraps up todonotes and passes it the disable option:

\ProvidesPackage{review}
\let\review@disable\@empty
\let\review@dateinlist\@empty
\DeclareOption{disable}{\def\review@disable{true}}
\DeclareOption{dateinlist}{\def\review@dateinlist{true}}
\ProcessOptions


% Put dates instead of page numbers in listoftodos
\newcommand{\tododate}{%
  \thesection~\chaphead
}

\ifx\review@disable \@empty
  \usepackage[french,colorinlistoftodos,textsize=small]{todonotes}
  \ifx\review@dateinlist \@empty
  \else
    \renewcommand{\addcontentsline}[3]{%
      \addtocontents{#1}{\protect\contentsline{#2}{#3}{\tododate}{section*.\thesection}}
    }
  \fi
\else
  \usepackage[french,colorinlistoftodos,textsize=small,disable]{todonotes}
\fi

This way, I can call my package in different ways:

\usepackage{review} % normal style
\usepackage[dateinlist}{review} % date instead of page numbers
\usepackage[disable]{review} % disable todonotes and do not mess up bookmarks
raphink
  • 31,894