Is it possible to execute macros after the correct mode is 'reached?'
\documentclass{article}
\usepackage{etoolbox}
\usepackage{marginfix}
\newcommand{\mymarginpar}[1]{\AfterEndEnvironment{figure}{\marginpar{#1}}}
%\newcommand{\mymarginpar}[1]{\AfterBackInOuterParMode{\marginpar{#1}}}
\begin{document}
\begin{figure}
\begin{center}
My figure goes here
\caption[Caption for LOF]{bar\mymarginpar{A note}}
\end{center}
\end{figure}
\begin{table}
\centering
my table goes here
\caption[Caption for LOF]{bar\mymarginpar{Another note}}
\end{table}
\end{document}
I also considered a simple if/elseif detection of the environment, i.e. figure or table, but in this question it is already discussed, how nested environments will not work. The first example is called in a center not figure environment?!
My naïve idea idea would be some kind of \AfterBackInOuterParMode. Is it possible to achieve this?
-- EDIT:
People ask to use MWEs in questions so I tried to do that. However, why other solutions can not be considered gets lost this way. So, here is a little bit of background: In a template (we made) to generate PhD theses or lecture notes, there are two most common complaints.
One of them is the (disability) to use sidenotes in captions, which is quite common if you utilize them for the references as well. In the end, it looks like this. You can also see, why any kind of manual adjustment (offset etc.) is out of the question, since two or more output files (one for a kindle) are generated from the same input file. marginnote cannot be used, because we use marginfix. Otherwise, the notes will overlap. This causes all kind of issues, one of them described here.
The \begin{center} was only added to exemplify nested environment. If I detect the actual (inner) environment, I would get center and not figure.
In total, is there a way to define mymarginpar such that is works in the MWE?
\marginparis a float just likefigureand thus it cannot be used from here. But\marginnotefrom the package of the same name can, so why not just use that. – daleif Apr 06 '20 at 13:37centerenv. It adds vertical space as does thefigureenv. Replace\begin{center}by\centeringand delete\end{center}– daleif Apr 06 '20 at 13:38tableas forfigurein the example, the marginal note could be on quite a different page from the figure or table. Is it really wanted?\marginnoteas suggested by @daleif sounds like a better idea, otherwise probably the OP should better explain how the whole should behave (IOW, this could be an XY problem). – frougon Apr 06 '20 at 13:42marginnotealong withmarginfix? – egreg Apr 06 '20 at 15:28\AfterBackInOuterParModeto do, the caption at no point is processed in outer par mode so you could detect that and so avoid the error by not making the marginal note but I don't think that is what you want) – David Carlisle Apr 06 '20 at 16:22\AfterEndEnvironment{figure}{\marginpar{#1}}in a figure environment and\AfterEndEnvironment{table}{\marginpar{#1}}in a table environment. Here @egreg explained why and how it fails if the environments are nested. So, I was hoping for something that can even detect the 'outer' figure. Does that make sense? – Andy Apr 06 '20 at 16:40