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?
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 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.
DoubleBracketingBar
aswell? It isn't in the Esc
menu
$\endgroup$
Commented
Nov 11, 2020 at 15:48
Esc-l||-Esc
and Esc-r||-Esc
(left and right double bar) works.
$\endgroup$
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.
Norm
with DoubleBracketingBar
feels like a native construct now (at least for one notebook). Thank you a lot!
$\endgroup$
Commented
Nov 12, 2020 at 8:48
Norm
. $\endgroup$Norm
, but I would like to use a "nicer" notation for it $\endgroup$