43

I have a script which automatically creates a beamer presentation with several .ps images, each one inserted in a distinct frame. Those images consist on graphs built using GraphViz. The script is basically iterating on this code snippet:

\begin{frame}{Image 1}

    \begin{figure}
        \includegraphics[width=1\textwidth]{image-1.ps}
    \end{figure}

\end{frame}

I have some images with large widths, like this one (reduced the size and quality for best viewing):

An image with large width

So the above code works like a charm. But I do have small images as well, like this one:

A small image

If I leave the \includegraphics command with no adjustments, my images will become way bigger than the frame itself. If I put width=1\textwidth, it will work fine with large width images, but not with small ones.

Is there a way to find the best figure size adjustment when using \includegraphics, so my images will be displayed correctly? I usually do that manually, but in this case it's a script which simply adds the image to the .tex body and compile it. I'm pretty sure I'm missing something obvious here, but couldn't figure out what it is.

0

2 Answers 2

71

graphicx provides the boolean value keepaspectratio. It's explained in the documentation as follow:

If set to true then specifying both width and height (or totalheight) does not distort the figure but scales such that neither of the specified dimensions is exceeded.

\includegraphics[width=\linewidth,height=\textheight,keepaspectratio]{picture}
10
  • 7
    Just in case someone needs the same for text: The adjustbox package allows to use \adjustbox[width=\linewidth,height=\textheight,keepaspectratio]{<text>}. You might want to use totalheight instead of height. Alternatievly you can use \resizebox{<width>}{<height>}{<text>} (or \resizebox* to use totalheight instead) but this requires \Gin@isotrue locally to enable the keepaspectratio feature. You could write is as \csname Gin@isotrue\endcsname to avoid issues with the @. May 25, 2011 at 11:21
  • 1
    @Marco @Martin Is there something similar for floats? If I use \begin{figure}[p] \includegraphics[width=\linewidth,height=\textheight,keepaspectratio]{picture} \caption{somecaption} \end{figure} then the graphic will be scaled to fit the page, but the float might still produce an overfull hbox because of the caption.
    – Janek
    Jun 20, 2013 at 11:27
  • 1
    @Janek: This shouldn't happen except the caption contains some strange content which can't be broken or hyphenated correctly. Also, try loading the caption package to get smarter captions. Jun 20, 2013 at 12:59
  • 2
    @Janek: The height=\textheight is the problem. It doesn't leave any space for the caption. Try height=\dimexpr\textheight-2\baselineskip-\abovecaptionskip-\belowcaptionskip\relax instead. Jun 21, 2013 at 11:59
  • 1
    @Janek: You need to adjust the height according to the height of the caption. You can get the height of the caption by storing it into a box first. If you post a follow-up question I will post a full answer. Jun 25, 2013 at 13:18
0

To have an effect similar to object-fit: cover in CSS, here is a snippet that could help you (supposing you use it with dimensions in pt, e.g. \textwidth or 0.4\textheight).

\newcommand{\ptToCm}[1]{#1 / 28.3464567}

\newcommand{\resizeimage}[3]{
  \begin{tikzpicture}
    % Calculate aspect ratio of the box
    \pgfmathsetmacro{\boxwidth}{#1}
    \pgfmathsetmacro{\boxheight}{#2}
    \pgfmathsetmacro{\boxaspectratio}{\boxwidth/\boxheight}

    % Read the dimensions of the image
    \newbox\imagebox
    \setbox\imagebox=\hbox{\includegraphics{#3}}
    \pgfmathsetmacro{\imagewidth}{\wd\imagebox}
    \pgfmathsetmacro{\imageheight}{\ht\imagebox}
    \pgfmathsetmacro{\imageaspectratio}{\imagewidth/\imageheight}

    \clip (0,0) rectangle (#1,#2);

    % Scale and place the image
    \begin{scope}[shift={(0.5 * \ptToCm{\boxwidth},0.5 * \ptToCm{\boxheight})}]
      \pgfmathparse{\boxaspectratio > \imageaspectratio ? 1 : 0}
      \ifnum\pgfmathresult=1
        \node[anchor=center, inner sep=0pt] at (0,0) {\includegraphics[width=\boxwidth pt]{#3}};
      \else
        \node[anchor=center, inner sep=0pt] at (0,0) {\includegraphics[height=\boxheight pt,keepaspectratio]{#3}};
      \fi
    \end{scope}
  \end{tikzpicture}
}

\resizeimage{\textwidth}{0.4\textheight}{[pathtoimage]}

You must log in to answer this question.

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