12

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 xelatex - memoir. 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?

3
  • 1
    switching bottom or to the right is fairly easy, but switching sides would require fairly extensive changes (probably simplest to do a multiple pass version where you extract from the aux file reference the page number) The problem is that latex sets floats into a box before it knows which page they will land on. Oct 11, 2012 at 10:26
  • @DavidCarlisle, It seems that the answer on the bottom switches sides correctly.
    – rowman
    Oct 13, 2012 at 5:38
  • yes i should have noticed you are using memoir, which does of course do lots of things:-) Oct 13, 2012 at 20:51

2 Answers 2

3
+50

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}
10
  • Thanks Munken. I don't have any problem with the caption itself since I want it justified as it is in the post. What I need is removing the \raggedleft and \raggedright after \begin{figure}.
    – rowman
    Oct 24, 2012 at 8:55
  • @rowman Have updated the code, so the text will not flow out of the page. The problem here is that we need to measure the width of the image. This can be accomplished by making this a macro like \sidecap[label]{caption}{onpage} Where the onpage would be the \includegraphics statement. Do I understand you correctly that if the image is wider than 0.5\linewidth then you want ordinary captions ?
    – Munken
    Oct 24, 2012 at 10:20
  • Thanks Munken for the followup. About your question, yes it is correct. Although I will choose myself either to go with sideway or ordinary bottom captions. No need for code to do this. more practical and easier. 0.5\linewidth was just an example. I want the picture on the second page raggedleft to save space just as the picture on the first page should always be raggedright. Now the second one is not aligned to the left, nor the first one if you change the image size.
    – rowman
    Oct 24, 2012 at 14:48
  • @rowman Your last comment is a little hard to understand, but I made the code issue the fitting ragging command based of what king of sidecaption it is. It might require you to recompile sometimes. If you can break the code please post how.
    – Munken
    Oct 24, 2012 at 17:05
  • If you modify the image width in \includegraphics[width=\linewidth] to say '0.8\linewidth', the image is not \raggedright any more (does not align to the left). In your code the horizontal position of caption is always the same and float is placed according to that. However, the float position should always be fixed and caption should be placed according to it. Thanks
    – rowman
    Oct 24, 2012 at 18:36
1

Maybe you chould use sidecaption. I would think that you need two figure enviroments: one ordinary and one for "sideway"-captions. The main idea would be to do something like

\documentclass{memoir}

\setsidecaps{5mm}{.7\textwidth}
\sidecapmargin{outer}

\usepackage{lipsum}

\begin{document}

\chapter{Captions}
\lipsum[2]
\begin{figure}[ht]
\begin{adjustwidth*}{0pt}{.5\textwidth}
\begin{sidecaption}{This figure shows that you should not make figures like this.}
\centering
{\huge FIGURE}
\end{sidecaption}
\end{adjustwidth*}
\end{figure}

\lipsum[1]
\begin{figure}[ht]
\centering
{\huge FIGURE}
\caption{This is a figure}
\end{figure}
\lipsum[1-2]
\begin{figure}[ht]
\begin{adjustwidth*}{0pt}{.5\textwidth}
\begin{sidecaption}{This figure shows that you should not make figures like this.}
\centering
{\huge FIGURE}
\end{sidecaption}
\end{adjustwidth*}
\end{figure}
\lipsum[1]

\end{document}

You can change the margin width etc. to fit with your layout. To do so, take a look at section 10.10 in memman. To define a new float enviroment take a look at the \newfloat command in memman section 10.1. I don't know if it is possible to change the width automatically; if not you have to include this an additional argument. I hope this helps a little :)

1
  • Good thing is that switching the caption sideway to the left or right works. That one is the most challenging.
    – rowman
    Oct 13, 2012 at 5:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .