# etoolbox: environment hooks in boolean switch

What did I wrong, that the following does not work here:

(Edit: I mixed up the true and false parts, but I don't want to change the code, because the answers refer to this in a way confusing one.)

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{xcolor}

\usepackage{fancyvrb}
\DefineVerbatimEnvironment{ExampleVerbatim}{Verbatim}%
{fontsize=\small,formatcom=\color{blue},commandchars=\\\{\}}

\usepackage{setspace}
\doublespacing

\usepackage{etoolbox}
\newbool{tightspace}
\setbool{tightspace}{false}
%----------------------------%
\ifbool{tightspace}{% true part empty - should do nothing
}{% false part:
\BeforeBeginEnvironment{ExampleVerbatim}{\begin{spacing}{0.9}}%
\AfterEndEnvironment{ExampleVerbatim}{\end{spacing}}%
}

\usepackage{lipsum}

\begin{document}

\lipsum[1]

\begingroup
\setbool{tightspace}{true}
\begin{ExampleVerbatim}
Because of \textcolor{black}{\itshape{\textbackslash}setbool\{tightspace\}\{true\}}
the line spacing should be very narrow ...
Don't wonder at \textcolor{red}{the missing line breaks:}
\lipsum[2]
\end{ExampleVerbatim}
\endgroup

\begin{ExampleVerbatim}
And now, with \textcolor{black}{\itshape{\textbackslash}setbool\{tightspace\}\{false\}},
it should have \textcolor{red}{the document's line stretch ...}
\lipsum[3]
\end{ExampleVerbatim}

\lipsum[4]

\end{document}


It seem's, that the boolean switch has no effect – or is there another thing I didn't realize?

-

You check the boolean switch once in the preamble and make the definition, which is not changed later.

It would work as expected if you would make the test later, such as:

\BeforeBeginEnvironment{ExampleVerbatim}{\ifbool{tightspace}{}{\begin{spacing}{0.5}}}%
\AfterEndEnvironment{ExampleVerbatim}{\ifbool{tightspace}{}{\end{spacing}}}%


However, I would make the spacing value a variable instead. And I guess you meant your tightspace check the other way round as in the original code.

-
Though I accepted your answer: Could you please explain the part with the variable a bit more (tm) detailed? And yes, you're right, I mixed up the true and false parts. –  Speravir Nov 25 '11 at 23:14

I think you you should make a small dry run.

I will try it:

1. You define a new bool tightspace -- no problem
2. You set the new bool tightspace to false -- no problem This step isn't required because newbool set the bool to false.
3. You make a condition -- here is the problem. The condition is false, so at the beginning of the environment ExampleVerbatim you set the spacing environment for the whole document.
-
Point 2: I didn't know, thanks. (As Stephan correctly remarked - in my original code I had it the other way round; I checked the MWE, too, but as it didn't work I could not notice my error.) To Point 3: That is what I didn't realize (and I think you should write something more comprehensive). I change the condition, before the environment begins, but important is the moment when the hook is defined! (BTW: 3 German tongues talk together in English, and mine is almost broken, because my English is quite poor …) –  Speravir Nov 25 '11 at 23:30