# Hook for outer par mode

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?

• Perhaps you should start by explaining what your end goal is here? A \marginpar is a float just like figure and thus it cannot be used from here. But \marginnote from the package of the same name can, so why not just use that. – daleif Apr 6 '20 at 13:37
• Unrelated: yuo should never center a figure using the center env. It adds vertical space as does the figure env. Replace \begin{center} by \centering and delete \end{center} – daleif Apr 6 '20 at 13:38
• Even if one were to add some environment detection that would do the same for table as for figure in the example, the marginal note could be on quite a different page from the figure or table. Is it really wanted? \marginnote as 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 6 '20 at 13:42
• Thank you for all the suggestions. I tried to clarify the question (which makes it not so MWEish). – Andy Apr 6 '20 at 15:06
• What's the problem in using marginnote along with marginfix? – egreg Apr 6 '20 at 15:28