Tag Archives: latex

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:


The counter is defined by:


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

To choose whether to show examples:

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

The environment is as follows:

  {\refstepcounter{example} \clearpage\vspace{1ex}\hrule\vspace*{1ex}\noindent EXAMPLE \theexample: #2\\ #3 \\}
  {\refstepcounter{example} \clearpage\vspace{1ex}\hrule\vspace*{1ex}\noindent EXAMPLE \theexample: #2\\ #3 \vspace*{\dimexpr#1}\begingroup\color{white}}

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:


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).
Constraints from in-extensible strings:
The angles are given by: