如何最好地重新定义 \includegraphics 命令?

如何最好地重新定义 \includegraphics 命令?

我正在尝试重新定义\includegraphics命令以添加周围的代码来使用该silence包隐藏警告。

到目前为止,我有以下 MWE:

\documentclass{article}

\usepackage{graphicx}
\usepackage{subfig}
\usepackage{silence}
\usepackage{letltxmacro}

\WarningFilter[warning_to_hide]{latex}{File}

\makeatletter
  \LetLtxMacro{\includegraphicsorig}{\includegraphics}
  \def\includegraphicsnowarningstar{%
    \@ifnextchar[%
      {\includegraphicsnowarningstar@i}
      {\includegraphicsnowarningstar@j}}
  \def\includegraphicsnowarning{%
    \@ifnextchar[%
      {\includegraphicsnowarning@i}
      {\includegraphicsnowarning@j}}
  \def\includegraphicsnowarningstar@i[#1]{%
    \@ifnextchar[%
      {\includegraphicsnowarningstar@jjj[#1]}
      {\includegraphicsnowarningstar@jj[#1]}}
  \def\includegraphicsnowarning@i[#1]{%
    \@ifnextchar[%
      {\includegraphicsnowarning@jjj[#1]}
      {\includegraphicsnowarning@jj[#1]}}

  \def\includegraphicsnowarningstar@j#1          {\ActivateWarningFilters[warning_to_hide]\includegraphicsorig*{#1}\DeactivateWarningFilters[warning_to_hide]}
  \def\includegraphicsnowarning@j#1              {\ActivateWarningFilters[warning_to_hide]\includegraphicsorig{#1}\DeactivateWarningFilters[warning_to_hide]}
  \def\includegraphicsnowarningstar@jj[#1]#2     {\ActivateWarningFilters[warning_to_hide]\includegraphicsorig*[#1]{#2}\DeactivateWarningFilters[warning_to_hide]}
  \def\includegraphicsnowarning@jj[#1]#2         {\ActivateWarningFilters[warning_to_hide]\includegraphicsorig[#1]{#2}\DeactivateWarningFilters[warning_to_hide]}
  \def\includegraphicsnowarningstar@jjj[#1][#2]#3{\ActivateWarningFilters[warning_to_hide]\includegraphicsorig*[#1][#2]{#3}\DeactivateWarningFilters[warning_to_hide]}
  \def\includegraphicsnowarning@jjj[#1][#2]#3    {\ActivateWarningFilters[warning_to_hide]\includegraphicsorig[#1][#2]{#3}\DeactivateWarningFilters[warning_to_hide]}

  \renewcommand{\includegraphics}{%
    \@ifstar
      {\includegraphicsnowarningstar}
      {\includegraphicsnowarning}}
\makeatother

\begin{document}

  \begin{figure}[t]
    \centering
    \includegraphics{test.jpg}
    \caption{This is a working test image.}
  \end{figure}

  \begin{figure}[t]
    \centering
    \subfloat[][First sub-figure]{\includegraphics{test.jpg}}\\
    \subfloat[][Second sub-figure]{\includegraphics{test.jpg}}
    \caption{Why doesn't this work?}
  \end{figure}

\end{document}

\includegraphics除了在 中使用new 的地方外,此方法工作正常\subfloat。我在 中收到以下错误\subfloat

! Argument of \includegraphicsnowarning@j has an extra }.
<inserted text> 
                \par 
l.52 ...st sub-figure]{\includegraphics{test.jpg}}
                                                  \\
Runaway argument?
{test.jpg}
! Paragraph ended before \includegraphicsnowarning@j was complete.
<to be read again> 
                   \par 
l.52 ...st sub-figure]{\includegraphics{test.jpg}}
                                                  \\

我在这里遗漏了什么?

如果有更简单的方法来执行重新定义那就太好了。

答案1

不需要重新定义,这不会出错:

在此处输入图片描述

\documentclass{article}

 \usepackage{graphicx}
 \usepackage{subfig}

 \begin{document}

     \begin{figure}[t]
         \centering
         \includegraphics[width=.7\linewidth]{example-image}
         \caption{This is a working test image.}
     \end{figure}

     \begin{figure}[t]
         \centering
         \subfloat[][First sub-figure]{\includegraphics[width=.7\linewidth]{example-image}}\\
         \subfloat[][Second sub-figure]{\includegraphics[width=.7\linewidth]{example-image}}
         \caption{Why doesn't this work?}
     \end{figure}

 \end{document}

在 MWE 编辑后进行编辑以添加“静音包使用”:

我想不出您为什么要禁用警告,但如果您确实要\includegraphics解析其参数并将其添加到内部命令周围,那就可以了。

\usepackage{silence}
\makeatletter

\let\oldGin@ii\Gin@ii
\def\Gin@ii[#1]#2{%
\ActivateWarningFilters[warning_to_hide]%
\oldGin@ii[{#1}]{#2}%
\DeactivateWarningFilters[warning_to_hide]}
\makeatletter

答案2

我不确定您想要抑制什么警告,但是您选择的方式非常复杂且脆弱(这是失败的原因\subfloat)。

这是一个使用版本xparse,它使检查可选参数变得非常容易。我还添加了一个+变体(如果需要两者,请先指定*),它不执行“启用/禁用过滤器”命令。

还要注意,使用 时graphicx不支持使用两个可选参数指定边界框,因此无需检查两个可选参数。

\documentclass{article}
\usepackage[margin=1cm]{geometry} % just to fit as much as possible
\pagestyle{empty} % page number is useless for the example

\usepackage{graphicx}
\usepackage{subfig}
\usepackage{silence}
\usepackage{letltxmacro}
\usepackage{xparse}

\LetLtxMacro{\includegraphicsorig}{\includegraphics}
\RenewDocumentCommand{\includegraphics}{ t+ s O{} m }{%
  \IfBooleanF{#1}{\ActivateWarningFilters[warning_to_hide]}%
  \IfBooleanTF{#2}{\includegraphicsorig*}{\includegraphicsorig}[#3]{#4}%
  \IfBooleanF{#1}{\DeactivateWarningFilters[warning_to_hide]}%
}

\WarningFilter[warning_to_hide]{latex}{File}


\begin{document}

\begin{figure}[!htp]
\centering
\includegraphics[width=2cm]{example-image}
\caption{This is a working test image.}
\end{figure}

\begin{figure}[!htp]
\centering
\subfloat[][First sub-figure]{\includegraphics[width=2cm]{example-image}}\\
\subfloat[][Second sub-figure]{\includegraphics[width=2cm]{example-image}}
\caption{Why doesn't this work?}
\end{figure}

\begin{figure}[!htp]
\centering
\includegraphics+[width=2cm]{example-image}
\caption{This is a working test image.}
\end{figure}

\begin{figure}[!htp]
\centering
\subfloat[][First sub-figure]{\includegraphics+[width=2cm]{example-image}}\\
\subfloat[][Second sub-figure]{\includegraphics+[width=2cm]{example-image}}
\caption{Why doesn't this work?}
\end{figure}

\end{document}

相关内容