I have used a variety of commands to hide answers to questions in documents. I use a boolean to to determine whether the material should be hidden, i.e. hideans True then hide, hideans False then don't.
\newbool{hideans} % True: Hide False: Show
Usually, the hiding commands themselves work by something along the lines of turning the text white so it is invisible when printed:
\newcommand{\ans}[1]{%
\ifbool{hideans}{\textcolor{white}{#1}}{#1}
}
Other versions use \phantom{} or similar approaches. The advantage is that the commands above keep the exact amount of whitespace that I would like to leave for people to put their answer.
The downside of these commands is that they not work well with line breaks, they do not work with figures and tables and can be fickle with equations. To get around this, I created a separate command for each of: figures, tables, inline equations, line breaks, etc.. Even then from time to time, I have to manually code in the whitespace. So my questions is...
Question. Is there a way of having LaTeX measure the size of the text, figures, equations, etc. enclosed by braces so that, as the command above, if a given boolean is true will leave that amount of whitespace between it and the next text and if the boolean is false print the material enclosed in the braces? Essentially, a 'command' like the one below
\ans{\ifbool{Hide?}{
% Yes
Measure size of figures, eqs, tables here and leave that amount of whitespace
}{
% No
Then print the stuff in the braces before
}
EDIT. As an example was requested, here is what I mean.
Question 1: What is 2 x 2?
[[[[Solution: Well, recall the following figure from Quantum Mechanics:
\begin{figure}[!ht]
\centering
\includegraphics[width = \textwidth}{theimage.png}
\end{figure}
This figure has nothing to do with the problem. But it's cute! \par
Now construct a figure of 1s representing the multiplication.
\begin{table}[!ht]
\centering
\begin{tabular}{cccc}
1 & 1 \\
1 & 1
\end{tabular}
\end{table}
So that we quickly see that $2 \times 2$ is
\[
2 \times 2= 4.
\]
]]]]
What I am hoping for is something in LaTeX that will measure the amount of space that the material in the [[[[....]]]] consumes and if the boolean is set to True, hides it but leaves whitespace the size of what the text would have taken up, and if the boolean is False, just prints what I had typed for the solution.

\begin{hidden}and\end{hidden}is covered by a white foreground provided a, say, booleanhideis settrue. The question is whether you want something of that sort. (Since these are overlay pictures, switching the foreground on and off won't change the document layout.) – May 30 '19 at 03:01