Automatically add Todo for missing reference

I'm looking for a way to add todo notes in addition to the two questionmarks wherever a \ref is undefined/missing.

Currently I'm writing a \todo manually for any \ref that I intend to fulfill later. Figures for example where I don't have the actual image yet. I would like to see this done automatically.

Patch the \@setref macro.

\documentclass{article}
\usepackage{todonotes}
\usepackage{etoolbox}
\makeatletter
\patchcmd{\@setref}{\ifx#1\relax}{\ifx#1\relax \todo{MISSING REFERENCE}}{}{}
\makeatother
\begin{document}
This is a missing reference: \ref{somefig}.
\end{document}


May not work with every package that does something with the ref mechanism.

Update:

For \autoref in the hyperref package, add (also between \makeatletter and \makeatother):

\ifdefined\HyRef@autosetref \patchcmd{\HyRef@autosetref}
{\else}{\else\todo{MISSING REFERENCE}}{}{}\fi

• Thanks... umh, you don't know by chance how to do this with \autoref from the hyperref package, do you? – Hendrik Wiese Nov 23 '16 at 12:18
• See the updated answer. – Pieter van Oostrum Nov 23 '16 at 15:28
• Please Note: As these solutions hack into the internals, they may someday break if the internals are significantly changed. – Pieter van Oostrum Nov 23 '16 at 15:33
• I'll keep that in mind, thanks. However since this is a one time solution for my master thesis I suppose (i.e. hope) it will break long after I have handed it in. – Hendrik Wiese Nov 23 '16 at 15:49