\includegraphics 在图形环境内外的工作方式不同

\includegraphics 在图形环境内外的工作方式不同

我想重新定义\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}

这按预期工作(第一个图形居中,第二个图形左对齐)。

但是一旦我在中使用这个命令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}

两个图形都居中。问题出在哪里?我该如何修复?

相关内容