# defining a global flag to conditionally remove text

Apologies in advance. I deleted many features for my mwe, but I find LaTeX so hard to debug that the act of cutting out creates more errors and triggers more questions so I'm keeping it as it is at the moment.

I am creating course notes containing text for students and text for teacher about the pedagogy. I want to create two versions of the text, the teacher version and the student version. The teacher version could also have answers for exercises.

I created mwepreamble.tex to be included by all files of this pattern, with all the repetitive definitions, and mwe.tex with specific example text.

In the preamble, I define a boolean variable teach, and attempt to define a new environment teacher, so that if I surround a block of text by

\begin{teachr} ... \end{teachr}


the text should disappear whenever the teach variable is false. I try and get the error:

l.15 \begin{teachr}
! LaTeX Error: \begin{document} ended by \end{teachr}.


mwepreamble.tex:

\documentclass[12pt]{book}
\usepackage[titles]{tocloft}
\setcounter{tocdepth}{0}
\setlength{\cftbeforechapskip}{0pt}

\parindent0pt  \parskip10pt     % make block paragraphs
\raggedright                                % do not right justify
\usepackage{mathptmx}   % times roman, including math (where possible)
\usepackage{mathpazo}   % palatino, including math (where possible)
\usepackage{helvet}     % helvetica
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[usenames,dvipsnames]{color}
\usepackage{amsthm}
\usepackage{graphicx}
\usepackage{etoolbox}
%\usepackage{comment}
%\usepackage{etoc}
\usepackage{listings}
\usepackage{grffile}
\usepackage{listing}
\usepackage{changepage}
\usepackage{multirow}
\usepackage{outlines}
\usepackage{enumitem}
\usepackage{fancyhdr}
%\usepackage{subfigure}
\usepackage{textcomp}
\usepackage{collcell}
\usepackage{tabu}
\usepackage{titlesec}
\usepackage{environ}
\definecolor{DeepPink}{rgb}{0.8,0,0.4}
\definecolor{DarkRed}{rgb}{0.5,0,0}
\definecolor{DarkBlue}{rgb}{0,0,0.5}
\titleformat{\chapter}
{\normalfont\LARGE\bfseries\color{DarkBlue}}{\thechapter.}{1em}{}
\usepackage[top=0.5in, bottom=1in, left=1in, right=1in]{geometry}
\usepackage{cleveref}

\rfoot{\thepage}
\graphicspath{ {img/} }
\pagestyle{fancy}
\definecolor{lgray}{rgb}{0.9,0.9,0.9}
\definecolor{white}{rgb}{1,1,1}
\definecolor{dkgreen}{rgb}{0,.5,0.1}
\definecolor{wrongcolor}{rgb}{.9,.5,.4}
\definecolor{purple}{rgb}{0.6,0,0.6}

\lstset{
backgroundcolor=\color{lgray},
%   basicstyle=\footnotesize \ttfamily \color{black} \bfseries,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
deletekeywords={...},
escapeinside={\%*}{*)},
frame=single,
keywordstyle=\textbf,
morekeywords={BRIEFDescriptorConfig,string,TiXmlNode,DetectorDescriptorConfigContainer,istringstream,cerr,exit},
identifierstyle=\color{black},
stringstyle=\color{blue},
language=Java,
numbers=right,
numbersep=5pt,
numberstyle=\tiny\color{black},
rulecolor=\color{black},
showspaces=false,
showstringspaces=false,
showtabs=false,
stepnumber=1,
tabsize=5,
title=\lstname,
}

\newcommand{\java}{\begin{lstlisting}[language=Java]}
\newcommand{\cpp}{\begin{lstlisting}[language=C++]}

