[Tex/LaTex] Conditional figure environment (sideways, bottom captions)

captionsdouble-sidedfloatsmemoir

For my phd dissertation I would like to have a new environment for figures that gives me the possibility to either place the caption sideways if there is enough space (say the figure width is < 0.6 column width) otherwise places the caption below the figure. The sideways caption should switch left and right based on odd even page too, and it can stretch the line width limit in that part. An example for the sideways captions: say the figure width is 0.5\columnwidth, then the caption can start from 0.6\columnwidth to 1.2\columnwidth. Could someone give me the direction on how to realize this in latex?

For my thesis I am using xelatexmemoir. This is the type of sideways figures that I am looking for:

enter image description here

EDIT:
By the helpful comment of Ipsen and some tweaking I am managed to reach to the thing that I want although not perfect yet.

Code:

\documentclass{memoir}

\usepackage{graphicx}
\usepackage[english]{babel}
\usepackage{lipsum}
\usepackage{calc}

\renewcommand*{\sidecapstyle}{%
\captionnamefont{\bfseries}%
\slshape%
}

\renewcommand*{\sidecapfloatwidth}{0.5\linewidth}
\setsidecaps{0.1\textwidth}{.55\textwidth}
\sidecapmargin{outer}
\setsidecappos{b}
\strictpagecheck
\sidecapraise 0.05\textheight

\begin{document}

\chapter{Captions}
\begin{figure}[tb]
\raggedright
\begin{sidecaption}{This is a subcaption just for illustration purposes. This is a subcaption just for illustration purposes. 
This is a subcaption just for illustration purposes. Page number is \LARGE\textbf{\thepage}}[fig:test]
\includegraphics[width=\linewidth]{Picture}
\end{sidecaption}
\end{figure}

\lipsum[2-2]

\begin{figure}[tb]
\raggedleft
\begin{sidecaption}{This is a subcaption just for illustration purposes. This is a subcaption just for illustration purposes. 
This is a subcaption just for illustration purposes - Page number is \LARGE\textbf{\thepage}}[fig:test]
\includegraphics[width=\linewidth]{Picture}
\end{sidecaption}
\end{figure}

\lipsum[1]

\end{document}

Page 1 of output:
enter image description here

Page 2 of output:
enter image description here

As you see page number in float is not correct, thus, I have problem automatically parsing \raggedleft and \raggedright after \begin{figure}. How does sidecaption find which page it is now?

Best Answer

As I understand your question you want the margin note to be ragged towards the margin it is typeset in. This answer is based on Lars Madsen's answer to memoir: sidecaption: ragged against the text block not working.

For you particular example \strictpagecheck is still needed because the float is moved to the next page. A full working example based on you work is encluded here. Please note that this is very depedent on memoir.

EDIT: The code now provides the raggedsidecap and raggedsidecap2 environments. The optional argument is the label for the caption. The first argument is the width of the float and the second is the caption text. The inside of the environment is placed in the float.

\documentclass{memoir}

\usepackage{graphicx}
\usepackage[english]{babel}
\usepackage{lipsum}
\usepackage{calc}


% This you can set any way you want
\renewcommand*{\sidecapstyle}{%
  \captionnamefont{\bfseries}%
  \slshape%
}

\sidecapmargin{outer} % Place in the outer margin
\setsidecappos{c} % Align the caption at the center of the float
\sidecapraise 0.02\textheight  % Raise the caption a little - aestetic

% ----------------- % 
% Raggedsidecaption %
% ----------------- %

\strictpagecheck  % This is necessary because of overflow to next page
\newlength{\scapindentlength}          % Length so that float is aligned right on even sides
\newlength{\maxsidecapfloatwidth}      
\newlength{\sidecapintomargin}     % The distance the sidecaption will stick into the margin

\setlength{\sidecapsep}{0.1\linewidth} % Distance between float and caption 
\setlength{\scapindentlength}{\marginparwidth+\sidecapsep+\marginparsep} % Do not touch !
\setlength{\maxsidecapfloatwidth}{0.6\linewidth}
\setlength{\sidecapintomargin}{0.25\linewidth}

% Calculate how wide the sidecation can be if it should only stick \sidecapintomargin into
% margin, have a separtion of between float and caption of \sidecapsep and the float is
% \maxsidefloatwidth.
\setlength{\sidecapwidth}{\linewidth+\sidecapintomargin-\maxsidecapfloatwidth-\sidecapsep}

% Arguments:
% 1: (Optional) the label for the caption
% 2: The width of the float
% 3: Caption text
% Between begin and end: Everything there is placed in the float.
\newenvironment{raggedsidecap}[3][]
{
  \begingroup
  \renewcommand*{\sidecapfloatwidth}{#2}
  \setlength{\sidecapwidth}{\linewidth-#2-\sidecapsep+\sidecapintomargin}
  \addtolength{\scapindentlength}{\maxsidecapfloatwidth-#2-\sidecapsep}

 \begin{sidecaption}{#3}[#1]
}
{
  \end{sidecaption}
  \endgroup
}

\newenvironment{raggedsidecap2}[3][]
{
  \begingroup
  \renewcommand*{\sidecapfloatwidth}{#2}
  \addtolength{\scapindentlength}{\maxsidecapfloatwidth-#2-\sidecapsep}
  \addtolength{\sidecapsep}{\maxsidecapfloatwidth-#2}
  \begin{sidecaption}{#3}[#1]
}
{
  \end{sidecaption}
  \endgroup
}


% Here be dragons. Overwrite of memoir methods. Do not touch  
\makeatletter  
\def\endsidecaption{%
  \m@mscapend@fbox
  \refstepcounter\@captype
  \m@mscaplabel
  \m@mscapcheckside %<---- added this
  \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}{%
  % Correctly indented from margin
  \ifscapmargleft%
    \hspace{\scapindentlength}%
  \else\fi%
  %
  \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

\begin{document}

% \chapter{Captions}

\begin{figure}
  \begin{raggedsidecap}[ref:ref]{0.4\linewidth}
    { This is a subcaption just for illustration purposes. This is a subcaption just for
      illustration purposes.  This is a subcaption just for illustration purposes. Page number
      is \LARGE\textbf{\thepage}
    }
    \includegraphics[width=\linewidth]{Picture}
  \end{raggedsidecap}
\end{figure}


\begin{figure}
  \begin{raggedsidecap2}{0.4\linewidth}
    { This is a subcaption just for illustration purposes. This is a subcaption just for
      illustration purposes.  This is a subcaption just for illustration purposes. Page number
      is \LARGE\textbf{\thepage}
    }
    \includegraphics[width=\linewidth]{Picture}
  \end{raggedsidecap2}
\end{figure}



\lipsum[2-6]
\begin{figure}
  \begin{raggedsidecap}{0.4\linewidth}
    { This is a subcaption just for illustration purposes. This is a subcaption just for
      illustration purposes.  This is a subcaption just for illustration purposes. Page number
      is \LARGE\textbf{\thepage} }
    \includegraphics[width=\linewidth]{Picture}
  \end{raggedsidecap}
\end{figure}
\begin{figure}
  \begin{raggedsidecap2}{0.4\linewidth}
    { This is a subcaption just for illustration purposes. This is a subcaption just for
      illustration purposes.  This is a subcaption just for illustration purposes. Page number
      is \LARGE\textbf{\thepage} }
    \includegraphics[width=\linewidth]{Picture}
  \end{raggedsidecap2}
\end{figure}

\end{document}