21

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.

lockstep
  • 250,273
gablin
  • 17,006

2 Answers2

26

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.

Martin Scharrer
  • 262,582
2

works also for images which are wrongly set to a width greater than the text width:

\documentclass{minimal} 
\usepackage{graphicx}
\newsavebox\IBox
\let\Includegrfx\includegraphics
\renewcommand\includegraphics[2][]{%
  \sbox\IBox{\Includegrfx[#1]{#2}}%
  \ifdim\wd\IBox>\textwidth\resizebox{\textwidth}{!}{\usebox\IBox}\else
                           \usebox\IBox\fi}
\parindent=0pt % for demo
\begin{document} 
\rule{\textwidth}{1pt}

\includegraphics[width=3\textwidth]{tiger}

\end{document}