\incluye gráficos que funcionan de manera diferente dentro y fuera del entorno de la figura

\incluye gráficos que funcionan de manera diferente dentro y fuera del entorno de la figura

Quiero redefinir \includegraphicspara que, si bien funciona como de costumbre dentro de un figureentorno, se centre automáticamente fuera de él.figure los entornos.

Este es mi intento:

\documentclass{article}
\usepackage{graphicx}
\makeatletter
\let\old@includegraphics\includegraphics
\renewcommand\includegraphics[2][]{
    \ifx\@currenvir\@figureenvname
        \old@includegraphics[#1]{#2}
    \else
        \begin{center}\old@includegraphics[#1]{#2}\end{center}
    \fi}
\newcommand*\@figureenvname{figure}
\makeatother
\begin{document}
\includegraphics[width=0.4\textwidth]{example-image-a}
\begin{figure}[ht]
    \includegraphics[width=0.4\textwidth]{example-image-a}
\end{figure}
\end{document}

Esto funciona según lo previsto (primera figura centrada y segunda figura alineada a la izquierda).

Pero una vez que uso este comando beamer, ya no funciona:

\documentclass{beamer}
\usepackage{graphicx}
\makeatletter
\let\old@includegraphics\includegraphics
\renewcommand\includegraphics[2][]{
    \ifx\@currenvir\@figureenvname
        \old@includegraphics[#1]{#2}
    \else
        \begin{center}\old@includegraphics[#1]{#2}\end{center}
    \fi}
\newcommand*\@figureenvname{figure}
\makeatother
\begin{document}
\begin{frame}
\includegraphics[width=0.4\textwidth]{example-image-a}
\begin{figure}[ht]
    \includegraphics[width=0.4\textwidth]{example-image-a}
\end{figure}
\end{frame}
\end{document}

Ambas figuras están centradas. ¿Cuál es el problema? ¿Cómo puedo arreglarlo?

información relacionada