# memoir: sidecaption: ragged against the text block not working

I want to use the sidecaption environment in a figure with memoir and have the caption text be ragged against the text block. But when the figure is moved to the next page, the caption is on the correct side but the raggedness is wrong. I have created the following MWE according to the memoir manual but even with \strictpagecheck it does not work, no matter how many luatex runs I do:

\documentclass[a4paper,twoside,draft]{memoir}

\usepackage{fixltx2e}
\usepackage{calc}

\usepackage{luatextra}
\usepackage{lipsum}
\usepackage{ragged2e}

\marginparmargin{outer}
\setmpjustification{\RaggedLeft}{\RaggedRight}

% margin figure and caption typeset ragged against text block

\sidecapmargin{outer}
\setsidecappos{t}
\setsidecaps{\marginparsep}{\marginparwidth}

\setlength{\fboxsep}{0pt}

\begin{document}

\lipsum[1]
\begin{figure}[p]
\strictpagecheck
\begin{sidecaption}{This is an example text. It should be ragged on the page
border, not on the side of the figure.}
\fbox{\parbox[t][\textheight - 4pt][c]{\linewidth - 3pt}{test}}
\end{sidecaption}
\end{figure}
\lipsum

\end{document}

-

A simpler one, basically we just need to execute \m@mscapcheckside before the contents is stored inside a box

\makeatletter
\def\endsidecaption{%
\m@mscapend@fbox
\refstepcounter\@captype
\m@mscaplabel
\begin{lrbox}{\m@mscap@capbox}%
\begin{minipage}[c]{\sidecapwidth}%
\sidecapstyle
\@caption\@captype[\m@mscap@fortoc]{\m@mscap@forcap}
\end{minipage}%
\end{lrbox}%
\m@mscapopboxes}
\renewcommand*{\m@mscapopboxes}{%
\m@mcalcscapraise
\usebox{\m@mscap@fbox}%\m@mscapcheckside %<--- removed here
\ifscapmargleft%
\rlap{\kern-\m@mscaplkern
\raisebox{\m@mscapraise}{\usebox{\m@mscap@capbox}}}%
\else%
\rlap{\kern\sidecapsep
\raisebox{\m@mscapraise}{\usebox{\m@mscap@capbox}}}%
\fi
\gdef\m@mscapthisside{}%
\@mem@scap@afterhook%
}
\makeatother

-

This seems to do the trick

\def\endsidecaption{%
\m@mscapend@fbox
\refstepcounter\@captype
\m@mscaplabel
\def\mem@evil{\begin{minipage}[c]{\sidecapwidth}%
\sidecapstyle
\@caption\@captype[\m@mscap@fortoc]{\m@mscap@forcap}
\end{minipage}}%
\begin{lrbox}{\m@mscap@capbox}%
\mem@evil
\end{lrbox}%
\m@mscapopboxes}
\renewcommand*{\m@mscapopboxes}{%
\m@mcalcscapraise
\usebox{\m@mscap@fbox}\m@mscapcheckside
\ifscapmargleft%
\rlap{\kern-\m@mscaplkern
\raisebox{\m@mscapraise}{\mem@evil}}%
\else%
\rlap{\kern\sidecapsep
\raisebox{\m@mscapraise}{\mem@evil}}%
\fi
\gdef\m@mscapthisside{}%
\@mem@scap@afterhook%
}


basically we need two things, we need to have the caption inside a box in order to do some measuring, and we need to make sure that the caption is actually typeset at the right momemnt.

-