1

I am trying to coax LaTeX to wrangle my figures so that they keep their aspect-ratios but fit the image still on the page in terms of maximum width and height. Pandoc spits out LaTeX beamer code that should do this and that looks correct...except it does not work. Consider:

\documentclass[14pt,aspectratio=169]{beamer}

\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother

\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}

\begin{document}

\begin{frame}{What are my lengths?}
  maxwidth = \the\maxwidth

  maxheight = \the\maxheight

  linewidth = \the\linewidth

  textheight = \the\linewidth

\end{frame}

\begin{frame}{Wide}
\fbox{\includegraphics{wide.png}}
\end{frame}


\begin{frame}{high}
\fbox{\includegraphics{high.png}}
\end{frame}

\end{document}

The really high picture is not very suitable for inclusion in tse, so let me just describe them:

$ file high.png wide.png 
high.png: PNG image data, 1000 x 10000, 8-bit colormap, non-interlaced
wide.png: PNG image data, 10000 x 1000, 8-bit colormap, non-interlaced

The minor problem is that the output tells me that maxwidth and maxheight are 0pt. Unimportant, but how do I print it?

The major problem is that the high figure is not shrunk, but goes below where it should go. This has nothing to do with \maxwidth and \maxheight, because the same result obtains with \setkeys{Gin}{width=8in,height=4in,keepaspectratio}.

How do I coax figures in beamer to keep their aspect ratios but try to stretch as far as they can without overflowing the page in both x and y direction? (Advice, as always, appreciated.)

high image .

5
  • In general, something like \includegraphics[keepaspectratio,width=\linewidth,height=\textheight,]{example-image} might work, Note: \the\maxwidth only makes sense when \maxwidth is a length, not a \def. Commented Dec 20, 2021 at 23:36
  • thx, s. nope, doesn't work in the \includegraphics, either.
    – ivo Welch
    Commented Dec 20, 2021 at 23:45
  • Beamer formats the text before the title, so there is no way to subtract the frame overhead from \textheight or \textwidth. Just use trial and error. Commented Dec 21, 2021 at 0:22
  • thx, j. the frame overhead is not the problem here. the plot extends miles below the slide. (I could even set maxwidth to 11in and maxheight to 7in, as long as I knew that includegraphics will preserve the aspect ratio and hit one or the other of the two, while staying under the other.)
    – ivo Welch
    Commented Dec 21, 2021 at 0:39
  • As John Kormylo said, you have to take into account the space of the title, so is just change \textheight\textheight in maximum height definition by some like .7\textheight.7\textheight (adjust the .7 to your needs)
    – Fran
    Commented Dec 21, 2021 at 7:35

1 Answer 1

1

I would suggest tapping into some of adjustbox's keys: max width and max height. It tests the dimensions sequentially, so it may not end up working as expected in some cases. However, writing your own \includeimage{<img>} command you can check whether the image is landscape/portrait, and specify a max width or max height as needed. Since you're only specifying a single length to be adjusted, it will maintain the aspect ratio.

enter image description here

\documentclass{article}

\usepackage[export]{adjustbox}
\usepackage{xfp}

\newsavebox{\imgbox}

\newcommand{\includeimage}[1]{%
  \savebox{\imgbox}{\includegraphics{#1}}%
  \ifdim\fpeval{\wd\imgbox/\ht\imgbox}pt>1pt
    % Landscape image
    \includegraphics[max height=3em]{#1}%
  \else
    % Portrait image
    \includegraphics[max width=3em]{#1}%
  \fi
}

\setlength{\parindent}{0pt}

\begin{document}

\rule{5em}{1pt}% For reference

\includegraphics[width=5em,height=3em]{example-image}

\includegraphics[width=5em,max height=2em]{example-image} \rule{1pt}{2em}

\includegraphics[height=10em,max width=5em]{example-image}

\rule{5em}{1pt}% For reference

\bigskip

\includeimage{example-image} \rule{1pt}{3em}

\includeimage{example-image-9x16}

\rule{3em}{1pt}


\end{document}

The rules are just for reference in the above output.

2
  • thx, werner. there is magic here---where is the example-image file?? in any case, beamer uses \includegraphics{somefile.png} when one specifies ![]{somefile.png. I tried to override with \let, but this failed for my beamer / \includegraphics. do you happen to know how to do this?
    – ivo Welch
    Commented Dec 24, 2021 at 22:28
  • @ivoWelch: example-image forms part of the mwe package. If you installed a full TeX distribution you can just use it, since mwe would have been installed. I don't understand the other things you speak of...
    – Werner
    Commented Dec 24, 2021 at 22:58

You must log in to answer this question.

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