# How to create sidenote helper macro which handles notes inside captions

I am trying to write a simple macro that will add a sidenote which will inline the note if it is added to a caption. To do this I am testing if I am in inner mode, but the test doesn't seem to work. Can anyone suggest what I should do? Here is an example of the failing code. The key is my definition of \addnote on line 9 which should either add a sidenote or just add in the text.

\documentclass[12pt]{article}

\usepackage{sidenotes}
\usepackage{setspace}
\usepackage{color}

\def\mysidenote#1#2{\color{#1}\marginpar{\footnotesize\setstretch{0.9}\raggedright\color{#1} #2}}

\usepackage[total={6.5in,8.5in},top=1in,left=.75in,marginparwidth=2.5in,, marginparsep=.1in,includeall]{geometry}
\begin{document}
here is some text\addnote{red}{the first note} and some more text\addnote{blue}{the second note} and blah blah.
\begin{figure}
a figure
\end{figure}
and some more text after the figure.
\end{document}


When the above code is run through pdflatex, I get:

! LaTeX Error: Float(s) lost.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
...

l.20 \end{document}


Note, that if I don't put the \ifinner test in the definition of \addnote, then instead I get:

! LaTeX Error: Not in outer par mode.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
...



So, the test is doing something, just not what I want it to do. Any suggestions would be welcome.

The test from here seems to work.

\documentclass[12pt]{article}

\usepackage{sidenotes}
\usepackage{setspace}
\usepackage{xcolor}
\makeatletter
\newcommand\ifInFloat[2]{\@ifundefined{@captype}{#2}{#1}}
\makeatother

\def\mysidenote#1#2{\color{#1}\marginpar{\footnotesize\setstretch{0.9}\raggedright\color{#1} #2}}

\usepackage[total={6.5in,8.5in},top=1in,left=.75in,marginparwidth=2.5in,, marginparsep=.1in,includeall]{geometry}
\begin{document}
here is some text\addnote{red}{the first note} and some more text\addnote{blue}{the second note} and blah blah.
\begin{figure}
a figure
\caption{figure caption