How can I undo an etoolbox patch?

First of all note that \BeforeBeginEnvironment and \AfterEndEnvironment does not patch the environment itself. It only adds material to hooks. etoolbox patches usage of these hooks to \begin and \end. That's the reason why resetting \itemize and \enditemize to their original definitions does not change anything.

But you may simply undefine the environment end hook of itemize:

\documentclass{article}
\usepackage{etoolbox}
\AfterEndEnvironment{itemize}{bleat bleat}
\begin{document}
\begin{itemize}
\item This is an item.
\end{itemize}

\csundef{@afterend@itemize@hook}% undefine the end hook of itemize
\begin{itemize}
\item This is another item.
\end{itemize}
\end{document}

\csundef is a etoolbox command and \@afterend@itemize@hook is the environment end hook of environment itemize. The environment begin hook would be \@beforebegin@itemize@hook.

Maybe a feature request for commands like \CleanBeforeBeginEnvironment or \CleanAfterBeginEnvironment could be suggested.

Alternative: If you only want to remove something but not everything, you may try this:

\documentclass{article}
\usepackage{etoolbox}

\newrobustcmd*{\RemoveFromAfterEndEnvironment}[2]{%
  \expandafter\patchcmd\csname @afterend@#1@hook\endcsname{#2}{}%
}

\AfterEndEnvironment{itemize}{bleat bleat\par}{}{}
\AfterEndEnvironment{itemize}{don't remove this\par}{}{}
\begin{document}
\begin{itemize}
\item This is an item.
\end{itemize}

\RemoveFromAfterEndEnvironment{itemize}{bleat bleat\par}{}{}
\begin{itemize}
\item This is another item.
\end{itemize}
\end{document}

The third and fourth arguments of \RemoveFromAfterEndEnviroment are the same like third and fourth arguments of \patchcmd.


There might be an official way, but defining a new command \AfterEndItemize to specify what you want to do after \end{itemize}, and then use \renewcommand to change \AfterEndItemize to not do anything seems to work:

\documentclass{article}
\usepackage{etoolbox}

\newcommand{\AfterEndItemize}{bleat bleat}%
\AfterEndEnvironment{itemize}{\AfterEndItemize}

\begin{document}
\begin{itemize}
\item This is an item.
\end{itemize}
%
\renewcommand{\AfterEndItemize}{}%
\begin{itemize}
\item This is another item.
\end{itemize}
\end{document}

use it this way:

\documentclass{article}

\let\End\end
\usepackage{etoolbox}
\AfterEndEnvironment{itemize}{bleat bleat}
\begin{document}

\begin{itemize}
\item This is an item.
\end{itemize}

\let\end\End 
\begin{itemize}
\item This is the last item.
\end{itemize}

\end{document}

Tags:

Etoolbox