# How to automatically center images?

In a beamer-LaTeX document, I want to horizontally center all images, without specifying this explicitly in each case. I did the following:

In the preamble I write:

\let\oig\includegraphics % original includegraphics
\renewcommand{\includegraphics}[1]{{\centering\oig{#1}\par}}


And in the body e.g.

\begin{frame}{Frame title}

\includegraphics{mcg}

\end{frame}


The result is the error message

 [9

]
Runaway argument?
][
! Paragraph ended before \Gin@iii was complete.
<to be read again>
\par
l.224 \end{frame}


(The snippet above generates the 10th slide of my presentation, and the last line of the snippet is line 224 of the .tex-file.)

What am I doing wrong? How is this done correctly?

Note: the body of the beamer-LaTeX document is automatically generated by pandoc, which means I cannot change the syntax with which images are included.

• Then\includegraphics command takes two arguments: one required and one optional argument, so when you define it you should make sure you deal with the optional argument. – user10274 Aug 20 '15 at 16:54
• @MarcvanDongen, but using \renewcommand I overwrite the original definition of \includegraphics and make my own, don't I? And my own definition has only 1 argument which is required. Correct me if I'm wrong. – A. Donda Aug 20 '15 at 16:59
• You are also killing the fact that \includegraphics is overlay-aware. – Gonzalo Medina Aug 20 '15 at 17:17
• @GonzaloMedina, then can you tell me how to achieve this correctly? – A. Donda Aug 20 '15 at 17:23
• @A.Donda I may have commented too quickly. It looks as if my concern doesn't apply (as in: it's not the cause of the error). – user10274 Aug 20 '15 at 17:33

The beamer class redefines \includegraphics to make it a robust command and overlay aware.

It's a bit complicated to get the thing right, because the redefinitions happen at begin document.

\documentclass{beamer}
\usepackage{etoolbox}

\AtBeginDocument{%
\letcs\oig{@orig\string\includegraphics}%
\renewcommand<>\includegraphics[2][]{%
\only#3{%
{\centering\oig[{#1}]{#2}\par}%
}%
}%
}

\begin{document}

\begin{frame}
\frametitle{Test}

Some meaningless text

\includegraphics<2>[width=.3\textwidth]{duck}

Other meaningless text

\end{frame}

\end{document}


• I used the code from the previous version of your answer, which seems to work fine. What's the difference here? – A. Donda Aug 20 '15 at 17:53
• This one works, too, though. – A. Donda Aug 20 '15 at 18:04


\documentclass{beamer}
\usepackage{lmodern}
\begin{document}

\newcommand*{\myincludegraphics}[2]{{\centering\includegraphics[#1]{#2}\par}}

\begin{frame}
\myincludegraphics{width=.4\textwidth}{example-image-A}
\end{frame}
\begin{frame}
\myincludegraphics{width=.4\textwidth}{example-image-B}
\end{frame}
\begin{frame}
\myincludegraphics{width=.4\textwidth}{example-image-C}
\end{frame}

\end{document}


• As I wrote in the question, "the body of the beamer-LaTeX document is automatically generated by pandoc, which means I cannot change the syntax with which images are included". Of course an alternative would be a sed script or similar, but I'd prefer a LaTeX-only version. Thanks though. – A. Donda Aug 20 '15 at 17:49
• Better to use \newcommand<> so the new command is overlay-aware. – Gonzalo Medina Aug 20 '15 at 17:50