# Dedicated pages for figures and automatic scaling and use of pdflscape

There are some books where you have a couple of pages that contain only a picture with no footer, header or anything like that. I wanted to have something similar although I'd like to keep a uniform margin, the aspect ratio of the image and a caption. I also wanted the page to rotate using pdflscape according to the dimensions of the image.

This is how it might be used in a more or less MWE:

documentclass[a5paper]{article}
\usepackage{picturepage}
\begin{document}
\picturepage{portrait}{This is a portrait picture. \href{http://www.flickr.com/photos/thomasleuthard/}{Thomas Leuthard} took it in France and released it under the \href{http://creativecommons.org/licenses/by/2.0/}{Creative Commons attribution licence} on Flickr.}{fig:portrait}
\picturepage{landscape}{This is a landscape picture. It was released under the \href{http://creativecommons.org/licenses/by/2.0/}{Creative Commons attribution licence} by someone calling himself \href{http://www.flickr.com/photos/80901381@N04/}{A Guy Taking Pictures}'' on Flickr.}{fig:landscape}
\end{document}


And this is how this might look:

I'm going to publish my take on this problem in an answer. Everyone is welcome to tinker with my code or post your own answers. It shouldn't be that hard to come up with something prettier than what I did.

These are the pictures I used. If you don't want to go with the boring mwe stuff, this might save those some time who want to get some Create Commons pictures to play around with: http://www.flickr.com/photos/80901381@N04/7758832526/ and http://www.flickr.com/photos/thomasleuthard/6106078656/

-
 – lockstep Nov 26 '12 at 12:21 But not the same … I'm going to accept my own answer until someone comes up with a better solution. I hope someone will since I'd like to make this a package but I actually find my code a bit too ugly and I'm sure some of the gurus around here could tell me how to improve it. – Christian Dec 5 '12 at 15:59

So here is my take. The TOC caption optional argument is probably not ideal where it is now but should instead come before or after the cation argument. Also caption and label could be optional arguments, too. And then the code could be considerably cleaner I guess. The distinctions of cases blow this code up more than should be necessary but I was unable to use the same approach for landscape and portrait somehow.

% Provides full page picture command
% \picturepage[TOC caption]{picture}{caption}{label}

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{picturepage}[2012/04/16 v0.1 Picture Page]

\newlength{\maxpicturewidth}
\newlength{\maxpictureheight}
\newlength{\maxfigureheight}
\newlength{\pictureheight}
\newlength{\picturewidth}
\newlength{\totalfigureheight}
\newlength{\captionheight}
\newlength{\originalpicturewidth}
\newlength{\originalpictureheight}
\newlength{\picturehoffset}
\newlength{\picturevoffset}
\newlength{\vmargin}
\newlength{\subpictureskip}
\setlength{\subpictureskip}{0pt}
\newsavebox{\captionbox}
\newcounter{recalccounter}

\RequirePackage{pdflscape}
%\RequirePackage{lscape}
\RequirePackage{graphicx}
\RequirePackage{ifthen}
\RequirePackage{calc}
\RequirePackage[absolute]{textpos}
\RequirePackage{geometry}[2010/09/12]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% \picturepage[ToC caption]{file}{caption}{label} %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\picturepage}[4][\empty]{%
\clearpage% TODO: in a perfect world, we could carry over floats past the picturepage
% using the [H] placement from the float package, \suppressfloats[htpb] and \newpage didn't work
\thispagestyle{empty}%
\settowidth{\originalpicturewidth}{\includegraphics{#2}}%
\settoheight{\originalpictureheight}{\includegraphics{#2}}%
% check if we have a landscape picture
\ifthenelse{\lengthtest{\originalpicturewidth > \originalpictureheight}}{%
%%% landscape %%%
\newgeometry{textheight=\paperheight,textwidth=\paperwidth}%
\begin{landscape}%
\begin{figure}%
% set max picture dimensions
\setlength{\maxpictureheight}{.9\paperwidth}% TODO: make width/height depend
\setlength{\maxpicturewidth}{.9\paperheight}% on orientation one day
\setlength{\maxfigureheight}{\maxpictureheight}%
% calculate picture and caption dimensions including caption until it fits
\setlength{\captionheight}{\baselineskip}%
\setcounter{recalccounter}{0}%
\setlength{\totalfigureheight}{1.01\maxfigureheight+1pt}% force 1st iteration
\whiledo{\lengthtest{\totalfigureheight>1.01\maxfigureheight} % give some leeway
\AND \value{recalccounter}<10}{% if we don't converge in 10 iterations, give up
\ifthenelse{\value{recalccounter}>0}{%
\typeout{picture too high, recalculating (iteration \arabic{recalccounter})}%
}{}%
% calc initial picture dimensions
\setlength{\maxpictureheight}{\maxfigureheight-\captionheight-\subpictureskip}%
\settoheight{\pictureheight}{\includegraphics[width=\maxpicturewidth,%
height=\maxpictureheight, keepaspectratio=true, draft]{#2}}%
\settowidth{\picturewidth}{\includegraphics[width=\maxpicturewidth,%
height=\maxpictureheight, keepaspectratio=true, draft]{#2}}%
% measure caption height with initial picture dimensions
\savebox{\captionbox}{\parbox{\picturewidth}{%
\ifthenelse%
{\equal{#1}{\empty}}%
{\caption{#3}}%
{\caption[#1]{#3}}%
}%
}%
\settoheight{\captionheight}{\usebox{\captionbox}}%
% measured magic factor, total weirdness, should *so* be 1.0!
\setlength{\captionheight}{1.9\captionheight}%
% calc intial figure height including caption
\setlength{\totalfigureheight}{\pictureheight+\captionheight+\subpictureskip}%
\stepcounter{recalccounter}%
}%
\centering%
\includegraphics[width=\maxpicturewidth,%
height=\maxpictureheight, keepaspectratio=true]{#2}\vspace{\subpictureskip}

\usebox{\captionbox}\label{#4}%
\end{figure}%
\end{landscape}%
\restoregeometry
}{%%% portrait %%%
\setlength{\vmargin}{\topmargin}
\begin{figure}%
% set max picture dimensions
\setlength{\maxpictureheight}{.9\paperheight}%
\setlength{\maxpicturewidth}{.9\paperwidth}%
\setlength{\maxfigureheight}{\maxpictureheight}%
% calculate picture and caption dimensions including caption until it fits
\setlength{\captionheight}{\baselineskip}%
\setcounter{recalccounter}{0}%
\setlength{\totalfigureheight}{1.01\maxfigureheight+1pt}% force 1st iteration
\whiledo{\lengthtest{\totalfigureheight>1.01\maxfigureheight} % give some leeway
\AND \value{recalccounter}<10}{% if we don't converge in 10 iterations, give up
\ifthenelse{\value{recalccounter}>0}{%
\typeout{picture too high, recalculating (iteration \arabic{recalccounter})}%
}{}%
% calc initial picture dimensions
\setlength{\maxpictureheight}{\maxfigureheight-\captionheight}%
\settoheight{\pictureheight}{\includegraphics[width=\maxpicturewidth,%
height=\maxpictureheight, keepaspectratio=true, draft]{#2}}%
\settowidth{\picturewidth}{\includegraphics[width=\maxpicturewidth,%
height=\maxpictureheight, keepaspectratio=true, draft]{#2}}%
% measure caption height with initial picture dimensions
\savebox{\captionbox}{\parbox{\picturewidth}{%
\begingroup%
\ifthenelse%
{\equal{#1}{\empty}}%
{\caption{#3}}%
{\caption[#1]{#3}}%
\endgroup%
}%
}%
\settoheight{\captionheight}{\usebox{\captionbox}}%
% measured magic factor, total weirdness, should *so* be 1.0!
\setlength{\captionheight}{1.9\captionheight}%
% calc intial figure height including caption
\setlength{\totalfigureheight}{\pictureheight+\captionheight}%
\stepcounter{recalccounter}%
}%
\setlength{\picturevoffset}{\paperheight-\totalfigureheight}%
\setlength{\picturevoffset}{.5\picturevoffset}%
\setlength{\picturehoffset}{\paperwidth-\picturewidth}%
\setlength{\picturehoffset}{.5\picturehoffset}%
\begin{textblock*}{\picturewidth}(\picturehoffset,\picturevoffset)
\centering
\includegraphics[width=\maxpicturewidth,%
height=\maxpictureheight, keepaspectratio=true]{#2}\vspace{\subpictureskip}

\usebox{\captionbox}\label{#4}%
\end{textblock*}
\null\clearpage
\end{figure}%
}%