Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
\usepackage[hidelinks]{hyperref}
\begin{document}
\picturepage{some_portrait_picture}{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{some_landscape_picture}{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:

Example with one landscape and one portrait picture ... literally.

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/

share|improve this question
    
Related: tex.stackexchange.com/questions/13307/… –  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
add comment

1 Answer 1

up vote 3 down vote accepted

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]
\RequirePackage{afterpage}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% \picturepage[ToC caption]{file}{caption}{label} %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\picturepage}[4][\empty]{%
%\afterpage{
\clearpage
\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}}%
  }%
  \addtocounter{figure}{-1}%
}%
\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}
\addtolength{\vmargin}{1in}
\addtolength{\vmargin}{\headheight}
\addtolength{\vmargin}{\headsep}
\addtolength{\vmargin}{\topskip}
\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%
  }%
  \addtocounter{figure}{-1}%
}%
\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}%
}%
\addtocounter{figure}{1}%
\clearpage%
}%}
share|improve this answer
    
Thanks for sharing the code; it still needs a bit of re-factoring IMHO. Since you compute the image dimensions you should let the code decide if you require landscape or portrait page from the aspect ratio. Simplifies the interface. –  Yiannis Lazarides Nov 26 '12 at 17:09
    
@YiannisLazarides It definitely needs refactoring. The portrait/landscape decision, however, is actually done automatically. The portrait and landscape strings in the MWE are just the image file names for clarity. It seems my clarity caused a bit of confusion ;) Sorry for that. –  Christian Nov 26 '12 at 17:15
    
Sorry, you right the strings confused me. –  Yiannis Lazarides Nov 26 '12 at 21:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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