0

I want to be able to switch off (i.e. hide) blocks of math within align via a switch and have it replaced with white space. I've found numerous examples of how to switch on/off text and even elements within an align environment but none of those really fit what I need (or worked for me in general).

Consider the following MWE:

\documentclass{article}
\usepackage{amsmath}

% SWITCH MATH ON(1)/OFF(0)
\def\showmath{1}
\newcommand{\mathsolution}[1]{
   \ifnum\showmath=1
   #1
   \fi

   \ifnum\showmath=0
   \vspace{2\baselineskip} \hspace{2cm}
   \fi
}

\begin{document}

Some text.
\mathsolution{
\begin{align*}
y = mx+b \\
y = mx+b 
\end{align*}
}
Some text.

\end{document}

I am currently trying to fuss around with the following bit of code to do this:

\ifnum\showmath=0
\vspace{2\baselineskip} \hspace{2cm}
\fi

Here the math doesn't get printed and some whitespace is inserted. However, I want the whitespace to be roughly approximate to what the actual block of math takes up.

Note: Printing in white won't work here because it can still be copy/pasted.

1

You can store the solution in a box and display either the box or a vspace of that height:

\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
}


\begin{document}
\showmathfalse
Hidden solution:
\begin{mathsolution}
\begin{align}
   x & = 12 \\
   y & = 42
\end{align}
\end{mathsolution}

\showmathtrue

Shown solution:
\begin{mathsolution}
\begin{align}
   x & = 12 \\
   y & = 42
\end{align}
\end{mathsolution}
\end{document}

That implementation is likely not very robust especially in combination with pagebreaks.

  • Is there anyway to set the switch to a simple 0/1 parameter rather than a true/false string? So far this seems to be working for me. I'm hoping a samepage environment helps alleviate any pagebreak issues. – LordStryker May 23 '17 at 22:49
  • You can replace the \ifshowmath by \ifnum\showmath=0 (as in your MWE) in the \newenvironment and remove/replace all other occurrences of it. – chtz May 24 '17 at 6:17

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.