# How to take figure width=\linewidth OR height=\pageheight?

I want to include figures such that either width or height is used to adjust the document on the page. There are about 5\% of images where the height is dominating such that width=\linewidth causes overful. In such a case, I would like to adjust by height=\pageheight. Pseudocode

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\begin{figure}
% Make the decision here primarily by width; if overful, then height.
\includegraphics[width=\linewidth OR height=\pageheight]{1.jpg}
\end{figure}
\end{document}


TeXLive: 2016
OS: Debian 8.5

• MWE?____________ – user36296 Nov 29 '16 at 18:18
• @samcarter I added a pseudocode because I do not know if this possible what I proposed. – Léo Léopold Hertz 준영 Nov 29 '16 at 18:19
• You're talking about latex. Everything is possible (maybe with an infinite amount of work, but possible). How dare you doubt latex? – user36296 Nov 29 '16 at 18:26

Just let the graphicx package have its way:

\documentclass{article}
\usepackage{graphicx}

\usepackage[showframe]{geometry} %just to visualise the boarder

\begin{document}

\noindent\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{example-image-golden}

\newpage

\noindent\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{example-image-golden-upright}

\end{document}