# The problem of empty page when starting new chapter with an image on first page

This is a tough one. I have many cases where I start new chapter, and the first page of the new chapter is a pdf page, which is loaded using \includegraphics (I have lots of documents, I scan, then load into Latex like this).

If I do not adjust the width of the pdf/image page correctly, I end up with an empty page at start of the chapter. So I spend a lot of time, manually adjusting the image width to make it small enough to get rid of the empty page before the chapter.

Not only that, if I change from letter size to legal size, all these changes will not have been needed.

I'd like to ask if there is an automated way, to find what width to use for the pdf page or the image to load, so that no empty page is generated.

The case is always on first page on new chapter. Here is a MWE of what I mean.

\documentclass[11pt]{report}%
\usepackage[demo]{graphicx}
\usepackage[letterpaper]{geometry}
%\usepackage[legalpaper]{geometry}
\begin{document}
Some text here
Some text here

Some text here
Some text here

\chapter{one}
\includegraphics[width=0.9\textwidth,height=.9\textwidth]{whatever}

\includegraphics[width=0.9\textwidth,height=.9\textwidth]{whatever}
\end{document}


Here is the result. The second page is empty

Changing height=.9 to height=.85 fixes it

....
\chapter{one}
\includegraphics[width=0.9\textwidth,height=.85\textwidth]{whatever}

\includegraphics[width=0.9\textwidth,height=.9\textwidth]{whatever}
....


Here is the result

I understand the issue. And why Latex did this. The image was a little to large, so it tried it on a new page, and gave up, ending with empty wasted page.

Note: in the above MWE, I added height= just to show the problem. In my actual code, only width= is used. But I did not know how to show the problem here without using height=.

So my question is: Is there a way to tell Latex to adjust the width of the image being loaded automatically to correct width such that no empty page is generated? Or a smarter way to do this, without all this manual adjustments.

What I do now, is run Latex, spend lots of time adjusting width values of images to remove empty pages before them. Sometimes I also change geometry and have to do this all over again. It will be nice to automate this.

Using TL 2015.

Macro \getremaining computes the vertical space left on the page, putting the answer into length \vremaining.

Remember to run it twice.

\documentclass[11pt]{report}%
\usepackage[demo]{graphicx}
\usepackage[letterpaper]{geometry}
%\usepackage[legalpaper]{geometry}
\usepackage{tikzpagenodes}

\newlength{\vremaining}

\newcommand{\getremaining}%
{\begin{tikzpicture}[remember picture,overlay]
\pgfextracty{\vremaining}{\pgfpointanchor{current page text area}{south}}%
\global\vremaining=-\vremaining
\end{tikzpicture}%
\vspace*{-\baselineskip}\newline}% place above image

\begin{document}
Some text here
Some text here

Some text here
Some text here

\chapter{one}
\getremaining
\includegraphics[width=0.9\textwidth,height=\vremaining]{whatever}

\includegraphics[width=0.9\textwidth,height=.9\textwidth]{whatever}
\end{document}