2
$\begingroup$

The mathematical notation for the magnitude of a vector is $\vert x \vert$, but this does not work:

∥ {1, 2, 3} ∥

Is there any way to overload the DoubleVerticalBar symbol so my Mathematica code would look like a textbook formula?

$\endgroup$
2
  • $\begingroup$ Please try Norm. $\endgroup$ Commented Nov 11, 2020 at 11:30
  • $\begingroup$ @ΑλέξανδροςΖεγγ I know about Norm, but I would like to use a "nicer" notation for it $\endgroup$
    – kungfooman
    Commented Nov 11, 2020 at 11:32

2 Answers 2

8
$\begingroup$

If you only want to have the "nice" format for input, you could assign Norm to DoubleBracketingBar (which is equivalent to \[LeftDoubleBracketingBar] # \[RightDoubleBracketingBar] &)

DoubleBracketingBar := Norm
DoubleBracketingBar[x]
(* Norm[x] *)

Alternatively, and more usable, you could use the Notation package

Needs["Notation`"]
Notation[ParsedBoxWrapper[
RowBox[{"DoubleBracketingBar", "[", "x_", 
     "]"}]] \[DoubleLongLeftRightArrow] ParsedBoxWrapper[
RowBox[{"Norm", "[", "x_", "]"}]]]
Notation[ParsedBoxWrapper[
RowBox[{"\[LeftDoubleBracketingBar]", "x_", 
     "\[RightDoubleBracketingBar]"}]] \[DoubleLongLeftRightArrow] 
  ParsedBoxWrapper[
RowBox[{"Norm", "[", "x_", "]"}]]]

Which is displayed more readable as

The way the input is displayed in the notebook

The usage is then as expected:

DoubleBracketingBar[x] // FullForm
(* Norm[x] *)
\[LeftDoubleBracketingBar]x\[RightDoubleBracketingBar] \
// FullForm
(* Norm[x] *)
Norm[x]
(* \[LeftDoubleBracketingBar]x\[RightDoubleBracketingBar] *)

Edit: By the way, the reason I did not use \[DoubleVerticalBar] is because it is built in as an infix operator.

$\endgroup$
2
  • $\begingroup$ Thank you a lot! Do you happen to know an easy way to insert the special character for DoubleBracketingBar aswell? It isn't in the Esc menu $\endgroup$
    – kungfooman
    Commented Nov 11, 2020 at 15:48
  • 1
    $\begingroup$ @lama12345 Esc-l||-Esc and Esc-r||-Esc (left and right double bar) works. $\endgroup$
    – Natas
    Commented Nov 11, 2020 at 15:56
5
$\begingroup$

You could create an input alias for this:

CurrentValue[EvaluationNotebook[], {InputAliases, "norm"}] = TemplateBox[
    {"\[SelectionPlaceholder]"},
    "Norm"
]

The only downside is you have to tab to the placeholder.

$\endgroup$
1
  • $\begingroup$ Wow, Norm with DoubleBracketingBar feels like a native construct now (at least for one notebook). Thank you a lot! $\endgroup$
    – kungfooman
    Commented Nov 12, 2020 at 8:48

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.