How can I strikeout words in math mode (similar to \sout
with \usepackage{ulem}
in text mode)? I've tried \usepackage{cancel}
and \(\cancel{1+1=2}\)
but that draws a diagonal line.
4 Answers
Use something like
\sout{$a^2+b^2=c^2$}
or
\[
\hbox{\sout{$a^2+b^2$}}=c^2
\]
And you can of course define a command for convenience.
version 2 (fine remark from Martin)
\documentclass{scrartcl}
\usepackage{xcolor,cancel}
\newcommand\hcancel[2][black]{\setbox0=\hbox{$#2$}%
\rlap{\raisebox{.45\ht0}{\textcolor{#1}{\rule{\wd0}{1pt}}}}#2}
\begin{document}
\[
x+\hcancel[red]{ \sum_a \mathcal{D}^a\mathcal{D}_a}=2x+ \sum_a \mathcal{D}^a\mathcal{D}_a+3
\]
\end{document}
-
1
\hbox{#2}
will typeset#2
in textmode, which is fine withy
(except maybe a slightly wrong width), but will break horrible with real math expressions. Jun 13, 2011 at 12:44 -
-
-
-
I removed the
$$
from the\hbox
and included them in the parameter (e.g.,\hcancel{$2+2=4$}
and it worked fine. Thanks!– cxwDec 30, 2016 at 21:26
I'm using this hack with the ulem
package in the preamble.
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\newcommand{\stkout}[1]{\ifmmode\text{\sout{\ensuremath{#1}}}\else\sout{#1}\fi}
In the document, I use the command thus defined, \stkout
Lorem \stkout{ipsum}. $x + \stkout{y + z} = 1$.
\begin{equation}
A + \stkout{B + C + D} = E.
\end{equation}
Reference: Opposite of \ensuremath: ensure that I'm *not* in math mode?
-
3If find the
cancel
package much more practical, but your method is anyway interesting :) Jul 7, 2016 at 13:20 -
1I quite like this approach, but feel that it would work better by just patching this to
\sout
- you can do this with\let\isout\sout
\renewcommand{\sout}[1]{\ifmmode\text{\isout{\ensuremath{#1}}}\else\isout{#1}\fi}
in the premable of your document. Requires amsmath and ulem Apr 29, 2020 at 20:31 -
Calling the ulem package seems to have unintended consequences for my bibliography. :(.– TillJul 18, 2022 at 9:24
-
Why does this preserve italics even though the math is inside \text?– sturgmanSep 2, 2022 at 13:11
Works in math and text mode
\documentclass{article}
\usepackage{calc}
\newsavebox\CBox
\newcommand\hcancel[2][0.5pt]{%
\ifmmode\sbox\CBox{$#2$}\else\sbox\CBox{#2}\fi%
\makebox[0pt][l]{\usebox\CBox}%
\rule[0.5\ht\CBox-#1/2]{\wd\CBox}{#1}}
\begin{document}
\hcancel{Test}
\hcancel[2pt]{Test}
\end{document}
-
1Indeed, but you need to restart the mathmode, don't you? E.g. `$x+\hcancel{$y$}+z$. Jun 13, 2011 at 12:43