8
$\begingroup$

Is there a keyboard shortcut or esc-sequence for typing in the absolute value bars into input cells? I know that if you do,

Abs[-4] // TraditionalForm

and copy the output into an input cell using the "Paste literal" option, you get functioning absolute value bars with a placeholder. I'm curious if there's a less roundabout way to obtain them.

$\endgroup$
  • 1
    $\begingroup$ You can define your own function to follow this form: BracketingBar[x_] := Abs[x] $\endgroup$ – Algohi Jun 4 '16 at 22:09
  • 1
    $\begingroup$ That give the traditional notation in the output (for arguments that are not numeric), but not in input. O.P. requested the notation in Input cells. $\endgroup$ – murray Jul 15 '16 at 14:48
10
$\begingroup$

There isn't a built-in keybinding, but you can define one yourself:

ClearAll[myAbs]

appearanceAbs[x_] := 
 TemplateBox[{x}, "myAbs", 
  DisplayFunction :> (RowBox[{"\[LeftBracketingBar]", 
       "\[NoBreak]", #1, "\[NoBreak]", "\[RightBracketingBar]"}] &), 
  InterpretationFunction :> (RowBox[{"myAbs", "[", #1, "]"}] &)]

myAbs[x_?NumericQ] := Abs[x]

myAbs /: MakeBoxes[myAbs[x_], _] := appearanceAbs[ToBoxes[x]]

SetOptions[EvaluationNotebook[], 
 InputAliases -> 
  DeleteDuplicates@
   Join[{"abs" -> appearanceAbs["\[SelectionPlaceholder]"]}, 
    InputAliases /. 
      Quiet[Options[EvaluationNotebook[], InputAliases]] /. 
     InputAliases -> {}]]

This adds a shortcut EscabsEsc to the notebook's InputAliases. When you type it, a template will appear that looks the way one expects, $|\square|$. You may have to tab into it (that's a bug that didn't exist in older versions), and it will evaluate as Abs if the argument you entered is numeric. Otherwise, the display will remain of the form you see in TraditionalForm output. The evaluation of Abs happens because I define the template with an InterpretationFunction. I restrict this to numeric arguments, and for the other cases there is the definition myAbs /: MakeBoxes which kicks in when an unevaluated template is to be displayed again. To make this case distinction possible, I define an intermediate function myAbs.

$\endgroup$
9
$\begingroup$

If one does not mind occupying BracketingBar this can be done quite cleanly.

MakeBoxes[Abs[x_], StandardForm] := MakeBoxes @ BracketingBar[x]

BracketingBar = Abs;

This provides bidirectoinal translation, i.e. Abs[x] is formatted with bars, and also it may be entered with the special characters \[LeftBracketingBar] and \[RightBracketingBar], entered with EscL|Esc and EscR|Esc.

Alternatively one may use the Notation Package to set up bidirectional or unidirectional translation.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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