\newcommand{\qqq}{\section{Questions}\begin{enumerate}}
\newcommand{\exer}{\end{enumerate}\section{Exercises}\begin{enumerate}}
\def\endexer#1{\end{enumerate}%
\ifstrempty{#1}{%
}{%
\href{\quizzes/#1}{Take the quiz}}
}%
\makeatletter
\newcounter{lab}% also defines \thelab
% roughly equivalent to \chapter
\refstepcounter{lab}%
\if@openright\cleardoublepage\else\clearpage\fi%
{\hbox to .65in{Lab}\protect\numberline{\thelab}#1}{\thepage}}%
\global\@topnum\z@% page number to bottom?
\noindent%
\@lab{#1}% fixed first character separation
}

% roughly equivalent to \@chapter
\if@twocolumn%
\@topnewpage[{\huge\bfseries Lab \thelab: #1\par}]%
\else{\huge\bfseries Lab \thelab: #1\par}%
\fi}

\newcommand{\ohm}{$\Omega$ }
\newcommand{\kohm}{k$\Omega$ }

\newtoggle{teach}
\toggletrue{teach}
\NewEnviron{teachr}
{\iftoggle{teach}{\BODY}{}}

\setenumerate[1]{label=\arabic*.}
\setenumerate[2]{label=\Alph*.}
\setenumerate[3]{label=\roman*.}
\setenumerate[4]{label=\alph*.}


mwe.tex

\input{mwepreamble.tex}

\begin{document}                        % End of preamble, start of text.

\chapter{Introduction}
\begin{teachr}
These notes are just for teachers and should only display when printing the teacher version.
\end{teachr}

Introductory Text

\begin{itemize}
\item Things you will need to do.
\item Yet another bullet point.
\item Safety third!
\item Tired of working? Hold a meeting! Meetings, the alternative to work.
\end{itemize}

\chapter{Safety Warning}
Before starting your kindergarten course in radioactive materials, here are a few things you should know

\begin{itemize}
\item Don't chew on the radium pacifiers.
\begin{itemize}
\item We know they are sugar coated and really tasty, but control yourself!
\end{itemize}

\item Don't put too many plutonium blocks into a big pile -- it's dangerous!
\end{itemize}

\begin{teachr}
\chapter{Teachers Notes on Engineering Competitions}

Text. text. text....
\end{teachr}

\end{document}


Your definition of the teachr environment is incorrect. You're probably after something like this:

\usepackage{environ}
\NewEnviron{teachr}
{\ifthenelse{\boolean{teach}}{\BODY}{}}


The above uses the environ package to define teachr and condition on the teach boolean. If it's set to true, \BODY is set, otherwise teachr gobbles its entire contents. One reference might be Suppressing remarks when not needed.

This is what a minimal example would look like in your instance:

\documentclass{book}

\usepackage{etoolbox,environ}

\newtoggle{teach}
\toggletrue{teach}
\NewEnviron{teachr}
{\iftoggle{teach}{\BODY}{}}

\begin{document}                        % End of preamble, start of text.

\chapter{Introduction}
\begin{teachr}
These notes are just for teachers and should only display when printing the teacher version.
\end{teachr}

Introductory Text

\begin{itemize}
\item Things you will need to do.
\item Yet another bullet point.
\item Safety third!
\item Tired of working? Hold a meeting! Meetings, the alternative to work.
\end{itemize}

\chapter{Safety Warning}
Before starting your kindergarten course in radioactive materials, here are a few things you should know

\begin{itemize}
\item Don't chew on the radium pacifiers.
\begin{itemize}
\item We know they are sugar coated and really tasty, but control yourself!
\end{itemize}

\item Don't put too many plutonium blocks into a big pile -- it's dangerous!
\end{itemize}

\begin{teachr}
\chapter{Teachers Notes on Engineering Competitions}

Text. text. text....
\end{teachr}

\end{document}


I've removed most packages that doesn't relate to the question and updated the use of conditionals to that supported by etoolbox using "toggles". See Why is the ifthen package obsolete? for a motivation.

You've also included the comment package that can be used to selectively include/exclude environments rather than using environ. See How to exclude text portions by simply setting a variable or option? or Commenting out large sections.

• The toggle works great. Only remaining question is how to externally define the toggle true or false in order to print out both a teachers and student edition. – Dov May 6 '16 at 14:00
• @Dov: What do you mean by "externally define"? – Werner May 6 '16 at 14:59
• @Werner -- i'd interpret that as meaning, for one possibility, if latex is run from the command line, how to specify which version from the command line, without having to change the file itself. (for myself, i'd just have two different wrapper files, so that the outputs would get different names.) – barbara beeton May 6 '16 at 15:08
• pdflatex -Dteach=true ... pdflatex -Dteach=false ... generate two documents, one with teacher comments, one without, in a script or makefile – Dov May 11 '16 at 3:57
• This solution didn't work for me (I had errors), but this one worked: tex.stackexchange.com/a/45704/27733. TL;DR: \usepackage{environ} \newif\ifteacher \NewEnviron{me}{\ifteacher\BODY\fi} \teachertrue – Basj Oct 19 '18 at 8:23