redefinir \includegraphics para proyector

redefinir \includegraphics para proyector

Estoy usando Pandoc para convertir de Markdown a LaTeX o Beamer. La plantilla LaTeX de Pandoc tiene un buen comando para que las imágenes cambien de tamaño para ajustarse al ancho del texto, pero la salida de Beamer no.

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

Entonces intenté copiar los comandos a la plantilla de Beamer, pero luego aparece un error al compilar con pdflatex. Revisé el preámbulo pero no pude determinar cuál es incompatible. ¿Quizás la clase de proyector en sí sea el problema?

Aquí está mi código de proyector de muestra.

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

Respuesta1

La beamerclase redefine \includegraphics, pero "al inicio del documento", por lo que su redefinición debe darse al mismo tiempo:

\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 la documentación de letltxmacropara comprender por qué \LetLtxMacroes necesario.

información relacionada