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

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.


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

Tags:

Formatting