# Macro for \textwidth, \textheight and keepaspectratio

I'm using many pictures within my beamer presentation. Somehow I'm tired of writing:

\begin{figure}
\includegraphics[width=0.8\textwidth,height=0.8\textheight,keepaspectratio]{source}
\end{figure}


It works fine and does what I want, but I want to write less code :-)) e.g.:

\begin{figure}
\includegraphics[shrink=0.8]{source}
\end{figure}


Yes, I know scale but it works relative to the image size, but not (what I want) relative to the page size … Is there a lightweight and genuine way to achieve this?

-

You could define your own include command, like so:

\documentclass{beamer}
\newcommand{\mycommand}[1]{\includegraphics[width=0.8\textwidth,height=0.8\textheight,keepaspectratio]{#1}}

\begin{document}

\begin{frame}
\begin{figure}
\mycommand{source}
\end{figure}
\end{frame}

\end{document}


edit by Claudio Fiandrino

To have the possibility of shrink the image with custom values, one could proceed with a small change to the \mycommand:

\documentclass{beamer}
\usepackage{lmodern}
\usepackage{mwe}% for dummy images
% mycommand:
% #1: optional -> "shrink" value (default value to 0.8)
% #2: mandatory -> image
\newcommand{\mycommand}[2][0.8]{\includegraphics[width=#1\textwidth,height=#1\textheight,keepaspectratio]{#2}}

\begin{document}

\begin{frame}
\begin{figure}% notice that in Beamer having floating figures is not necessary
\mycommand[0.1]{example-image}~\mycommand[0.2]{example-image}~\mycommand[0.4]{example-image}
\end{figure}
\end{frame}

\end{document}


The result:

-
See below: Sometimes I want to have a "shrink" of 0.8, sometimes of 0.4 or sth. else… this one would fix the page-image-ratio to 0.8, wouldn't it? But if I modify your solution using two parameters, I could achieve my goal… –  Vincent Jan 13 '13 at 15:47
@Vincent: just change a bit the command: \newcommand{\mycommand}[2][0.8]{\includegraphics[width=#1\textwidth,height=#1\t‌​extheight,keepaspectratio]{#2}}. The use \mycommand{source} will use 0.8 by default, while \mycommand[0.1]{source} will shrink more the image. –  Claudio Fiandrino Jan 13 '13 at 15:51
@ClaudioFiandrino: this is what I'm looking for and trying to build! Thank you. –  Vincent Jan 13 '13 at 15:55

You can use

 \setkeys{Gin}{width=0.8\textwidth,height=0.8\textheight,keepaspectratio}


and it will apply to all following \includegraphics

If you want to make it paramatersised by argument then perhaps simplest is to observe that the keys you are using are defined by

\define@key{Gin}{width}{\def\Gin@ewidth{#1}}
\define@key{Gin}{height}{\def\Gin@eheight{#1}}
\define@key{Gin}{keepaspectratio}[true]{%
\lowercase{\Gin@boolkey{#1}}{iso}}


so all you need is

\makeatletter
\define@key{Gin}{myscale}{%
\def\Gin@ewidth{#1\linewidth}%
\def\Gin@eheight{#1\textheight}%
\Gin@isotrue}
\makeatother


Then myscale=0.8 does what you ask (\linewidth will be \textwidth or \columnwidth or some other length relevant for the current context.)

-
Note that \setkeys{Gin}{..} doesn't work with all graphicx keys (but with the three shown here it does), because some will try to affect the current image directly and if used like this there is no current image. Because of this I added a feature to adjustbox to also set such keys globally. –  Martin Scharrer Jan 13 '13 at 20:19