# Referencing todonotes by custom counter value?

I have my todonotes set up as follows:

\documentclass{article}
\usepackage{tikz}
\usepackage{todonotes}
\usepackage{hyperref}
\usepackage{setspace}
\usepackage{kantlipsum}

\newcounter{todoListItems}
\newcommand{\sstodo}[2][]
{\refstepcounter{todoListItems}{1}
\todo[caption={\protect\hypertarget{todo\thetodoListItems}{}\arabic{todoListItems}. #2}, #1]

\makeatletter\let\chapter\@undefined\makeatother

\begin{document}

\listoftodos

\kant[1] \sstodo{First}

\kant[2] \sstodo{Second}

\kant[3] \sstodo{Third}

\end{document}


As you can see, I have a custom counter set up to count the todos (instances of the custom \sstodo) and print the value in the list of todos. I'd like to be able to reference a given todo and have the number of the todo pulled printed with the \ref. I'd like to do this in a way where the labels for the todos are automatically generated.

I also realize that I might be thinking about how to accomplish what I'm looking for in the wrong way. What I'm after is an easy way to cross-reference todonotes that doesn't require manually adding a label to each todonote. The purpose of this is to allow for easy referencing of todo items among collaborators.

I tried using the solution here but was unable to get it to work (though that is what made me change my \addtocounter to \refstepcounter).

I'm not entirely clear what you want this to look like but here's one possibility:

To set this up, I used xparse which provides \NewDocumentCommand. This allows for more than one optional argument. This changes the syntax of \sstodo in a backwards-compatible way. Basically, it should work as before but if you want to specify the label explicitly (perhaps because the contents of the todo note is unsuitable as a label), you can write

\sstodo(mylabel){My complex todo note}


Here's the code:

\documentclass{article}
\usepackage{tikz}
\usepackage{todonotes}
\usepackage{xparse}
\usepackage{hyperref}
\usepackage{setspace}
\usepackage{kantlipsum}

\newcounter{todoListItems}
\NewDocumentCommand\sstodo{O{}d()m}{%
\refstepcounter{todoListItems}{1}
\IfNoValueTF{#2}{\label{#3}}{\label{#2}}%
\todo[caption={\protect\hypertarget{todo\thetodoListItems}{}\arabic{todoListItems}. #3}, #1]

\makeatletter\let\chapter\@undefined\makeatother

\begin{document}

\listoftodos

See todo notes \ref{First}, \ref{Second} and \ref{firstnew}.

\kant[1] \sstodo{First}

\kant[2] \sstodo{Second}

\kant[3] \sstodo(firstnew){Third}

\end{document}


It occurred to me after I posted this that you might want the following instead:

If so, you might prefer:

\documentclass{article}
\usepackage{tikz}
\usepackage{todonotes}
\usepackage{xparse}
\usepackage{hyperref}
\usepackage{setspace}
\usepackage{kantlipsum}

\newcounter{todoListItems}
\NewDocumentCommand\sstodo{O{}d()m}{%
\refstepcounter{todoListItems}%
\arabic{todoListItems}%
\IfNoValueTF{#2}{\label{#3}}{\label{#2}}%
\todo[caption={\protect\hypertarget{todo\thetodoListItems}{}\arabic{todoListItems}. #3}, #1]

\makeatletter\let\chapter\@undefined\makeatother

\begin{document}

\listoftodos

See todo notes \ref{First}, \ref{Second} and \ref{firstnew}.

\kant[1] \sstodo{First}

\kant[2] \sstodo{Second}

\kant[3] \sstodo(firstnew){Third}

\end{document}

• Both of these work except that I had included something unintended with my MWE. I didn't intend to print the number of the counter in the body of the text, only in the todonote. I managed to get rid of this in a later attempt with: \newcommand{\sstodo}[2][] {\addtocounter{todoListItems}{1} \todo[caption={\protect\hypertarget{todo\thetodoListItems}{}\arabic{todoListItems}. #2}, #1] {\begin{spacing}{1} \hfill \hyperlink{todo\thetodoListItems}{\thetodoListItems :{} #2} \end{spacing} }} but I don't understand what produced the in-text todonote number. – Dennis Feb 13 '14 at 3:52
• With your second answer it is pretty clear (the \arabic{...} after \refstepcounter{...}), but what prints the number in the body of the text with your first solution and my MWE? – Dennis Feb 13 '14 at 3:53
• It is because you had \refstepcounter{todoListItems}{1}. The '1' is just being printed as such. It is not printing the counter at all in that case which is why every instance is '1'. It was that which made me think you wanted the counter in the text but had mistakenly printed '1' in each case instead. You can just delete {1} in the first case or the \arabic{...} (as you realise) in the second. – cfr Feb 13 '14 at 16:22