I need to execute a \renewenvironment within a group. Using \global\renewenvironment is not sufficient.
What changes do I need to make to the \DisableMyEnvironment macro in the MWE below so as to achieve the equivalent of \global\renewenvironment?
Currently, the MWE yields:
Once this is working as desired, the second line in blue should be in black.
Notes:
- There are two test cases. The default one is within a
\foreachand commenting out\def\ForeachTestCase{}uses a simple group.
References:
Code:
\def\ForeachTestCase{}%
\documentclass{article}
\usepackage{xcolor}
\usepackage{xstring}
\usepackage{tikz}
\newenvironment{MyEnvironment}[1][blue]{%
\begingroup
\color{#1}%
}{%
\endgroup
}%
\newcommand*{\DisableMyEnvironment}{%
%% How make this global??
\renewenvironment{MyEnvironment}[1][]{}{}%
}%
\newcommand*{\MyTestText}[1]{%
Some before text
\begin{MyEnvironment}
This should be in #1.
\end{MyEnvironment}
Some after text%
}
\begin{document}
\MyTestText{blue}
%\DisableMyEnvironment% <-- This works, but want it to work when used in a group as follows
\ifdefined\ForeachTestCase
\foreach \x in {1,...,3} {%
\IfStrEqCase{\x}{%
{1}{}%
{2}{\DisableMyEnvironment}%
{3}{}%
}%
}
\else
\begingroup
\DisableMyEnvironment
\endgroup
\fi
\medskip\par
\MyTestText{black}
\end{document}


\aftergroup\DisableMyEnvironment. I don't know if this will do what you want, though. – Phelype Oleinik Aug 28 '18 at 18:47\fpreach). Will look into it and try to figure it out. – Peter Grill Aug 28 '18 at 18:50\foreachdoes its thing inside two grouping levels, so\aftergroup\aftergroup\aftergroup\DisableMyEnvironmentshould do. Not the prettiest code there is though :P – Phelype Oleinik Aug 28 '18 at 18:52\environementand\endenvironmentseparately. – Peter Grill Aug 28 '18 at 19:16{\let\def\gdef\renewenvironment{MyEnvironment}[1][]{}{}}and it's global! – Andreas Storvik Strauman Aug 28 '18 at 19:29\foreach. – egreg Aug 29 '18 at 15:46