5
$\begingroup$

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?

$\endgroup$
7
  • $\begingroup$ There is CTRL+U shortcut for underline. I think it does not matter in your case whether it is strikeout or underline. $\endgroup$ May 1, 2022 at 13:38
  • 1
    $\begingroup$ @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. $\endgroup$
    – AlbertLew
    May 1, 2022 at 13:53
  • 1
    $\begingroup$ 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 mathematica.stackexchange.com/questions/47474/… you can find how to add your own menu item with shortcut. $\endgroup$ May 1, 2022 at 13:58
  • $\begingroup$ 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. $\endgroup$
    – lericr
    May 1, 2022 at 15:29
  • $\begingroup$ 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. $\endgroup$
    – lericr
    May 1, 2022 at 15:30

1 Answer 1

5
$\begingroup$

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.

$\endgroup$
2
  • 1
    $\begingroup$ 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? $\endgroup$
    – B flat
    May 1, 2022 at 23:14
  • $\begingroup$ 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. $\endgroup$
    – AlbertLew
    May 3, 2022 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.