[Tex/LaTex] Macro for \textwidth, \textheight and keepaspectratio

graphicsscaling

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?

Best Answer

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:

enter image description here