5

Sometimes I need to strikeout the code I wrote which has turned out to be wrong, like this:

enter image description here

By this way, it can remind me intuitively that code struck out does not work and to learn from the mistake here. However, I found to strike out a code, I have to click Format->Font-Strikeout to reach the interface and check the strikeout box, which is not convenient at all.

enter image description here

What I want here is to define a short cut like "Alt+S", so that each time I need to strick out a code or text, I only have to select the target code or text and press down the shortcut, which is much more efficient than the current way I have to follow.

Any idea how to achieve this?

AlbertLew
  • 483
  • 2
  • 7
  • There is CTRL+U shortcut for underline. I think it does not matter in your case whether it is strikeout or underline. – azerbajdzan May 01 '22 at 13:38
  • 1
    @azerbajdzan. Unfortunately, it does. In my notebooks, underline is used for something correct but easy to forget, while strikeout is used for something incorrect and easy to repeat if not rectified. – AlbertLew May 01 '22 at 13:53
  • 1
    I would not use strikeout nor underline. It is hard to read such texts. I would use change of background color of particular cell. Here https://mathematica.stackexchange.com/questions/47474/create-custom-shortcut-to-change-font-color you can find how to add your own menu item with shortcut. – azerbajdzan May 01 '22 at 13:58
  • I know it can be annoying to get suggestions that bypass what you specifically asked for, but this does seem like a very awkward way to achieve your main objective, which seems to be to highlight "wrong" code as some form of documentation. You can use section headings to group "wrong" cells together. This has the added benefit that you can hide them when you're not analyzing your mistakes. You can use background color or cell tags. – lericr May 01 '22 at 15:29
  • If you do still want strikethrough and you figure out the menu/shortcut stuff, you could probably make a function associated to the menu command that adds FontVariations->{"StrikeThrough"->True} to the cell options. – lericr May 01 '22 at 15:30
  • Thanks @lericr. I will give your suggestions careful thought. – AlbertLew May 03 '22 at 13:49
  • Thanks @azerbajdzan. I think your point of change of background colors to high light those cells is brilliant, I might have a try in the days to come. – AlbertLew May 03 '22 at 13:53

1 Answers1

5

You can modify the menu system to add this, e.g.:

FrontEndExecute @ FrontEnd`AddMenuCommands[
    "PlainFont",
    {MenuItem[
        "Strikethrough", 
        FrontEnd`FontVariationsStrikeThrough -> Toggle,
        System`MenuKey["u", System`Modifiers -> {"Command"}]
    ]}
]

This is for Mac. After doing this, you should be able to highlight code and use Command-u to strikethough text.

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
  • 1
    Nice! I can't seem to find any documentation on this Toggle option for frontendtokens. Can this Toggle option be used to turn on and off cell brackets for an entire notebook instead of just the current cell? – B flat May 01 '22 at 23:14
  • Thanks a lot Carl! My computer is a Win 10 but your code still works like a charm. And your solution does not even require modifying the initial files which I expected from previous experience. – AlbertLew May 03 '22 at 13:45