In an earlier question I learned the following code to define conditionally compiled text (for a teacher guide)
\newtoggle{teach}
\toggletrue{teach}
\NewEnviron{teachr}
{\iftoggle{teach}{\BODY}{}}
\newcommand{\cpp}{\begin{lstlisting}[language=C++]}
If I want to embed code within this conditionally compiled text, it fails. Conditional text works fine.
\begin{teachr}
\cpp
class foo {};
\end{lstlisting}
\end{teachr}
The error message is: ! LaTeX Error: \begin{teachr} on input line 166 ended by \end{lstlisting}.
I assume this is some obvious failing, but if I should construct an mwe let me know and I will edit the question.


lstlistingis a verbatim content environment. This can't be used (this way). Your code is a fragment and not really useful for us in order to help you – Jun 14 '16 at 16:08listings, you need\lstnewenvironmentrather than some command-form usage. Moreover, you can't nest a listing inside another environment s the cat-codes are important. – Werner Jun 14 '16 at 16:08