Sometimes I don't need to have a specific picture size, but for layout reasons I would like to scale the image so that the rest of the page is filled. How can I achieve that?
[Tex/LaTex] How to define a figure size so that it consumes the rest of a page
graphicsscalingvertical alignment
Related Solutions
Your PNG probably doesn't have the correct resolution information set in its metadata. If you have ImageMagick on your system, you can run identify -verbose overview_pyramid.png
to see the metadata of the image. If the output contains Units: Undefined
and/or a Resolution:
that is incorrect, you need to add the information.
You can do this using the command convert overview_pyramids.png -density 300 -units PixelsPerCentimeter overview_pyramids.png
to set the resolution to 300 pixels per centimetre, for instance; or just convert overview_pyramids.png -units PixelsPerInch overview_pyramids.png
if the value of the "Density" field is correct but the unit is missing.
The following example assumes that the images can be scaled to the half of the text height, then the example measures the height of the captions and calculates the available height for the images.
\documentclass{article}
\usepackage[pass,showframe]{geometry}
\newsavebox\CaptionBoxA
\newsavebox\CaptionBoxB
\newlength\ImgHeight
\begin{document}
\begin{figure}
\vbox to \textheight{%
\centering
\setbox\CaptionBoxA=\vbox{%
\begingroup % color support
\centering
\caption{My first figure}%
\label{fig:first}%
\endgroup
}
\setbox\CaptionBoxB=\vbox{%
\begingroup % color support
\centering
\caption{My second figure}%
\label{fig:second}%
\endgroup
}
\setlength{\ImgHeight}{%
.5\dimexpr\textheight
-\ht\CaptionBoxA-\dp\CaptionBoxA
-\ht\CaptionBoxB-\dp\CaptionBoxB
-\floatsep
\relax
}
\rule{10mm}{\ImgHeight}%
% \includegraphics[height=\ImgHeight,width=\linewidth,keepaspectratio]{...}
\unvbox\CaptionBoxA
\vspace{\floatsep}
\vspace{0pt minus .25\floatsep}% glue for safety
\vspace{0pt plus 1fil}% glue for smaller images
\nointerlineskip % interline skip affects the calculation of \ImgHeight
\rule{10mm}{\ImgHeight}
% \includegraphics[height=\ImgHeight,width=\linewidth,keepaspectratio]{...}
\unvbox\CaptionBoxB
% \vspace{0pt plus 1fil}% glue for smaller images
}
\end{figure}
\end{document}
Result with paper size A5 and rules instead of images:
Remarks:
- The code would have to be put in a macro for practical usage.
- There is room for improvements. For example, the available space is equally distributed among the images. If one image cannot be scaled to the available space because of width limitations, then the other image could grow.
- There is room for extensions (three and more images, ...).
Best Answer
Update 2011/09/16:
I now created a package
tikzpagenodes
(CTAN) which provides a special node for the text area. This simplifies my original answer as follows:However, it is also possible without this special node by using two
tikzpicture
(one to set the start coordinate) as demonstrated by Herbert for PSTricks in Stretching a framebox over the whole page. In TikZ and for a figure this looks like:You can use TikZ for that. In allows you to calculate the amount of the rest of the page. Note that this needs two compiler runs to work. Here my solution for Stretching a framebox over the whole page adapted to include an image instead of a frame-box. Because this isn't 100% trivial for beginners (of LaTeX and/or TikZ) I think this isn't a duplicate.
You might add
width=\textwidth,keepaspectratio
to the\includegraphics
options to ensure that the image isn't scaled wider than the text width.Note that above code doesn't work inside a float because the
tikzpicture
is kind of anchored to the page and the float isn't by definition. The use of a non-floating environment is therefore required. In this specific case the figure should not float anyway. The best solution I can image is to place the caption as part of thetikzpicture
using\captionof{float}{...}
of thecaption
environment. Otherwise thetikpicture
will overlay the caption.The following code places the caption at the very end of the page (i.e. text area of the page) and scales the image so that it goes from the current position to the top of the caption plus
\abovecaptionskip
: