9
$\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$
2
  • 1
    $\begingroup$ You can define your own function to follow this form: BracketingBar[x_] := Abs[x] $\endgroup$ Jun 4, 2016 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, 2016 at 14:48

2 Answers 2

11
$\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$
11
$\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$
3
  • $\begingroup$ Do you mind adding how you would go about this using the Notation Package? Fine with asking as a separate question if you'd prefer. $\endgroup$
    – Sterling
    Feb 9, 2021 at 0:09
  • $\begingroup$ @Sterling I have been away from Stack Exchange. Did you solve this in the interim? $\endgroup$
    – Mr.Wizard
    Mar 6, 2021 at 19:04
  • $\begingroup$ No worries. I haven't solved this yet. $\endgroup$
    – Sterling
    Mar 8, 2021 at 16:07

Your Answer

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

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