\includegraphics は、figure 環境の内外で異なる動作をします

\includegraphics は、figure 環境の内外で異なる動作をします

\includegraphics環境内では通常どおりに動作し、環境figure外では自動的に中央揃えになるように再定義したいと思います。figure

これが私の試みです:

\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}

これは意図したとおりに動作します (最初の図は中央揃え、2 番目の図は左揃え)。

しかし、このコマンドを で使用するとbeamer、機能しなくなります。

\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}

両方の図が中央に配置されています。何が問題なのでしょうか? どうすれば修正できますか?

関連情報