为 beamer 重新定义 \includegraphics

为 beamer 重新定义 \includegraphics

我正在使用 Pandoc 将 markdown 转换为 LaTeX 或 Beamer。Pandoc 的 LaTeX 模板有很好的命令,可以调整图像大小以适合文本宽度,但 Beamer 输出却不是这样。

\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth
\else\Gin@nat@width\fi}
\makeatother
\let\Oldincludegraphics\includegraphics
\renewcommand{\includegraphics}[1]{\Oldincludegraphics[width=\maxwidth]{#1}}

所以我尝试将命令复制到 Beamer 模板,但在使用 pdflatex 编译时出现错误。我检查了前言,但无法找出哪一个不兼容。也许问题出在 beamer 类本身?

这是我的示例投影仪代码。

\documentclass[ignorenonframetext,]{beamer}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifxetex
  \usepackage{fontspec,xltxtra,xunicode}
  \defaultfontfeatures{Mapping=tex-text,Scale=MatchLowercase}
\else
  \ifluatex
    \usepackage{fontspec}
    \defaultfontfeatures{Mapping=tex-text,Scale=MatchLowercase}
  \else
    \usepackage[utf8]{inputenc}
  \fi
\fi

\usepackage{graphicx}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth
\else\Gin@nat@width\fi}
\makeatother
\let\Oldincludegraphics\includegraphics
\renewcommand{\includegraphics}[1]{\Oldincludegraphics[width=\maxwidth]{#1}}
% Comment these out if you don't want a slide with just the
% part/section/subsection/subsubsection title:
\AtBeginPart{\frame{\partpage}}
\AtBeginSection{\frame{\sectionpage}}
\AtBeginSubsection{\frame{\subsectionpage}}
\AtBeginSubsubsection{\frame{\subsubsectionpage}}
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
\setlength{\emergencystretch}{3em}  % prevent overfull lines
\setcounter{secnumdepth}{0}


\begin{document}

\begin{frame}\frametitle{Introduction}

Testing image size from R saved by various methods.

\end{frame}

\begin{frame}\frametitle{Results}

All images were set width = 7. For ggsave, the dpi =300.

\begin{block}{Saved by ggsave}

\begin{figure}[htbp]
\centering
\includegraphics{./plot_by_ggsave.pdf}
\caption{Saved by ggsave}
\end{figure}

\end{block}
\end{frame}

\end{document}

答案1

该类beamer重新定义了\includegraphics,但是“在文档开始处”,因此必须同时给出重新定义:

\usepackage{letltxmacro}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\makeatother
\AtBeginDocument{
  \LetLtxMacro\Oldincludegraphics\includegraphics
  \renewcommand{\includegraphics}[2][]{%
    \Oldincludegraphics[#1,width=\maxwidth]{#2}}
}

请参阅文档letltxmacro以了解为什么\LetLtxMacro需要它。

相关内容