redefinir \includegraphics para o beamer

redefinir \includegraphics para o beamer

Estou usando o Pandoc para converter de markdown para LaTeX ou Beamer. O modelo LaTeX do Pandoc possui um bom comando para que as imagens sejam redimensionadas para caber na largura do texto, mas a saída do Beamer não.

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

Então tentei copiar os comandos para o template Beamer, mas deu um erro ao compilar com pdflatex. Verifiquei o preâmbulo, mas não consegui descobrir qual deles é incompatível. Talvez a própria classe beamer seja o problema?

Aqui está meu exemplo de código do 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}

Responder1

A beamerclasse redefine \includegraphics, mas "no início do documento", portanto sua redefinição deve ser dada ao mesmo tempo:

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

Consulte a documentação de letltxmacropara entender por que \LetLtxMacroé necessário.

informação relacionada