15

For writing my master thesis I have an image in landscape that needs to be oriented correctly when it is eventually printed in a book.

If the page with the image is on the left side (even page number) the image needs to be rotated 90 degrees, if it is on the right side (odd page number) it needs to be rotated -90 degrees. In both cases the bottom of the image needs to be near the spine of the book.

Is there any way to do this automatically without me having to check each time if the image is oriented correctly? I'm using the report documentclass, if that helps.

2
  • Are your images in figure or other floating environment or directly in the main text flow? Jun 6 '13 at 21:43
  • They are in the figure environment
    – Tim
    Jun 7 '13 at 6:33
14

Let a package do the hard work for you (that's one of the reasons they were written in the first place). The rotating package offers you sidewaysfigure (and sidewaystable):

\documentclass{book}
\usepackage[a6paper]{geometry}% just for the example
\usepackage{graphicx}
\usepackage{rotating}

\begin{document}

.\clearpage% just for the example

\begin{sidewaysfigure}
\centering
\includegraphics[width=8cm]{example-image-a}
\caption{rotated image}
\end{sidewaysfigure}

\begin{sidewaysfigure}
\centering
\includegraphics[width=8cm]{example-image-a}
\caption{rotated image}
\end{sidewaysfigure}

\end{document}

enter image description here

The code needs two runs to determine the right position.

From the package documentation:

If the twoside option has been given to the main document class (either explicitly, or implicitly as in the default for book class), the package will rotate sideways figures according to the page number (this requires at least two passes through LaTeX). If you want the twoside option, but want the figures always in one direction, use the figuresright or figuresleft options to the package.

Now that I re-read the question, it seems that the orientation should be such that bottom of rotated figures should be near the spine; this is somehow against the accepted practice in which top of rotated figures should be near the spine; the rotating package doesn't implement an option for this situation, so some redefinitions are needed:

\documentclass{book}
\usepackage[a6paper]{geometry}% just for the example
\usepackage{graphicx}
\usepackage{rotating}

\makeatletter
\def\end@rotfloat{%
   \end{minipage}\end{lrbox}%
   \stepcounter{r@tfl@t}%
   \rot@label{RF\ther@tfl@t}%
   \rot@pageref{RF\ther@tfl@t}{\R@@page}%
   \edef\@tempa{Adding sideways \@captype\space on page \R@@page\space}
   \rot@mess@toks\expandafter{\@tempa}
   \wd\rot@float@box\z@
   \ht\rot@float@box\z@
   \dp\rot@float@box\z@
   \vbox to \textheight{%
    \setkeys{Grot}{units=360}%
    \if@rot@twoside
    \else
      \let\R@@page\rot@LR
    \fi
    \ifthenelse{\isodd{\R@@page}}{%
      \if@rot@twoside
        \rot@mess@toks\expandafter{\the\rot@mess@toks (right hand page)}%
      \fi
      \@@line{%
        \hskip\rotFPbot
        \rotatebox{-90}{\box\rot@float@box}%
        \hskip\rotFPtop
      }%
      \vfill
    }{%
      \if@rot@twoside
        \rot@mess@toks\expandafter{\the\rot@mess@toks (left hand page)}%
      \fi
      \vfill
      \@@line{%
        \hskip\rotFPtop
        \rotatebox{90}{\box\rot@float@box}%
        \hskip\rotFPbot
      }%
    }%
    \rot@message{\the\rot@mess@toks}
  }%
  \end@float
}
\let\endsidewaysfigure\end@rotfloat
\let\endsidewaystable\end@rotfloat
\makeatother

\begin{document}

.\clearpage% just for the example

\begin{sidewaysfigure}
\centering
\includegraphics[width=8cm]{example-image-a}
\caption{rotated image}
\end{sidewaysfigure}

\begin{sidewaysfigure}
\centering
\includegraphics[width=8cm]{example-image-a}
\caption{rotated image}
\end{sidewaysfigure}

\end{document} 

enter image description here

3
  • You were right in assuming that the bottom of the image must be near the spine. Perhaps I mixed up the two rotations in my question (I fixed that). Your answer works near perfectly, I didn't know about sidewaysimage. However, my image appears always with the top near the spine after two runs, so I had to rotate the image first 180 degrees (bottom up, which seems weird).
    – Tim
    Jun 7 '13 at 6:32
  • I just reread your answer and took a better look at your answer and the pictures accompanying in. It seems like the best practice is to have the bottom of the image near the spine, I didn't know that as well and seems strange to me; I always assumed the top needed to be near the spine which is why was my question seemed strange to you.
    – Tim
    Jun 7 '13 at 6:38
  • @Tim common practice is to have the top of material near the spine; when I wrote the paragraph about this I wrote it backwards; now I've corrected this in my updated answer. Jun 7 '13 at 13:02
5

You can extend the method of

Formatting floats differently based on placement

to save 8 versions of the image for top bottom here and float, on odd and even pages, and choose which version to use as the float is added to the area.

enter image description here

\documentclass{article}
\usepackage{color}
\usepackage{graphicx}

\def\a{One two three four five six. }
\def\b{Apple, Pear, Banana, Orange. }
\def\c{\a\a\b\par\b\b\b\a\b\par\b\b\b\par}

\makeatletter

\def\floatswitch#1#2#3#4#5#6#7#8{%
  \def\@elt##1{\global\value{##1}\the\value{##1}\relax}%
  \edef\FS@ckpt{\cl@@ckpt}%
  \let\@elt\relax
\hbox to 3sp{%
\vbox{{\FS@ckpt#1\par}}%
\vbox{{\FS@ckpt#2\par}}%
\vbox{{\FS@ckpt#3\par}}%
\vbox{{\FS@ckpt#4\par}}%
\vbox{{\FS@ckpt#5\par}}%
\vbox{{\FS@ckpt#6\par}}%
\vbox{{\FS@ckpt#7\par}}%
\vbox{{\FS@ckpt#8\par}}%
\hss}\aftergroup\break}


\tracingonline2
\showboxdepth1
\showboxbreadth25
\errorcontextlines\maxdimen

\def\FS@checkswitch#1{%
\ifdim\wd#1=3sp %
\setbox\z@\box#1%
\begingroup
\vbadness\maxdimen
\setbox\z@\vsplit\z@ to \textheight
\setbox\z@\vbox{\unvbox\z@\global\setbox\@ne\lastbox}%
\setbox\z@\vbox{\unvbox\@ne\unskip\global\setbox\@ne\lastbox}
\setbox\z@\hbox{\unhbox\@ne\unskip
\FS@junk
\global\setbox\@ne\lastbox}%
\endgroup
\else
\global\setbox\@ne\box#1%
\fi}

\def\@comflelt#1{%
\FS@checkswitch#1%
\setbox\@tempboxa
      \vbox{\unvbox\@tempboxa\box\@ne\vskip\floatsep}}

\let\saved@cflt\@cflt
\def\@cflt{%
\def\FS@junk{%
\setbox\z@\lastbox\setbox\z@\lastbox\setbox\z@\lastbox
\setbox\z@\lastbox\setbox\z@\lastbox\setbox\z@\lastbox
\ifodd\c@page\setbox\z@\lastbox\fi
}%
\saved@cflt}

\let\FS@junk\relax
\let\saved@cflb\@cflb
\def\@cflb{%
\def\FS@junk{%
\setbox\z@\lastbox\setbox\z@\lastbox
\setbox\z@\lastbox\setbox\z@\lastbox
\ifodd\c@page\setbox\z@\lastbox\fi
}%
\saved@cflb}


\def\foo#1\box\@currbox#2!!{%
\def\@addtocurcol{%
#1%
\def\FS@junk{%
\ifodd\c@page\setbox\z@\lastbox\fi
}
\FS@checkswitch\@currbox
\box\@ne
#2}}%
\expandafter\foo\@addtocurcol!!

\def\@wtryfc #1{%
  \global\setbox\@outputbox\vbox{%
    \unvbox\@outputbox
    \vskip\@fpsep
\def\FS@junk{%
\setbox\z@\lastbox
\setbox\z@\lastbox
\ifodd\c@page\setbox\z@\lastbox\fi
}%
\FS@checkswitch#1%
\box\@ne}}

\makeatother
\begin{document}

\c

\def\fig#1{%
\begin{figure}#1
\floatswitch
{%
\color{red}\centering\rotatebox{90}{\fbox{$\frac{123456}{\frac{1}{2}}$}}
\caption{odd t floats are red}}
{%
\color{red}\centering\rotatebox{-90}{\fbox{$\frac{123456}{\frac{1}{2}}$}}
\caption{even t floats are red}}
%
{%
\color{blue}\centering\rotatebox{90}{\fbox{$\frac{123456}{\frac{1}{2}}$}}
\caption{odd b floats are blue}}
{%
\color{blue}\centering\rotatebox{-90}{\fbox{$\frac{123456}{\frac{1}{2}}$}}
\caption{even b floats are blue}}
%
{%
\color{green}\centering\rotatebox{90}{\fbox{$\frac{123456}{\frac{1}{2}}$}}
\caption{odd p floats are green}}
{%
\color{green}\centering\rotatebox{-90}{\fbox{$\frac{123456}{\frac{1}{2}}$}}
\caption{even p floats are green}}
%
{%
\color{yellow}\centering\rotatebox{90}{\fbox{$\frac{123456}{\frac{1}{2}}$}}
\caption{odd h floats are yellow!}}
{%
\color{yellow}\centering\rotatebox{-90}{\fbox{$\frac{123456}{\frac{1}{2}}$}}
\caption{even h floats are yellow??}}
\end{figure}%
}
%
\clearpage

\fig{}
\c
\fig{[!h]}
\fig{[b]}
\c
\fig{[p]}
\begin{figure}
\centering
\fbox{NO SWITCH}
\caption{no switch}
\end{figure}
\c
\clearpage
\mbox{zzz}
\clearpage

\fig{}
\c
\fig{[!h]}
\fig{[b]}
\c
\fig{[p]}
\begin{figure}
\centering
\fbox{NO SWITCH}
\caption{no switch}
\end{figure}
\c

\end{document}
1

Because there was nothing about a caption, it should be enough (package graphicx must be loaded):

\def\mygr#1{\ifodd\value{page} \includegraphics[angle=-90]{#1} 
\else \includegraphics[angle=90]{#1} \fi}

with an obvious usage: \mygr{nameofpicture}.

5
  • 4
    That doesn't work as it tests the page on which the float is saved not on the page to which it floats. Jun 6 '13 at 21:36
  • @DavidCarlisle In the question there was nothing about floats, hence suggested simple solution. Jun 6 '13 at 21:38
  • testing for odd and even pages is trickier than it first appears, as detailed in @DavidCarlisle's answer to Equation numbers in outer margins on a two-sided document
    – cmhughes
    Jun 6 '13 at 21:39
  • True it doesn't mention floats but I bet it is about them anyway:-) I'll ping the OP and ask:-) Jun 6 '13 at 21:40
  • Even without floats it can still be out depending on where the output routine breaks a page. Jun 6 '13 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.