9
$\begingroup$

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): strikethrough examples

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.

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Jens Apr 12 '16 at 2:25
  • $\begingroup$ For a single character, you can perhaps get by with this: How to use Feynman Slash notation? $\endgroup$ – Jens Apr 12 '16 at 3:58
13
$\begingroup$

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

screenshot

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}}], 
            PlotRangePadding -> 1, 
            PlotRange -> {{0, width}, {0, height}}, 
            ImageSize -> {width, height}]]}] &[
      NotebookRead[InputNotebook[]]]]]}];

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.

$\endgroup$
  • $\begingroup$ +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. $\endgroup$ – Jason B. Apr 12 '16 at 7:29
  • 1
    $\begingroup$ 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? $\endgroup$ – ibeatty Apr 14 '16 at 17:24
  • $\begingroup$ 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? $\endgroup$ – ibeatty Apr 14 '16 at 17:30
  • $\begingroup$ @ibeatty I added a palette that automates things. $\endgroup$ – Jens Apr 14 '16 at 18:20
  • 1
    $\begingroup$ @ibeatty Thanks - looking at your last comment, I'm glad your original answer wasn't 42 because then I wouldn't know the question... $\endgroup$ – Jens Apr 14 '16 at 20:35
10
$\begingroup$

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}"]

enter image description here

$\endgroup$
  • $\begingroup$ 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… $\endgroup$ – ibeatty Apr 14 '16 at 17:34
  • $\begingroup$ 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. $\endgroup$ – ibeatty Apr 14 '16 at 17:36
  • $\begingroup$ @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. $\endgroup$ – Szabolcs Sep 26 '16 at 11:42

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.