4

I am trying to find a way to hide expressions inside a math environment. I found a solution, provided by Steven B. Segletes at

Hiding part of text leaving blank space

However, when I add the amsmath package, it breaks, giving me the error "Missing $ inserted". I have provided a MWE example below. If I remove \usepackage{amsmath}, it compiles. If I remove the \frac command it compiles.

\documentclass{article}

\usepackage{amsmath}

\usepackage{censor}
\usepackage{stackengine}
\usepackage{scalerel}

\censorruleheight=0ex

\makeatletter
\long\def\blackout#1{%
 \def~{-}%
 \protected@edef\save@arg{#1}%
 \expandafter\censor@Block\save@arg\stringend\let~\sv@tilde}
 \let\sv@cenword\@cenword
\newcommand\m@cenword[1]{\ThisStyle{%
\stackengine{\mcensorruledepth}{$\SavedStyle\phantom{#1}$}%
{\rule{\widthof{$\SavedStyle#1$}}{\the\censorruleheight}}{U}{c}{F}{T}{L}}}
\newcommand\mblackout[2][\dp\strutbox]{%
 \let\@cenword\m@cenword%
\def\mcensorruledepth{#1}%
\blackout{{#2}}%
\let\@cenword\sv@cenword%
}
\makeatother

\begin{document}

\[\mblackout{\frac{a}{b}} \]

\end{document}
5
  • 1
    Welcome to TeX.SX! Your code says amsmath, your text, title and tag says amsthm, I assume you meant to have amsmath everywhere? Commented Sep 21, 2014 at 14:46
  • this has nothing to do with amsmath. any math substituted in the \mblackout expression will fail. comment out amsmath and change the display line to \[ \mblackout{\alpha} \] -- that results in the same Missing $ inserted., and inserting $ signs as \[ \mblackout{$\alpha$} \] results in the error Missing } inserted. so no, there is something wrong with your definitions, which i haven't got time to debug. Commented Sep 21, 2014 at 15:28
  • Why not use \color{white}{Text to be hidden here} like one of the other solutions in Hiding part of text leaving blank space
    – 0az
    Commented Sep 21, 2014 at 15:35
  • You are right that it does not work with \alpha and most other math commands. However,it is not correct that any math substituted into \mblackout fails. It works with \frac without amsmath, but not with. I assume amsmath redefines \frac in some way? I do not want to use \color{white} (which was my original solution) because I am blanking out text on the students' version of my notes and I decided I do not want them to be able to copy the hidden text (also, I do not always have a white background).
    – smcc
    Commented Sep 21, 2014 at 15:51
  • When I try \[1+\mblackout{1\alpha}+1\], the code compiles correctly with no errors. The code does not compile correctly (and with the same error message as before) when I try \[1+\mblackout{\alpha}+1\].
    – smcc
    Commented Sep 21, 2014 at 16:48

2 Answers 2

3

With version 4.2 of the censor package, the censoring of math text is handled somewhat automatically. For example:

\documentclass{article}
\usepackage{amsmath}
\usepackage{censor}
%\censorruleheight=0ex
\begin{document}
\[y = \blackout{x = \frac{a_{1}}{b_{2}}} \cdot \frac{1}{2} \]

\[y = x = \frac{a_1}{b_2} \cdot \frac{1}{2} \]

$\beta / \blackout{\alpha} \times 3$

$\beta / \alpha \times 3$
\end{document}

yields

enter image description here

Note several points: 1) non-text macros like \frac are not censored, per se, but only the numerator and denominator text are censored (thus the vinculum shows), and 2) censored sub- and superscripts must be embraced even when a single token.

Therefore, for the current question, where the \censorruleheight can be set to 0pt, the vinculum remains:

enter image description here

ORIGINAL ANSWER

You can set the offending macros in their own group (via braces) and then they set just fine, with or without amsmath:

\documentclass{article}

\usepackage{amsmath}

\usepackage{censor}
\usepackage{stackengine}
\usepackage{scalerel}

\censorruleheight=0ex

\makeatletter
\long\def\blackout#1{%
 \def~{-}%
 \protected@edef\save@arg{#1}%
 \expandafter\censor@Block\save@arg\stringend\let~\sv@tilde}
 \let\sv@cenword\@cenword
\newcommand\m@cenword[1]{\ThisStyle{%
\stackengine{\mcensorruledepth}{$\SavedStyle\phantom{#1}$}%
{\rule{\widthof{$\SavedStyle#1$}}{\the\censorruleheight}}{U}{c}{F}{T}{L}}}
\newcommand\mblackout[2][\dp\strutbox]{%
 \let\@cenword\m@cenword%
\def\mcensorruledepth{#1}%
\blackout{{#2}}%
\let\@cenword\sv@cenword%
}
\makeatother

\begin{document}
\[y = \mblackout{x = {\frac{a}{b}}} \cdot \frac{1}{2} \]

\[y = x = \frac{a}{b} \cdot \frac{1}{2} \]

$\beta / \mblackout{{\alpha}} \times 3$

$\beta / \alpha \times 3$
\end{document}

enter image description here

4
  • Thanks. This solves the problem in my original question with \alpha and the like. However it does not work across alignment and new line characters (which is what I was hoping for). So, unless there is some way to modify the definition of \mblackout to remove this necessity, simply using \phantom as suggested by barbara beeton is preferable for blanking math, since it does not require one to put things in braces. (Although your solution does have the advantage of being easily modifiable to allow for blacking out and underlining - this flexibility may prove useful to me.)
    – smcc
    Commented Sep 24, 2014 at 10:22
  • @Steven B. Segletes, Ever since I updated to Version 4.2 of the censor package, this solution does not work for me anymore (not sure which version I used before). Do you happen to have an updated solution that works for math expressions via the new xblackout environment or with an updated \newcommand\mblackout ? Thanks for your help!
    – Raphael
    Commented Mar 6, 2023 at 16:34
  • @Raphael You shouldn't need a redefinition of \blackout and a separate \mblackout macro. The new version handles the text of math already. In fact, extra braces are not even required now: \begin{document} \[y = \blackout{x = \frac{a}{b}} \cdot \frac{1}{2} \] \[y = x = \frac{a}{b} \cdot \frac{1}{2} \] $\beta / \blackout{\alpha} \times 3$ $\beta / \alpha \times 3$ \end{document} works. Commented Mar 6, 2023 at 21:37
  • @Raphael See my updated answer, as well as the censor v4.2 package documentation (section 6). Commented Mar 6, 2023 at 21:57
3

why don't you just use \phantom{...}? \phantom never puts anything but (precisely measured) space on a page.

of course, you'll have to be careful about keeping things like & and other formatting instructions in proper synch, but it should be reasonably obvious how to do that.

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\[ abc\phantom{def}ghi \]

first, the example without \verb+\phantom+s:
\begin{align*}
 abc &= def\\
 ghi &= \frac{1}{m}jklmnopq\\
 rst &> \frac{1}{2}

\end{align*}
this even works in $abc\tfrac{1}{1+n}de$ in-line text.

now, apply \verb+\phantom+ to selected elements:
\begin{align*}
 abc &= def\\
 g\phantom{h}i &= \phantom{\frac{1}{m}}jk\phantom{lmn}opq\\
 rs\phantom{t} &> \frac{1}{2}
\end{align*}
this even works in $ab\phantom{c\tfrac{1}{1+n}}de$ in-line text.

\end{document}

output of example code

3
  • It doesn't work across the alignment character & or the new line character \\ in aligned equations. I was hoping for a solution that would (so as to not have to put the command around each part of the expression separately). Is this possible? I am not sure that even a solution with the censor package will work. I also wanted to find out what was wrong with the code from the answer to the previous question about hiding math.
    – smcc
    Commented Sep 21, 2014 at 17:01
  • you are correct that it will work only within a "cell". if you have many, many instances of this to change, then a "comment"-type approach might be worth the effort. but for just a few, you are attacking a flea with a bludgeon. sorry i can't help with debugging the code you posted, but it's really nontrivial, and i'm not a user of any of the packages you load, except for amsmath. regarding your earlier comment, yes, amsmath does redefine \frac (to add functionality). Commented Sep 21, 2014 at 17:08
  • Ideally I would like something like \visible in Beamer (which does work almost everywhere: across alignment characters, figures and tables, around list environments). But the way it is done there (I have not yet looked into how the overlay commands are defined) may only work due to things being put in frames.
    – smcc
    Commented Sep 21, 2014 at 17:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .