1

I have a custom environment \newenvironment{mathsolution} that contains content that I want to hide based on the switch \showmathtrue and \showmathfalse. The code I have for this conditional is the following:

\newenvironment{mathsolution}{
\setbox0\vbox\bgroup} % store solution in box 0
{\egroup              % end of box
\ifshowmath
    \par\box0         % display box
\else
    \vskip\ht0        % whitespace of height of box
\fi
}

I am attempting to keep the exact whitespace of the box when the content is hidden but this is not happening.

Example of the problem

Notice in the image below, the first math solution spacing is close but not quite exact. In the second problem, I am using an adjustbox and a minipage and all of the whitespace is ignored.

enter image description here

What I Need

enter image description here

MWE

\documentclass{book}
\usepackage[margin=1.00in,showframe]{geometry}
\usepackage{amsmath,amssymb,amsfonts}    % Equations
\usepackage{adjustbox}

% LONG (MATH) SOLUTIONS
\newif\ifshowmath     % use \newif to define switches
\newenvironment{mathsolution}{
\setbox0\vbox\bgroup} % store solution in box 0
{\egroup              % end of box
\ifshowmath
    \par\box0         % display box
\else
    \vskip\ht0        % whitespace of height of box
\fi
}

%\showmathtrue
\showmathfalse

\begin{document}

Some math problem stated here.
\begin{mathsolution}
$$ 1.0\times 10^{1} \left ( \dfrac{1}{10^{1}} \right ) = 1 $$
\end{mathsolution}

Some other math problem stated here.
\begin{mathsolution}
\adjustbox{valign=t}{\begin{minipage}{0.43\linewidth}
\begin{align*}
x_1 &= 1.000  \\
y_1 &= \left ( \dfrac{1}{2}\right )   \left ( a - b \right ) \\
    &= 2.000 \\
x_2 &=~? \\
y_2 &= \left ( \dfrac{1}{2}\right )   \left ( b - a \right ) \\
    &= 2.000 \\
\end{align*}
\end{minipage}
}
\hfill
\adjustbox{valign=t}{\begin{minipage}{0.55\linewidth}
\begin{align*}
\dfrac{c}{d} = \dfrac{e}{f}  \longrightarrow
c   &= \dfrac{d\times e}{f} \\
    &= 3.000
\end{align*}
\end{minipage}
}
\end{mathsolution}

Some text.

\end{document}

Note: Changing the text to white is not an option.

  • Don't forget to include \dp0. A second \vskip is actually faster than \dimexpr \ht0+\dp0\relax. – John Kormylo Jun 7 at 18:51
2

This modification of your MWE fixes the problem:

\usepackage{environ}
\newif\ifshowmath     % use \newif to define switches
\newsavebox{\mybox}
\NewEnviron{mathsolution}{%
\sbox{\mybox}{\begin{minipage}{\textwidth}\BODY\end{minipage}}%
}[\ifshowmath\par\usebox{\mybox}%
\else\par\begin{minipage}{\textwidth}\vspace*{\dimexpr\ht\mybox+\dp\mybox\relax}\end{minipage}\fi]

As you can see:

  • I used the environ package, which makes this kinf of job more easy
  • I didn't used adjustbox, which IMO is not needed here
  • I changed your use of TeX primitive to LaTeX commands enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.