4

On the question How to create a dummy generic environment replacement? I learned how to replace an generic envinronment with a dummy one. Until now I had success replacing then as replacing \currenttime from datetime package with \def\currenttime{Current Time}.

But when I tried this strategy with \includegraphics:

\documentclass[10pt,a5paper,twoside]{memoir}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage[brazil]{babel}
\usepackage[showframe,pass]{geometry}

\newif\ifdebug
\debugtrue
% \debugfalse

\ifdebug
    % \def\includegraphics{includegraphics}
    \newcommand{\includegraphics}{includegraphics}
\else\fi

\begin{document}

    \includegraphics[width=\linewidth]{pictures/ex01}

\end{document}

It does not worked:

! Missing number, treated as zero.
<to be read again> 
                   ]
l.86     \includegraphics[width=\linewidth]
                                           {pictures/ex01}
A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)

! Illegal unit of measure (pt inserted).
<to be read again> 
                   ]
l.86     \includegraphics[width=\linewidth]
                                           {pictures/ex01}
Dimensions can be in units of em, ex, in, pt, pc,
cm, mm, dd, cc, nd, nc, bp, or sp; but yours is a new one!
I'll assume that you meant to say pt, for printer's points.
To recover gracefully from this error, it's best to
delete the erroneous units; e.g., type `2' to delete
two letters. (See Chapter 27 of The TeXbook.)

Can I wrap the command contents as \includegraphics[width=\linewidth]{pictures/ex01} on verbatim environment as done for environments?

\@ifundefined{longtable}
{
    \newenvironment{longtable}[0]
        {longtable environment \par\verbatim\tiny}
        {\endverbatim\endgraf\normalfont replacement for debug mode}
}{}

Update

Trying to do:

\newcommand{\includegraphics}[2]{\par\verbatim\tiny includegraphics #1 #2 \endverbatim\endgraf\normalfont}

Throws a new error:

File: t1cmtt.fd 2014/09/29 v2.5h Standard LaTeX font definitions
)
Runaway argument?
! Paragraph ended before \next was complete.
<to be read again> 
                   \par 
l.87 

I suspect you've forgotten a `}', causing me to apply this
control sequence to too much text. How can we recover?
My plan is to forget the whole thing and hope for the best.

But it is printing something on the PDF:

enter image description here


Related:

  1. 'Dummy' LaTeX environment
  2. More than one optional argument for newcommand
  3. https://en.wikibooks.org/wiki/LaTeX/Macros
user
  • 4,745
  • 4
    Why not just pass the draft or demo option to graphicx? – Alan Munn Aug 20 '17 at 19:27
  • My idea is not to load it, only treat its instructions as plain text or perhaps hide it. – user Aug 20 '17 at 19:30
  • 1
    Did you try what I suggested? – Alan Munn Aug 20 '17 at 19:41
  • 4
    You need to handle the arguments, e.g. with \renewcommand\includegraphics[2][]{....} – Ulrike Fischer Aug 20 '17 at 19:42
  • You are missing the default for the optional argument. Not[2] but [2][]. And don't use verbatim here, it doesn't make much sense. \detokenize{#2} is better. – Ulrike Fischer Aug 20 '17 at 20:02
  • @UlrikeFischer, its magic! I used \newcommand{\includegraphics}[2][]{ includegraphics \detokenize{#1} \detokenize{#2} } and it worked. – user Aug 20 '17 at 20:41
  • @Alan Munn, if using draft or demo option to graphicx does the same thing, I would just look into their implementation and copy it. Also, now I can do it for any package command, whether they support draft or demo option or not. Perhaps my question title was misleading. I will change it to How to replace a command with a dummy one? – user Aug 20 '17 at 20:44
  • 3
    Don't believe that you can use the same dummy for everything. In general you need to have an idea about the type of arguments a command can receive to write a good dummy command. – Ulrike Fischer Aug 20 '17 at 20:48
  • So perhaps other commands can have a little different signature, but I think can be learned its difference and the same ideia applied here used on them. – user Aug 20 '17 at 20:51

2 Answers2

4

Looks like a job of xparse ;-)

\documentclass[10pt,a5paper,twoside]{memoir}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage[brazil]{babel}
\usepackage[showframe,pass]{geometry}

\usepackage{xparse}
\usepackage{graphicx}

\newif\ifdebug
\debugtrue
% \debugfalse

\ifdebug
  \RenewDocumentCommand\includegraphics{s+O{}om}{includegraphics}
\else\fi

\begin{document}

    \includegraphics[width=\linewidth]{pictures/ex01}

\end{document}
Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • Why +O{}? som suffices – egreg Aug 21 '17 at 08:26
  • @egreg Original graphics syntax allows two optional arguments, and one (probably) needs to allow for \par tokens in keys (someone is bound to do that): on the O{} I was allowing for not needing a NoValue test if passing to key setting (so one only needs to check #3). – Joseph Wright Aug 21 '17 at 08:36
3

Something like this??

\documentclass[10pt,a5paper,twoside]{memoir}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

%\usepackage[brazil]{babel}
\usepackage[showframe,pass]{geometry}
\usepackage{graphicx}

\newif\ifdebug
\debugtrue
% \debugfalse

\ifdebug
    \renewcommand{\includegraphics}[2][]{\detokenize{\includegraphics[#1]{#2}}}
\else\fi

\begin{document}
\sloppy% can be used to avoid overfull boxes

    \includegraphics[width=\linewidth]{pictures/ex01}

    \includegraphics[width=\linewidth]{pictures/long-directory-name/ex01}

\end{document}

enter image description here

  • Yes, but I would not include graphicx, I would use \@ifundefined{includegraphics}{\newcommand{\includegraphics}...}{} instead of \renewcommand{\includegraphics}. This should be useful when I am disabling packages to find out incompatibilities. – user Aug 21 '17 at 00:27
  • @user how about \ifdebug \newcommand{\includegraphics}[2][]{\detokenize{\includegraphics[#1]{#2}}} \else \usepackage{graphicx} \fi – Steven B. Segletes Aug 21 '17 at 00:33
  • Basically, it is what I am doing, but for now I manually disable the \usepackage{graphicx} and set the \ifdebugtrue. – user Aug 21 '17 at 00:40