0

I want to create a page with two images and some text. The images will get recreated from time to time, which possibly results in different image-sizes. I do not want to manually set the maximum height and width of these images in my Latex document each time they get recreated - So is there a way to force these images to appear in the maximum possible size (So both images are 150% it's original size or only 90% it's original size)

I tried to place them in one figure environment and set they're width and height to width=\textwidth,height=\textheight and set the keepaspectratio attribute.

\newpage
\begin{figure}[ht]
\textbf{Title of the first image} \vspace*{10pt} \\ 
\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{tmp/first.png} 
\textbf{Title of the second imate} \vspace*{10pt} \\ 
\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{tmp/second.png}
\end{figure}

This resulted it to be forced on one page but the second image was to big and cut off at the bottom

Is there a way to automatically let latex handle the sizing of the images?

5
  • If you are keeping aspect ratio then for any situation you should only need to define one dimension since the other is relative, however as you are possibly noticing that gives the system some freedom to go against your desires. Say I have two landscape images then setting width should be enough to keep both on the one page however if I then load two portrait then it will likely break thus it is better if I use height as the default and accept that with fixed ratio the landscape ones may not be full width. This will be natural issue when inserting unframed fixed aspect images of variable size.
    – user170109
    Commented Apr 17, 2019 at 17:34
  • 1
    unrelated but \vspace*{10pt} \\ is a rather strange construct adding space after the end of the line, better to use \\[10pt] Commented Apr 17, 2019 at 17:43
  • Are the images' shapes square or rectangular? If they're rectangles, are they "wide" ("landscape") or tall ("portrait")?
    – Mico
    Commented Apr 17, 2019 at 17:59
  • You could possibly adapt from third (0.33) to half (0.5) this answer tex.stackexchange.com/questions/406566/…
    – user170109
    Commented Apr 17, 2019 at 18:08
  • There are limits on how big a [ht] figure can be. Only [p] figures can take an entire page. Commented Apr 18, 2019 at 5:42

1 Answer 1

1

I made both images as wide a possible, then shrunk them if too tall. The tricky bit was deciding how much to shrink each image.

\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{showframe,duckuments}% MWE only
\begin{document}
\begin{figure}[p]
\centering
\sbox0{\includegraphics[width=\textwidth]{example-image-duck}}
\sbox1{\includegraphics[width=\textwidth]{example-image-duck}}
\dimen0=\dimexpr 2\baselineskip+20pt\relax% height of text
\ifdim\textheight>\dimexpr \dimen0+\ht0+\ht1\relax% max width fits
  \textbf{Title of the first image}\\[10pt]
  \usebox0\par
  \textbf{Title of the second image}\\[10pt]
  \usebox1
\else
  \dimen1=\dimexpr \dimen0+\ht0+\ht1-\textheight\relax% excess height
  \ifdim\ht0>\dimexpr\ht1+\dimen1\relax% sbox0 is HUGE!
    \textbf{Title of the first image}\\[10pt]
    \resizebox{!}{\dimexpr \ht0-\dimen1}{\usebox0}\par
    \textbf{Title of the second image}\\[10pt]
    \usebox1
  \else
    \ifdim\ht1>\dimexpr\ht0+\dimen1\relax% sbox1 is HUGE!
      \textbf{Title of the first image}\\[10pt]
      \usebox0\par
      \textbf{Title of the second image}\\[10pt]
      \resizebox{!}{\dimexpr \ht1-\dimen1}{\usebox1}
    \else% scale images proportionally
      \dimen1=\dimexpr \textheight-\dimen0\relax
      \dimen2=\dimexpr \ht0+\ht1\relax
      \textbf{Title of the first image}\\[10pt]
      \resizebox{!}{\dimexpr \dimen1*\ht0/\dimen2}{\usebox0}\par
      \textbf{Title of the second image}\\[10pt]
      \resizebox{!}{\dimexpr \dimen1*\ht1/\dimen2}{\usebox1}
    \fi
  \fi
\fi
\end{figure}
\end{document}

This uses image files with different aspect ratios.

full page

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .