新答案

新答案

我想为元素的max width(和max height)设置一个默认值\includegraphics

\usepackage[export]{adjustbox}在序言中已经说明了图像的max width密钥。然后我尝试像这样设置默认值:

\setkeys{Gin}{max width=\textwidth,max height=0.5\textheight,keepaspectratio}

但就像它不存在一样。直接将它应用到元素上\includegraphics\includegraphics[max width=\textwidth,max height=0.5\textheight,keepaspectratio]{...}可以了。

答案1

新答案

\includegraphics可以使用etoolbox和,而不必重新定义\patchcmd。这可能是一个更清晰的解决方案。

\documentclass[]{article}

\usepackage[]{graphicx}
\usepackage[export]{adjustbox}
\usepackage{etoolbox}
\expandafter\patchcmd\csname Gin@ii\endcsname
  {\setkeys {Gin}{#1}}
  {%
    \setkeys {Gin}
      {max width=\textwidth,max height=.5\textwidth,keepaspectratio,#1}%
  }
  {}{}

\begin{document}
\includegraphics{./chapterinsection.pdf}
\end{document}

原始答案

一个丑陋的解决方案,重新定义\includegraphics为默认包含这两个键:

\documentclass[]{article}

\usepackage[]{graphicx}
\usepackage[export]{adjustbox}
\usepackage{letltxmacro}
\LetLtxMacro\includegraphicsBAK\includegraphics
\usepackage{xparse}
\newcommand\MyStarProcessor[1]
  {%
    \IfBooleanTF{#1}
      {\def\ProcessedArgument{*}}
      {\def\ProcessedArgument{}}%
  }
\RenewDocumentCommand \includegraphics { >{\MyStarProcessor}s O{} o m }
  {%
    \IfNoValueTF{#3}
      {%
        \includegraphicsBAK#1
          [%
            max width=\textwidth,
            max height=.5\textheight,
            keepaspectratio,
            #2
          ]
          {#4}%
      }
      {%
        \includegraphicsBAK#1[#2][#3]{#4}%
      }
  }

\begin{document}
\includegraphics{./chapterinsection.pdf}
\end{document}

相关内容