# Showing/Hiding verbatim environment based on boolean condition

I would like to show and hide a verbatim or spverbatim environment based on a boolean condition (sth. like \showenv). I tried the solutions proposed at Hide custom environment content based on boolean, e.g. when using

\usepackage{environ}
\NewEnviron{myenv}{
\ifshow
\spverbatim{\BODY}
\fi}

...

\begin{myenv}
Test 1
Test 2
\end{myenv}


I get the following errors:

! Undefined control sequence.
\env@myenv@process -> \ifshow
! Extra \else.
! Extra \fi.
! Paragraph ended before \@tempa was complete.
! Paragraph ended before \verbatim@test was complete.

• You can't use \NewEnviron for processing verbatim material. There are already similar questions on the site. – egreg Sep 1 '14 at 16:49
• I tried some proposed solutions but they either don't work (e.g. the first one from tex.stackexchange.com/questions/69009) or I get the same or similar errors (the second one). – problemzebra Sep 2 '14 at 8:18

I think the comment-environment of the verbatim package is what you are searching for. Here is an example how to define myenv according to your requirements.

\documentclass{article}

\usepackage{verbatim}

\newif\ifshow

\newenvironment{myenv}%
{\ifshow\expandafter\verbatim\else\expandafter\comment\fi}%
{\ifshow\expandafter\endverbatim\else\expandafter\endcomment\fi}

\begin{document}

\showtrue
\begin{myenv}
Test 1
% some verbatim text
\end{myenv}

\showfalse
\begin{myenv}
Test 2
% some verbatim text
\end{myenv}
\end{document}