I have been exploring different methods of suppressing environment content under some conditionals. The problem with environments is that a lot of the tricks don't work because of how the environment scans for an end token and so suppressing content can often fail to find the end of the suppression. For example:
\begin{document}
\newif\iftest
\testfalse
\newenvironment{testenv}{\iftest}{\fi}
\begin{document}
\begin{testenv}
text!
\end{testenv}
\end{document}
This returns "incomplete iffalse" because it fails to find the \fi since it can't execute the \end{testenv} while it looks for the \fi (thus not finding the end. This part I get.
I've tried various other avenues but I'd like to stay away from verbatim environments as I'd like to nest this eventually and nesting verbatim environments is a monumental headache.
But then, I tripped across the "suppress" command found here: Selectively suppress generation of typeset output.
So I built the following working code:
\documentclass{article}
\usepackage{amsthm}
\usepackage{environ}
\newif\iftest
\testfalse
\NewEnviron{testenv}{\iftest\BODY\else Nope! \fi}
\newcommand{\Test}{}
\makeatletter
\font\dummyft@=dummy \relax
\def\suppress{%
\begingroup\par
\parskip\z@
\offinterlineskip
\baselineskip=\z@skip
\lineskip=\z@skip
\lineskiplimit=\maxdimen
\dummyft@
\count@\sixt@@n
\loop\ifnum\count@ >\z@
\advance\count@\m@ne
\textfont\count@\dummyft@
\scriptfont\count@\dummyft@
\scriptscriptfont\count@\dummyft@
\repeat
\let\selectfont\relax
\let\mathversion\@gobble
\let\getanddefine@fonts\@gobbletwo
\tracinglostchars\z@
\frenchspacing
\hbadness\@M}
\def\endsuppress{\par\endgroup}
\makeatother
\newenvironment{testenvtwo}{
\iftest\else\suppress\fi
}{
\iftest\else\endsuppress\fi
}
\begin{document}
Th below should read "Nope"
\begin{testenv}
This shouldn't show!
\end{testenv}
\hrule
Below this should be nothing
\begin{testenvtwo}
This also shouldn't show!
\end{testenvtwo}
\hrule
\testtrue
Below this should be "This should show!"
\begin{testenv}
This should show!
\end{testenv}
\hrule
Below this should be "This also should show!"
\begin{testenvtwo}
This also should show!
\end{testenvtwo}
\hrule
\end{document}
So, why does \suppress command here manage to work correctly when various other methods fail? I can't really understand how \suppress works in practice, but I think it's just gobbling everything it sees until the \endsuppress maybe? But this seems like it should be the same problem as with the \if \fi type structure.
If anyone can enlighten me as to why this works when \fi and environment hooks from etoolbox and the like failed, I'd really appreciate it. I'd like to be able to make this kind of command in the future, so I'm trying to understand how it works.