Here's a method to maximize the height taken up by an image (without overflowing the page). The sidewaysfigure
environment is a bit awkward as it changes the values of \textwidth
and \linewidth
, so the correct maximum height for sideways figures must be calculated outside.
\documentclass{article}
\usepackage[
showframe% debugging, remove in final document
]{geometry}
\usepackage{graphicx}
\usepackage{rotating}
\newlength\maximageheight
\newlength\maximagewidth
\newlength\currentimagewidth
\newsavebox\imagebox
\newlength\maxsidewaysheight
\setlength{\maxsidewaysheight}{\dimexpr\textwidth-\abovecaptionskip-1.5\baselineskip}%
\newcommand{\includeimg}[2][]{%
\setlength\maximageheight{\dimexpr\textheight-\abovecaptionskip-1.5\baselineskip}%
\setlength{\maximagewidth}{\textwidth}%
\innerincludeimg{#1}{#2}%
}
\newcommand{\includesidewaysimg}[2][]{%
\setlength\maximagewidth{\textheight}%
\setlength\maximageheight{\maxsidewaysheight}%
\innerincludeimg{#1}{#2}%
}
\newcommand{\innerincludeimg}[2]{%
\sbox\imagebox{\includegraphics[#1,height=\maximageheight]{#2}}%
\settowidth{\currentimagewidth}{\usebox\imagebox}%
\ifdim\currentimagewidth>\maximagewidth
\includegraphics[#1,width=\maximagewidth]{#2}%
\else
\usebox\imagebox
\fi
}
\begin{document}
\section{this is main section} \label{Section 1}
\subsection{this is subsection} \label{Section 1.1}
There are some paragraphs in here.
\begin{sidewaysfigure}
\centering
\includesidewaysimg{example-image}
\caption{This is some caption}
\label{fig:Figure 1}
\end{sidewaysfigure}
\begin{figure}
\centering
\includeimg{example-image-10x16}
\caption{This is another caption}
\end{figure}
\end{document}
The showframes
option shows the page layout:


This assumes single-lined captions. If you have longer captions, you'll need to adjust the maximum settings. For example:
\setlength{\maxsidewaysheight}{\dimexpr\textwidth-\abovecaptionskip-6\baselineskip}%
Here's an example with a long caption:
\documentclass{article}
\usepackage[
showframe% debugging, remove in final document
]{geometry}
\usepackage{graphicx}
\usepackage{rotating}
\usepackage{lipsum}% dummy text for testing
\newlength\maximageheight
\newlength\maximagewidth
\newlength\currentimagewidth
\newsavebox\imagebox
\newlength\maxsidewaysheight
\setlength{\maxsidewaysheight}{\dimexpr\textwidth-\abovecaptionskip-1.5\baselineskip}%
\newcommand{\includeimg}[2][]{%
\setlength\maximageheight{\dimexpr\textheight-\abovecaptionskip-1.5\baselineskip}%
\setlength{\maximagewidth}{\textwidth}%
\innerincludeimg{#1}{#2}%
}
\newcommand{\includesidewaysimg}[2][]{%
\setlength\maximagewidth{\textheight}%
\setlength\maximageheight{\maxsidewaysheight}%
\innerincludeimg{#1}{#2}%
}
\newcommand{\innerincludeimg}[2]{%
\sbox\imagebox{\includegraphics[#1,height=\maximageheight]{#2}}%
\settowidth{\currentimagewidth}{\usebox\imagebox}%
\ifdim\currentimagewidth>\maximagewidth
\includegraphics[#1,width=\maximagewidth]{#2}%
\else
\usebox\imagebox
\fi
}
\begin{document}
\section{this is main section} \label{Section 1}
\subsection{this is subsection} \label{Section 1.1}
There are some paragraphs in here.
\begin{sidewaysfigure}
\centering
\includesidewaysimg{example-image}
\caption{This is some caption}
\label{fig:Figure 1}
\end{sidewaysfigure}
\begin{sidewaysfigure}
\centering
\includesidewaysimg{example-image}
\caption{\protect\lipsum*[1]}% long dummy caption
\end{sidewaysfigure}
\begin{figure}
\centering
\includeimg{example-image-10x16}
\caption{This is another caption}
\end{figure}
\end{document}
This results in an overfull page:

There are two ways of dealing with this. The simplest is to introduce an extra argument that provides an adjustment:
\documentclass{article}
\usepackage[
showframe% debugging, remove in final document
]{geometry}
\usepackage{graphicx}
\usepackage{rotating}
\usepackage{lipsum}% dummy text for testing
\newlength\maximageheight
\newlength\maximagewidth
\newlength\currentimagewidth
\newsavebox\imagebox
\newlength\maxsidewaysheight
\setlength{\maxsidewaysheight}{\dimexpr\textwidth-\abovecaptionskip}%
\newcommand{\includeimg}[3][]{%
\setlength\maximageheight{\dimexpr\textheight-\abovecaptionskip-#3}%
\setlength{\maximagewidth}{\textwidth}%
\innerincludeimg{#1}{#2}%
}
\newcommand{\includesidewaysimg}[3][]{%
{%
\setlength\maximagewidth{\textheight}%
\setlength\maximageheight{\dimexpr\maxsidewaysheight-#3}%
\innerincludeimg{#1}{#2}%
}%
}
\newcommand{\innerincludeimg}[2]{%
\sbox\imagebox{\includegraphics[#1,height=\maximageheight]{#2}}%
\settowidth{\currentimagewidth}{\usebox\imagebox}%
\ifdim\currentimagewidth>\maximagewidth
\includegraphics[#1,width=\maximagewidth]{#2}%
\else
\usebox\imagebox
\fi
}
\begin{document}
\section{this is main section} \label{Section 1}
\subsection{this is subsection} \label{Section 1.1}
There are some paragraphs in here.
\begin{sidewaysfigure}
\centering
\includesidewaysimg{example-image}{1.5\baselineskip}
\caption{This is some caption}
\label{fig:Figure 1}
\end{sidewaysfigure}
\begin{sidewaysfigure}
\centering
\includesidewaysimg{example-image}{10.5\baselineskip}
\caption{\protect\lipsum*[1]}% long dummy caption
\end{sidewaysfigure}
\begin{figure}
\centering
\includeimg{example-image-10x16}{1.5\baselineskip}
\caption{This is another caption}
\end{figure}
\end{document}
This is a bit of a nuisance if you have a lot of figures. Another method is to supply the caption as the argument and measure it. This requires knowing the format used by \caption
(without actually using \caption
at this point). Since this can vary according to class and packages, I've just provided a simplistic command to simulate the format:¹
\newcommand{\captionformat}[1]{Figure \thefigure: #1}
You may need to adjust this to match the caption style used by your document (such as incorporating font changing commands). Here's the adjusted MWE:
\documentclass{article}
\usepackage[
showframe% debugging, remove in final document
]{geometry}
\usepackage{graphicx}
\usepackage{rotating}
\usepackage{lipsum}% dummy text for testing
\newlength\maximageheight
\newlength\maximagewidth
\newlength\currentimagewidth
\newlength\captionheight
\newsavebox\imagebox
\newlength\maxsidewaysheight
\setlength{\maxsidewaysheight}{\dimexpr\textwidth-\abovecaptionskip
-\baselineskip}%
\newcommand{\captionformat}[1]{Figure \thefigure: #1}% change as appropriate
\makeatletter
\newcommand{\measurecaptionheight}[1]{%
\setbox\@tempboxa\hbox{\parbox{\linewidth}{\captionformat{#1}}}%
\captionheight=\dimexpr\ht\@tempboxa+\dp\@tempboxa
\setbox\@tempboxa\box\voidb@x
}
\makeatother
\newcommand{\includeimg}[3][]{%
{%
\measurecaptionheight{#3}%
\setlength\maximageheight{\dimexpr\textheight-\abovecaptionskip
-\baselineskip-\captionheight}%
\setlength{\maximagewidth}{\textwidth}%
\innerincludeimg{#1}{#2}%
\caption{#3}%
}%
}
\newcommand{\includesidewaysimg}[3][]{%
{%
\measurecaptionheight{#3}%
\setlength\maximageheight{\dimexpr\maxsidewaysheight-\captionheight}%
\setlength\maximagewidth{\textheight}%
\innerincludeimg{#1}{#2}%
\caption{#3}%
}%
}
\newcommand{\innerincludeimg}[2]{%
\sbox\imagebox{\includegraphics[#1,height=\maximageheight]{#2}}%
\settowidth{\currentimagewidth}{\usebox\imagebox}%
\ifdim\currentimagewidth>\maximagewidth
\includegraphics[#1,width=\maximagewidth]{#2}%
\else
\usebox\imagebox
\fi
}
\begin{document}
\section{this is main section} \label{Section 1}
\subsection{this is subsection} \label{Section 1.1}
There are some paragraphs in here.
\begin{sidewaysfigure}
\centering
\includesidewaysimg{example-image}{This is some caption}
\label{fig:Figure 1}
\end{sidewaysfigure}
\begin{sidewaysfigure}
\centering
\includesidewaysimg{example-image}{\protect\lipsum*[1]}% long dummy caption
\end{sidewaysfigure}
\begin{figure}
\centering
\includeimg{example-image-10x16}{This is another caption}
\end{figure}
\end{document}

¹The figure number won't be correct at this point as it doesn't get incremented until the actual \caption
command (for example, in figure 10 this will slightly under-estimate the caption contents), but it's just a rough guide.