[Tex/LaTex] Adding \todo inside section title

todonotes

I am using the todonotes package and I wanted to add a note inside a section title like:

\section{This is the section heading\todo{We should rethink the section title.}}

However, this is throwing the “Not in outer par mode” error. I understand why, but I wonder if there is a way to do this.

I had previously asked about putting todo inside a caption, and the proposed solution of \todo[inline]{...} works in that case, but not inside \section{...}

EDIT: After some help, I realized that the error is in the generation of the table of contents. Here is a MWP:

\documentclass{book}

\usepackage{todonotes}
\long\def\intodo#1{\todo[inline]{#1}}

\begin{document}

\tableofcontents

\chapter{Chapter 1}

\section{Another Section Name \intodo{We should rethink the section title for this as well.}}

\end{document}

The error that I get is:

./test.toc:2: Leaders not followed by proper glue.
<to be read again> 
                   \hfill 
l.2 ...e]{We should rethink the section title}}{3}

Best Answer

Similar to Fran's idea, with a macro, but redefining it in the ToC to disable its meaning there, such that \section[short title]{Other \todo} is not needed (but possible).

In order to get nice output in the ToC, the \intodo command should be robust.

The inline option is not necessary in this approach, since the ToDo - Content does not enter the ToC area.

\documentclass{article}
\usepackage{todonotes}

\DeclareRobustCommand{\intodo}[1]{%
  \todo[inline]{#1}%
}

\addtocontents{toc}{\begingroup\protect\renewcommand{\protect\intodo}[1]{}}
\AtEndDocument{%
  \addtocontents{toc}{\endgroup}
}

\begin{document}
\tableofcontents
\section{Section Name 
\intodo{We should rethink the section title}}
Some text 
\section{Another Section Name\intodo{We should rethink the section title for this as well.}}
More some text 
\end{document}

enter image description here

Here's the output if the inline option is not used:

enter image description here

A variant using the fact that the TOC is typeset in a group to begin with: (proposed by @egreg)

\documentclass{book}

\usepackage{todonotes}
\usepackage{etoolbox}

\newrobustcmd\intodo[1]{\todo[inline]{#1}}

\makeatletter
% fix \intodo so it does nothing in the TOC
\patchcmd{\@starttoc}
  {\begingroup}
  {\begingroup\let\intodo\@gobble}
  {}{}
\makeatother

\begin{document}

\tableofcontents

\chapter{Chapter 1}

\section{Another Section Name \intodo{We should rethink the section title for this as well.}}

\end{document}
Related Question