3

I want figures on the left pages, text on the right ones and numbering on the right pages only, so I followed this: Figures on left pages, text on right ones with class book, numbering only on right pages

However some of my figures must be sideways, so that I'd like to use a sidewaysfigure environment, and some other need subfigures: how can I use floats for my figures?

1 Answer 1

1

This is a modification of this solution to "Figures on left pages, text on right ones with class book, numbering only on right pages"; see below for the complete code.

Replace the definition of \addfig by the two lines

\newcommand\addfig[1]%
  {\g@addto@macro\sos@figures
     {\vbox{\def\@captype{figure}\centering#1}\vfill}%
  }
\newcommand\addtab[1]%
  {\g@addto@macro\sos@figures
     {\vbox{\def\@captype{table}\centering#1}\vfill}%
  }

Now you don't need to load the package caption anymore. For sideways figures, load the package rotating, for sub-figures the package subfigure. In the sample code referenced above this means to replace \usepackage{booktabs,caption} by

\usepackage{booktabs,rotating,subfigure}

To add figures or tables, use

\addfig
  {...code of figure...
   \caption{...caption of figure...}%
   \label{...label of figure ...}%
  }

or

\addtab
  {...code of table...
   \caption{...caption of table...}%
   \label{...label of table ...}%
  }

For sub-figures, use

\addfig
  {\subfigure[...caption of subfigure 1]{...code of subfigure 1...}%
   \subfigure[...caption of subfigure 2]{...code of subfigure 2...}%
   \caption{...main caption...}%
  }

(and likewise \addtab for tables).

For sideways figures without sideways caption use

\addfig
  {\begin{sideways}
   ...code of sideways figure...
   \end{sideways}%
   \caption{...caption of figure...}%
  }

For sideways figures with sideways caption use

\addfig
  {\begin{sideways}
     \begin{minipage}{...width of sideways box...}
     ...code of sideways figure...
     \caption{...sideways caption of sideways figure...}%
     \end{minipage}%
   \end{sideways}%
  }

enter image description here

For the sake of self-containedness, here is the complete modified code.

\documentclass{book}
\usepackage{atbegshi}
\makeatletter
\newlength{\sos@top}
\newlength{\sos@right}
\newcounter{sos@pages}
\newif\ifSOS
\renewcommand{\thepage}{\the\numexpr\value{page}-\value{sos@pages}\relax}
\newcommand\addfig[1]{\g@addto@macro\sos@figures{\vbox{\def\@captype{figure}\centering#1}\vfill}}
\newcommand\addtab[1]{\g@addto@macro\sos@figures{\vbox{\def\@captype{table}\centering#1}\vfill}}
\newcommand{\sos@reset@figures}
  {\gdef\sos@figures{\sos@reset@figures\vfill}}
\sos@reset@figures
\newcommand{\sos@shipout@figures}
  {%
    \begingroup
      \stepcounter{page}%
      \stepcounter{sos@pages}%
      \let\protect\relax
      \setbox\z@\vbox to\vsize{\sos@figures}%
      \let\protect\string
      \AtBeginShipoutOriginalShipout\vbox
        {\vbox to\sos@top{}\moveright\sos@right\box\z@}%
    \endgroup
  }
\AtBeginShipout{%
  \ifSOS\ifodd\c@page
    \begingroup
      \let\protect\string
      \AtBeginShipoutOriginalShipout\box\AtBeginShipoutBox
      \global\AtBegShi@Discardedtrue
      \sos@shipout@figures
    \endgroup
  \fi\fi
}%
\newcommand{\SOSshipout}{\clearpage\sos@shipout@figures}
\renewcommand{\SOStrue}{\clearpage\global\let\ifSOS\iftrue}
\renewcommand{\SOSfalse}{\clearpage\global\let\ifSOS\iffalse}
\setlength{\sos@top}{2cm}
\setlength{\sos@right}{2cm}
\makeatother

% Test example
\usepackage{booktabs,rotating,subfigure}
\usepackage{lipsum}
\usepackage{hyperref}
\begin{document}

\title{Hello world}
\author{Bruno Le Floch}
\maketitle

\tableofcontents
\listoffigures
\listoftables

\part{Abc}

\SOStrue
\chapter{Hello}

\addtab{\begin{tabular}{p{5cm}p{5cm}}
    \toprule
    Abc def & ghijk lmno pq \\
    \midrule
    \lipsum[1] & \lipsum[2] \\
    \bottomrule
  \end{tabular}
  \caption{\label{tab:atable}A table}}

\addfig{%
  \rule{8cm}{3cm}%
  \caption{A figure}}

\lipsum[1-10]

\addfig{\begin{sideways}\includegraphics[width=5cm]{example-image}\end{sideways}\caption{A sideways figure}}
\addfig{\begin{sideways}\begin{minipage}{5cm}\includegraphics[width=\textwidth]{example-image}\caption{A sideways figure with sideways caption.}\end{minipage}\end{sideways}}
\addfig{%
\subfigure[Image A]{\includegraphics[width=3cm]{example-image-a}}
\subfigure[Image B]{\includegraphics[width=3cm]{example-image-b}}
\caption{Two sub-figures side by side.}}

\chapter{Bye}

\makeatletter
\renewcommand{\lips@par}{ } % now \lipsum[1-10] makes one big par
\makeatother

\addfig{\rule{8cm}{3cm}\caption{That should be figure 5.}}
\addfig{\rule{1cm}{3cm}\caption{Perhaps the sixth}}

\lipsum[1-10]

\addfig{\rule{8cm}{3cm}\caption{Yet another one}}
\addfig{}

\SOSfalse

\chapter{Back to normal}

\addfig{\rule{8cm}{3cm}\caption{That figure won't be lost.}}

\lipsum[11-15]

\addfig{\rule{4cm}{5cm}\caption{Nor will that one.}}

\lipsum[16-20]

\lipsum[21-30]

See Table~\ref{tab:atable}.

\SOSshipout
\SOStrue

\chapter{Figures, again}

\addtab{\rule{5cm}{2cm}\caption{Let's pretend it's a table}}

\lipsum[21-25]

\addtab{\rule{5cm}{2cm}\caption{Let's pretend it's a table}}

\lipsum[26-30]

\addtab{\rule{4cm}{1cm}\caption{Last table}}

\end{document}
1
  • subfigure is obsolete and ought not be used. Use subfig or subcaption instead.
    – cfr
    May 6, 2017 at 2:46

You must log in to answer this question.

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