10

I want to put some comments about actual code into my document, and for that I thought I'd use \verb or \begin{verbatim}, but it turns out that the todonotes package does not like that:

\documentclass{minimal}

\usepackage{todonotes}

\begin{document}

\todo{\verb|Hello!|}

\end{document}

Which in turn gives the beautiful error:

! Argument of \reserved@a has an extra }.
<inserted text> 
                \par 
l.7 \todo{\verb|Hello!|}

A similar problem applies to \begin{verbatim}.

So... how do I get verbatim text/code into my todo notes?

4
  • 7
    It's not \todo that doesn't like \verb: \verb cannot go in the argument of any command. Do you need special characters such as backslash or braces in the "verbatim"?
    – egreg
    Jul 11, 2012 at 15:58
  • Aha, I did not know that, thank you! And yes, I do; the minimal example I gave just doesn't have that. I am commenting on mathematical (source)-code.
    – mSSM
    Jul 11, 2012 at 16:00
  • 1
    If all you need is monospaced font, you may use \ttfamily instead of \verb.
    – Jay
    Jul 11, 2012 at 16:23
  • That is not very helpful. As I wrote just above, I need extra symbols.
    – mSSM
    Jul 11, 2012 at 17:36

1 Answer 1

5

Here's a way:

\documentclass{article}
\usepackage{todonotes}
\usepackage{xparse}
\newcounter{verbtodo}
\renewcommand{\theverbtodo}{\roman{verbtodo}}
\makeatletter
\NewDocumentCommand{\verbtodo}{v}{%
  \stepcounter{verbtodo}%
  \global\@namedef{verbtodo@\theverbtodo}{#1}%
  \todo{\texttt{\expandafter\protect\csname verbtodo@\theverbtodo\endcsname}}%
}
\makeatother

\begin{document}
\verbtodo|Hello${okp}^&|

\listoftodos

\end{document}

Limitation: the \listoftodos command must go at the end of the document. It shouldn't be a big deal.


The following more complicated version allows for having \listoftodos also at the start.

\documentclass{article}
\usepackage{todonotes}
\usepackage{xparse}
\newcounter{verbtodo}
\renewcommand{\theverbtodo}{\roman{verbtodo}}
\makeatletter
\NewDocumentCommand{\verbtodo}{v}{%
  \refstepcounter{verbtodo}\label{verbtodo@\theverbtodo}%
  \global\@namedef{verbtodo@\theverbtodo}{#1}%
  \addtocontents{tdo}{\defineverbtodo{\theverbtodo}|#1|}%
  \todo{\texttt{\expandafter\protect\csname verbtodo@\theverbtodo\endcsname}}%
}
\NewDocumentCommand{\defineverbtodo}{mv}{%
  \@namedef{verbtodo@#1}{#2}}
\makeatother

\begin{document}
\listoftodos
\verbtodo|Hello${okp}^&|


\end{document}

Limitations Braces must be balanced and % is not allowed.

3
  • Thank you for your effort; I stumbled over fancyvrb, which seems to be nestable in other environments. Do you think this might be a better solution to my problem?
    – mSSM
    Jul 13, 2012 at 10:09
  • @mSSM Also the standard verbatim can go in other environments, but not in arguments. The same holds for Verbatim from fancyvrb.
    – egreg
    Jul 13, 2012 at 10:13
  • Yeah, I actually tried it out after posting my comment, and noticed the same thing. Going to use your solution.
    – mSSM
    Jul 13, 2012 at 23:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.