# Custom environment in Latex that can be hidden

I was writing lecture notes for a class and wanted to provide the students with a version of the lecture notes that they can fill in. In particular, I wanted to have some examples, where I can show the question, but not the derivation. So I created a new environment called ‘example’. I also added in a counter so that the examples would be numbered and can be referenced (with the usual \ref{}).

I had to include these packages:

\usepackage{comment}
\usepackage{ifthen}
\usepackage{color}


The counter is defined by:

\newcounter{example}
\def\theexample{\thechapter-\arabic{example}}


so example is the counter, but the label will reference things by chapter number and example.

To choose whether to show examples:

\newboolean{showexamples}
\setboolean{showexamples}{false} % set this to true or false


The environment is as follows:

\ifthenelse{\boolean{showexamples}}{%
\newenvironment{example}[3]
{\refstepcounter{example} \clearpage\vspace{1ex}\hrule\vspace*{1ex}\noindent EXAMPLE \theexample: #2\\ #3 \\}
{\vspace{1ex}\hrule\vspace{1ex}}
}
{%
\newenvironment{example}[3]
{\refstepcounter{example} \clearpage\vspace{1ex}\hrule\vspace*{1ex}\noindent EXAMPLE \theexample: #2\\ #3 \vspace*{\dimexpr#1}\begingroup\color{white}}
{\endgroup\vspace{1ex}\hrule\vspace{1ex}}
}


It takes 3 arguments: a padding size, a description of the example, and a figure to place for the example (optional). If one chooses to show the example, it shows all the body in the example environment. If one chooses to hide the example, it only shows the title, figure, some blank space, and some additional blank space (determined by the padding). The way the body is ‘hidden’ is by setting the text color to white.

I also defined the following environment for a captioned figure:

\newenvironment{capfig}[2]{\begin{figure}[!h]\center\includegraphics[width=0.5\textwidth]{#1}\caption{#2}\end{figure}}{}


it takes 2 arguments (the filename with the figure and the caption).

Here is an example use:

\begin{example}{0pt}{The Compound Pendulum is a typical example of a simple system that is awkward to describe using vectorial analysis, but straightforward using analytic mechanics.}{\capfig{figures/CompoundPenduluum.png}{Vectorial analysis of the compound pendulum}}
In order to describe the motion of the two masses as a function of time, we start by defining an origin, and label the coordinates of mass $i$ with $x_i$ and $y_i$. We wish to find the functions $x_i(t)$ and $y_i(t)$ given initial conditions on the positions and velocities of the masses. We have 6 unknowns (the accelerations in $x$ and $y$ of the two masses, and the two string tensions).
\begin{align*}
m_1\vec{a}_1&=\vec{T}_1+\vec{T}_2+m_1\vec{g}\\
m_2\vec{a}_2&=\vec{T}_2+m_2\vec{g}\\
\end{align*}
Constraints from in-extensible strings:
\begin{align*}
m_1g\cos{\theta}+T_2\cos{\phi}\cos{\theta}&=T_1\\
m_2g\cos{\phi}&=T_2\\
\end{align*}
The angles are given by:
\begin{align*}
\cos{\theta}&=\frac{x_1}{L_1}\\
\cos{\phi}&=\frac{x_2-x_1}{L_2}\\
\end{align*}
\label{ex:vectorCompPend}
\end{example}