2

With an image in its own section of a document, like:

\subsubsection{Title}

\begin{figure}
\centering
\includegraphics[width=0.7\textwidth]{somefile.pdf}
\end{figure}

How can you set it to make the image as big as possible within the page constraints, but not so large that the image gets put on a different page to the title?

OJW
  • 263
  • Possible duplicate of http://tex.stackexchange.com/questions/32886/how-to-fit-a-large-figure-to-page – OJW Sep 20 '13 at 11:54

2 Answers2

2

The following provides \stretchtofitimage which stretches the image vertically to fit on the "remainder of the page" by setting the height key-value using some calculations:

enter image description here

\documentclass{article}
\usepackage{geometry,graphicx,zref-savepos}% http://ctan.org/pkg/{geometry,graphicx,zref}
\makeatletter
% \zsaveposx is defined since 2011/12/05 v2.23 of zref-savepos
\@ifundefined{zsaveposy}{\let\zsaveposy\zsavepos}{}
\newcounter{stretchtofit}
\newcommand{\stretchtofitimage}[2][]{%
  \stepcounter{stretchtofit}% Next image
  \zsaveposy{fig:top\thestretchtofit}% Save vertical position on page
  \includegraphics[height=\dimexpr\zposy{fig:top\thestretchtofit}sp-\Gm@bmargin-1pt,#1]{#2}}
\begin{document}
\section{A section}
\subsection{A subsection}
Here is some text.

\subsubsection{A subsubsection}
{\centering\stretchtofitimage[width=.6\linewidth]{example-image-a}\par}

Here is some more text.
\end{document}

The calculation of the height uses zref's savepos module to store the vertical height (in scaled points) from the bottom of the page. Since this vertical height is measured from the bottom up, we subtract the bottom margin (provided by geometry's \Gm@bmargin) as well as a 1pt smidgen. There is some stretch allowed below sections, which this 1pt removal accommodates for.

Note that the above assumes you are not using a \caption - it wasn't included in your code snippet, so this may be a fair assumption.

The image above includes a showframe, which is also supported by geometry.


If you want to have a single-page image with \subsubsection title, then you can get around without zref:

enter image description here

\documentclass{article}
\usepackage{graphicx}% http://ctan.org/pkg/graphicx
\begin{document}
\section{A section}
\subsection{A subsection}
Here is some text.

\subsubsection{A subsubsection\strut}
{\centering
\includegraphics[width=\textwidth,height=\dimexpr\textheight-1.7ex-\baselineskip-1pt]{example-image-a}\par}

Here is some more text.
\end{document}

The measurements for calculating the height is based on the fact that a \subsubsection heading is set using \normalfont\normalsize\bfseries in the standard document classes (which has a regular \baselineskip) and a post-title skip of 1.5ex \@plus .2ex. Again, the 1pt difference is for good measure.

The \strut inside \subsubsection ensures proper usage of the \baselineskip for the title. You may not need it in your actual title.

Werner
  • 603,163
  • Nice answer, but I think the idea was to maintain the aspect ratio as well (although the original question is admittedly ambiguous). – Sean Allred Sep 20 '13 at 00:12
  • 1
    @SeanAllred: Yes, the intent of adding a width key is merely to show that the user can introduce other options. – Werner Sep 20 '13 at 01:35
0

A slightly "hack" way of doing it, but the program generating the LaTeX can just be told the expected page size, and specify either 20cm high or 15cm wide depending on aspect ratio.

OJW
  • 263