# Is there a way to diagonally strike through a character or group of characters in a DisplayForm equation?

In mathematical derivations, one often diagonally strikes through a variable, term, or factor -- either to indicate that the variable/term is zero/negligible, or that it is being cancelled with something else. (In the former case, one often puts a zero at the top right end of the strikethrough.)

Here's an example using TeX (from https://tex.stackexchange.com/a/40254):

Is there a way to achieve this in Mathematica DisplayForm equations? I thought Mma could handle pretty much every mathematical notation on the planet, but I can't find anything about this.

• I think you'll have to use Graphics in one way or another for that. Mathematica isn't as flexible for typesetting as $\LaTeX$ is... but it does allow combining graphics and other objects. However, it seems you're looking for a Graphics-free solution, and I don't think there is one. – Jens Apr 12 '16 at 2:25
• For a single character, you can perhaps get by with this: How to use Feynman Slash notation? – Jens Apr 12 '16 at 3:58

Here is a semi-manual way of doing the kind of decoration you're asking for, based on Overlay:

strikeThrough[obj_, {width_, height_}] :=
Overlay[{obj,
Graphics[Line[{{0, 0}, {width, height}}],
PlotRange -> {{0, width}, {0, height}},
ImageSize -> {width, height}]}]

strikeThrough[123456, {45, 12}]


The first argument of strikeThrough is entered as an Inline Cell (i.e., with Ctrl-9). The second argument is the width and height of the box that is supposed to be crossed by the diagonal line. This has to be entered manually. The output of this function is an Overlay that you can now copy. I then started a new display formula and pasted the overlay into it. As you can see, it can be placed anywhere, e.g., in a fraction.

You may also want to add PlotRangePadding -> 1 to the Graphics above, to avoid having the line appear cut off.

Update: palette version

This creates a palette which can be clicked after highlighting the expression to be struck. It inserts the dimensions of the expression by estimating it from a rasterized copy.

CreatePalette[{Button["Strike it!",
NotebookWrite[InputNotebook[],
OverlayBox[{#, Module[{width, height},
{width, height} =
ImageDimensions[Rasterize[DisplayForm[#]]];
ToBoxes@Graphics[Line[{{0, 0}, {width, height}}],
PlotRange -> {{0, width}, {0, height}},
ImageSize -> {width, height}]]}] &[


With this, you don't have to manually enter the size of the strike-out box, and the replacement is made in-place without the need to copy and paste.

• +1 This is the first time I've seen Overlay used that I like. Usually, it just weirds me out that it's ostensibly a combination of Graphics objects, but I can't select it like a Graphics object (no orange box around it when you click on it, no ability to resize interactively, etc). But here you can type 4 + strikeThrough[123456, {45, 12}]/2 and it displays nicely. – Jason B. Apr 12 '16 at 7:29
• Hmm… Is there any way to make the function detect the height and width of the box, to avoid a lot of guessing-and-trying? – ibeatty Apr 14 '16 at 17:24
• This seems to accomplish the goal, but it's a bit too slow and clumsy to use for regular typesetting. I write up extensive solutions to physics problem sets, with lots of equations, so efficiency of user effort is important. Can anyone elaborate on this to improve it? Perhaps make a palette button that instantly "overstrikes" a selected element of an equation, rather like the "add a vector over the top of" or "put a box around" buttons on the Basic Math Assistant palette do? – ibeatty Apr 14 '16 at 17:30
• @ibeatty I added a palette that automates things. – Jens Apr 14 '16 at 18:20
• @ibeatty Thanks - looking at your last comment, I'm glad your original answer wasn't 42 because then I wouldn't know the question... – Jens Apr 14 '16 at 20:35

Why not use Szabolcs's MaTeX package to create your display equations? It has the benefit that you can copy and paste directly from a LaTeX document.

<< MaTeX
SetOptions[
MaTeX, {"Preamble" -> {"\\usepackage{cancel}"}, FontSize -> 18}];
MaTeX["\\cancel{2x}"]
MaTeX["\\cancel{1234567890}"]


• Nice package. Didn't know about that! Though mixing struck-through terms done this way with the rest of an equation done on Mathematica seems likely to produce visual inconsistencies, and doing all equations in MaTeX gives up the benefits of using Mathematica for typesetting… – ibeatty Apr 14 '16 at 17:34
• I've got MaTeX working, but it's throwing an error on the \\cancel bit: "MaTeX::texerr: Error while running LaTeX: Undefined control sequence." This isn't the place for MaTeX help, though, so I'll persevere elsewhere. – ibeatty Apr 14 '16 at 17:36
• @ibeatty Try this exact command: MaTeX["\\cancel{2x}", "Preamble" -> {"\\usepackage{cancel}"}]`. Newer versions of MaTeX give better errors if something goes wrong. If something goes wrong, you can post a different question, and notify me separately so I'll notice it. – Szabolcs Sep 26 '16 at 11:42