1

I'm trying to re-define the \includegraphics command to add surrounding code to hide warnings using the silence package.

So far I have the following 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}

This works fine except where the new \includegraphics is used in the \subfloat. I get the following errors in the \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}}
                                                  \\

What am I missing here?

If there is a simpler way to perform the redefinition that would be great.

zelanix
  • 525
  • You should not need to redefine it. What error were you getting? – David Carlisle Feb 16 '15 at 11:14
  • It might be a lot better if you explained what exactly it is you are attempting to do, which is not really obvious from the MWE. Also, have you considered using xparse instead of all those manual argument parsing? – daleif Feb 16 '15 at 11:34
  • @daleif, I have updated the question to give a more detailed description of what I'm trying to acheive, but I really do think that this is just a problem with the re-definition - if there is a simpler way to achieve this then I would be very interested to hear it. I was not aware of xparse. – zelanix Feb 16 '15 at 11:46
  • Please also note that for me this is partially an exercise in understanding the internals of latex so I would be really interested to know what is actually wrong with what I am doing currently and why it doesn't work in the \subfloat. – zelanix Feb 16 '15 at 11:47
  • It's not clear what warnings you're trying to hide. And \includegraphics only accepts one optional argument, so the part you're doing with two optional arguments is useless. – egreg Feb 16 '15 at 11:48
  • @egreg, I really think that what warnings I'm trying to hide isn't relevant, but you can find the full details in here - I was just trying to avoid putting too much detail into one question. Also, surely \includegraphics*[100,100][300,300]{mypicture} is valid syntax? source. – zelanix Feb 16 '15 at 11:56
  • @zelanix That syntax is for graphics, not graphicx; when you load graphicx, you specify the bounding box by \includegraphics*[bb=100 100 300 300]{mypicture}. – egreg Feb 16 '15 at 12:14
  • @egreg, should that be viewport instead? Does bb woth with pdflatex? – daleif Feb 16 '15 at 12:14
  • @daleif They're different options, AFAIK. – egreg Feb 16 '15 at 12:17
  • Thanks @egreg, I see - I wasn't aware of that, I was just trying to be as generic as possible. I still don't understand why my redefined \includegraphics doesn't work inside the \subfloat though. Thanks for the answer though :) – zelanix Feb 16 '15 at 12:21
  • I don't see what you gain to silence the "File not found" warning. Don't you still get a pdftex error? Imho you should better check for existence http://tex.stackexchange.com/a/99099/2388 – Ulrike Fischer Feb 16 '15 at 12:22

2 Answers2

2

There is no need for a redefinition, this works without error:

enter image description here

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

Edit after MWE edited to add `silence package usage:

I can think of no reason why you should want to disable warnings but if you do let \includegraphics parse its arguments and just add it around the internal command.

\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
David Carlisle
  • 757,742
  • Thanks, but in reality I need to add extra code around the \includegraphicsorig in the redefinition - this is just a MWE – zelanix Feb 16 '15 at 11:24
  • @zelanix an MWE is a good thing but "which works well except when using it inside a \subfloat" Implies that the command does not work inside subfloat so any answer would be misleading to future readers. If you want to redefine it for some reason perhaps you should ask about that, the redefinition looks vastly too complicated, why not leave the original to do the argument parsing and just redefine the internal version, and any redefinition is presumably unreleated to subfloat except that that's where you happen to be using it. – David Carlisle Feb 16 '15 at 11:27
  • Alright, that makes sense - apologies for the confusion. I will rephrase the question to clarify. – zelanix Feb 16 '15 at 11:33
  • I have updated my MWE with a more detailed description of what I'm trying to achieve, but I really think that the problem is with the redefinition still. If there is a way to simplify this then that would be great! – zelanix Feb 16 '15 at 11:45
  • @zelanix updated answer – David Carlisle Feb 16 '15 at 12:25
2

I'm not sure what warnings you want to suppress, but the way you chose is quite complicated and fragile (which is the reason for the failure with \subfloat).

Here's a version using xparse that makes checking for optional arguments very easy. I also add a + variant (to be specified before * if you need both) that doesn't execute the “enabling/disabling filter” commands.

Note also that, with graphicx, specifying the bounding box with two optional arguments is not supported, so there's no need for checking two optional arguments.

\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}
egreg
  • 1,121,712