# Patching/Preventing effective code in a package

Consider a command being defined in a package (or used from another package), say \somethingstupid and being used before \endinput within the package, doing some non-typesetting setup (coloring, setting lengths, counters etc. or even defining other commands...).

Sometimes this code is annoying and should not be used at all.

Is there a way to patch the package (not only the command) to prevent this effective code?

Of course the 'annoying' part of the command can be patched but this can be complicated.

Here is a MWE

dummypackage.sty

\ProvidesPackage{dummypackage}
\RequirePackage{xcolor}

\newcommand{\somethingstupid}[1]{%
%%% A lot of non-typesetting code before
\color{#1}   % 'stupid' too
%%% A lot of non-typesetting code after
}

%%% Other stuff

\somethingstupid{blue} % Should not be here for some reasons

%%% Other stuff

\endinput


And some driver .tex file

\documentclass{article}

\usepackage{dummypackage}

\usepackage{blindtext}

\begin{document}
\blindtext
\end{document}

• Sorry, not in a general way. You could patch \newcommand to trap the offending command(s) (very fragile, of course), but there are too many possible variants: some command can even not being explicitly defined. – egreg Apr 6 '15 at 20:13
• @egreg: I feared this :-( – user31729 Apr 6 '15 at 20:14
• Knowing what action \somethingstupid does, you can undo it as soon as the package is loaded. Or try patching \@begindocumenthook if the code is delayed (difficult, though). – egreg Apr 6 '15 at 20:16
• Could you patch \somethingstupid to save a copy of itself and then redefine itself to innocuous behavior. And then, add an \AtBeginDocument macro to reinstate the original definition? – Steven B. Segletes Apr 6 '15 at 20:16
• @egreg: The reason why I asked is the answer I gave to this problem: tex.stackexchange.com/questions/237134/… (which is basically what you proposed: Redefining/reusing/redoing code) – user31729 Apr 6 '15 at 20:19

This puts

Ignoring \somethingstupid


in the log and doesn't execute the package definition of \somethingstupid

\documentclass{article}

\def\zsomethingstupid{\somethingstupid}
\def\newcommand#1{%
\def\ztmp{#1}%
\ifx\ztmp\zsomethingstupid
\expandafter\zz
\else
\expandafter
\znewcommand
\fi
#1}

\def\zz#1[#2]#3{\def#1##1{\typeout{Ignoring \string#1}}}

\usepackage{dummypackage}

\usepackage{blindtext}

\begin{document}
\blindtext
\end{document}

• A clever way ;-) – user31729 Apr 6 '15 at 20:23
• Hmm, any currently undefined command will be the same as \somethingstupid (before it's defined), as far as \ifx is concerned. – egreg Apr 6 '15 at 20:25
• @egreg It passed all the supplied tests:-) (I'll edit) – David Carlisle Apr 6 '15 at 20:27
• @egreg better..? – David Carlisle Apr 6 '15 at 20:29
• that would be OK, #1 would be * so it'll come back to \znewcommand I'm assuming the OP knows the form used in the command being trapped. – David Carlisle Apr 6 '15 at 20:32