[Tex/LaTex] Shrink figure only when necessary

beamerscalingtikz-pgf

I'm writing a report where I have generated and included several TikZ pictures which I also want to use in my beamer presentation. However, some of the pictures (but not all) which fit in the report are too large for the slides. Hence, I wonder if there is any package or macro which shrinks a figure if it is too large, but leaves it alone if it already fits.

If the answer only works for TikZ then that's fine. I don't mind shrinking texts and lines along with the entire figure.

Best Answer

The adjustbox package can be used to scale (or otherwise adjust) images or any other content, like a tikzpicture in several ways. You need the max size={<width>}{<height>} option in your case, which does exactly what you requested, i.e. only shrink the content if it is larger than the given width and/or height. It always keeps the aspect-ratio.

\usepackage{adjustbox}

% ..

\begin{adjustbox}{max size={.95\textwidth}{.8\textheight}}
  \begin{tikzpicture}
    % ..
  \end{tikzpicture}
\end{adjustbox}

This is also possible for images. Here the same key can be used with \adjustimage:

\adjustimage{max size={..}{..}}{<imagefilename>}

which uses \includegraphics internally, but wraps it in a \adjustbox macro.