# How to control conditional statements within new commands and environments

I'm writing a pretty complicated class to typeset a book based on memoir.

One of my commands draws a caption on the preceding verso page, draws content on the following recto and verso pages, and then needs to draw content on the following recto page. However, the following recto page is created by an environment that begins with \clearpage so that its TOC entry and footer definition end up on the right page.

The command already knows how to start on the verso page using

\strictpagecheck%
\checkoddpage\ifoddpage\hbox{}\newpage\fi


But I can't figure out how to add content past a following \clearpage. I'm trying to make this work with conditional statements, but it's not working the way I expect.

My understanding of LaTeX (and thus TeX) macros is that when we define a newcommand or a newenvironment, LaTeX substitutes the macro's content where the macro command is placed when it compiles the document.

So in theory,

\newcommand{foo}{\Huge}

\begin{document}
\foo test text
\end{document}


Would be interpreted by LaTeX as

\Huge test text


If this is correct, I should be able to add conditional switches that are toggled wherever the command is used, as though it was being interpreted straight through.

Here is a MWE showing my use of conditionals to try to display or hide a black box on the page after the \complicated command, regardless of the fact that the exampleenvironment begins with \clearpage.

\documentclass{memoir}
\usepackage{tikz}

\newif\iftest
\testfalse

\newcommand{\drawbox}{%
\begin{tikz}[remember picture, overlay]
\node
[text width=1in,text height=1in,fill=black]
at (current page.center){};
\end{tikz}%
}

\newenvironment{exampleenvironment}{%
\clearpage%
\noindent\textsf{This is the test Environment}\par%
It needs to start with a clearpage, but has to accept the black box from the other environment.%
\iftest\drawbox\fi%
\testfalse%
\\\par%
}{%
\par\noindent\rule{\linewidth}{.4pt}%
}

\newcommand{\complicated}{%
\vfill%
\clearpage%
\clearpage%
\textbf{And needs to draw a box on the next page}%
\clearpage%
\testtrue%
}

\begin{document}

\begin{exampleenvironment}
foo bar baz bat
\end{exampleenvironment}

\complicated

\begin{exampleenvironment}
\end{exampleenvironment}

% conditional "test" should now be false...

\begin{exampleenvironment}
\end{exampleenvironment}

\end{document}


My MWE outputs this document, which is correct except the black box on the last page should not be there. Typing \testfalse where the MWE says % conditional "test" should now be false...' gives the desired behavior, but I would like the environment to toggle the conditional, not the user in the main TeX file.

When you perform \testtrue, it effectively does

\let\iftest\iftrue


The same goes for \testfalse (\let\iftest\iffalse). Since they both use \let, they're very similar in nature to that of macro definitions/assignments. And, macro definitions are local to the group within which they are made. As such, your issue of \testfalse inside the exampleenvironment doesn't survive past \end{exampleenvironment}.

Here is a toned-down version of your implementation:

\documentclass{article}

\newif\iftest
%\testfalse% default is false

\newenvironment{exampleenvironment}
{\testtrue}% \begin{exampleenvironment}
{}% \end{exampleenvironment}

\begin{document}

\verb|test| is \iftest true\else false\fi.

\begin{exampleenvironment}
Setting \verb|test| to true.
\end{exampleenvironment}

\verb|test| is \iftest true\else false\fi.

\end{document}


If you want such definitions to survive your environment, consider prepending it with a \global definition, as in \global\testtrue.Of course, this makes a global change to the conditional and therefore survives all (nested) groups. If you which for it to survive only the current group, you can use \aftergroup\testtrue. Note that \aftergroup takes only a single token. For extending this to multiple tokens, see \aftergroup` a list of tokens.