2

I'm learning some formulas and in order to hide them (=print white) within my pdf-document using fcolorbox. I tried to create some macro, here's my mwe:

\documentclass[a4paper]{scrartcl} \nonstopmode
\usepackage{commath}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{calc}
\usepackage{mathtools}
\usepackage{xcolor}
\newcommand\f[2]{
%    \text{#1: } & #2 \\ % works fine
    \text{#1: } & \fcolorbox{white}{white}{#2} \\ % won't work
}
\begin{document}
    \begin{align*}
        \f{Pythagoras}{a^2 + b^2 &=& c^2}
        \f{Plank Equation}{E &=& \hbar\,\omega}
    \end{align*}
\end{document}

You can see I indicated two lines with comments. Why does the first line produce the expected output, but the second one yields errors? Namely: Missing $ inserted.

Is there a better command to colorize/hide equations? I tried \phantom{} which works fine but only without ampersands…

AnatraIlDuck
  • 770
  • 1
  • 8
  • 19
  • \fcolorbox saves the contents in an extra box and so the ampersand is handled as a special token which has to escaped. – Marco Daniel Aug 17 '13 at 06:51
  • removing the ampersands did not help – AnatraIlDuck Aug 17 '13 at 07:03
  • You have to change "#2" into "$#2$" or "\ensuremath{#2}" in the defintion of the macro \f and "&=&" into " = " on two places, where you use this macro. Then your document compiles. But the equation is not hidden. It is simply printed on white background in a rectangle with white boundary. To hide some content, try ocg-p package. – robert.marik.cz Aug 17 '13 at 07:20
  • If you don't use ampersands, \newcommand\f[2]{\text{#1: } & \color{white}#2 \\} works as well. – karlkoeller Aug 17 '13 at 07:21
  • thank you for your hints, but I'd prefer to keep the ampersands, as the documents contains many equations and I want to switch them "on" and "off" on purpose – AnatraIlDuck Aug 17 '13 at 07:31
  • 2
    http://tex.stackexchange.com/questions/13681/highlight-an-equation-within-an-align-environment/13693#13693 is perhaps of interest. – Torbjørn T. Aug 17 '13 at 07:53
  • Did you think about using the ifthen package? \ifthenelse{show_equations}{$e=mc^2$}{} – Thruston Aug 17 '13 at 13:11

2 Answers2

4

You could use ifthen instead of fiddling with the colour.

\documentclass[a4paper]{article}
\usepackage{ifthen}
\usepackage{amsmath}
\begin{document}
\newboolean{showeq}
\setboolean{showeq}{true}  % <-- comment this out to hide the equations
\begin{align*}
    \text{Pythagoras:}     && \ifthenelse{\boolean{showeq}}{a^2+b^2 &=c^2}{} \\
    \text{Plank equation:} && \ifthenelse{\boolean{showeq}}{E &= \hbar\,\omega}{}
\end{align*}
\end{document}
ShreevatsaR
  • 45,428
  • 10
  • 117
  • 149
Thruston
  • 42,268
1

You have two problems with your code for \f:

  1. \fcolorbox stores its contents in a box, which switches the mode back to text (from math). As elements like a^2 can only be used in math mode, this causes the error (missing $ inserted). If you're using \fcolorbox, you need to switch back to math mode explicitly: \fcolorbox{.}{.}{$..$}.
  2. Since \fcolorbox boxes its contents, it has no reference on using the symbol &/its hidden from its intended meaning - typically used for alignment inside tabular or align (and friends). So, just drop it in this case.

While using single-character macros may be convenient, it's advised to use something a little more describing (also because some core macros could be single-characters themselves). Here's a revised version of your input:

enter image description here

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\newif\ifshoweq\showeqtrue
\newcommand{\showeq}[2]{%
  \text{#1: } & \ifshoweq #2\else\phantom{#2}\fi
}
\begin{document}
Equations are visible:
\begin{align*}
  \showeq{Pythagoras}{a^2 + b^2 = c^2} \\
  \showeq{Plank Equation}{E = \hbar\,\omega}
\end{align*}

\showeqfalse

Equations are invisible:
\begin{align*}
  \showeq{Pythagoras}{a^2 + b^2 = c^2} \\
  \showeq{Plank Equation}{E = \hbar\,\omega}
\end{align*}
\end{document}

\showeq{<name>}{<eqn>} prints <name> in text mode, right aligned, and left aligns <eqn>. If the boolean variable \ifshoweq is set to \showeqfalse (default is to show equations/\showeqtrue), then it sets the equation as a \phantom. This provides the correct spacing of the equation, without actually printing it (also, no need for using "white" as a colour).

David Carlisle
  • 757,742
Werner
  • 603,163
  • thank you for your helpful and explaining answer! Unfortunately I can only accept one answer, and I accepted already one :-( – AnatraIlDuck Aug 21 '13 at 08:07