2
$\begingroup$

When I use the alias \[Esc] dintt \[Esc] to pretty print a definite integral in my notebook, it actually stands for Integrate[...].

I would like to have the same readable format with the integral sign, but to stand for numerical integration, i.e. NIntegrate[...]. Is it possible?

Of course, as a workaround wrapping everything in N works, like N[\[Esc] dintt \[Esc]].

$\endgroup$
1
  • 2
    $\begingroup$ N[\[Esc] dintt \[Esc]] calls Integrate first, so it's not exactly equivalent. $\endgroup$
    – Michael E2
    Aug 17, 2018 at 19:49

2 Answers 2

5
$\begingroup$

You can use a TemplateBox in your alias to control how the alias is interpreted:

CurrentValue[EvaluationNotebook[], InputAliases] = DeleteDuplicates @ Prepend[
    CurrentValue[EvaluationNotebook[], InputAliases],
    "ndint" -> TemplateBox[
        {"\[Placeholder]","\[Placeholder]","\[Placeholder]","\[SelectionPlaceholder]"},
        "ndint",
        DisplayFunction -> (
            RowBox[{
                SubsuperscriptBox["\[Integral]", #1, #2], 
                RowBox[{#3, RowBox[{"\[DifferentialD]", #4}]}]
            }]&),
        InterpretationFunction -> (
            RowBox[{
                "NIntegrate", "[",
                RowBox[{#3, ",", RowBox[{"{", RowBox[{#4, ",", #1, ",", #2}], "}"}]}],
                "]"
            }]&
        ),
        Tooltip->"NIntegrate"
    ]
];

Here is an animation showing the alias in action:

enter image description here

One difference with mikado's answer is that only integrals created by the alias are interpreted as a NIntegrate object. Other integrals are interpreted as an Integrate object as usual.

$\endgroup$
2
$\begingroup$

The Notation package can help you here. This definition interprets all definite integral expressions as being NIntegrate rather than Integrate

Needs["Notation`"]

Notation[ParsedBoxWrapper[
RowBox[{
SubsuperscriptBox["∫", "u_", "v_"], 
RowBox[{"f_", 
RowBox[{"\[DifferentialD]", "x_"}]}]}]] ⟹ 
  ParsedBoxWrapper[
RowBox[{"NIntegrate", "[", 
RowBox[{"f_", ",", 
RowBox[{"{", 
RowBox[{"x_", ",", "u_", ",", "v_"}], "}"}]}], "]"}]]]

\!\(
\*SubsuperscriptBox[\(∫\), \(0\), \(1\)]\(Sin[
   x] \[DifferentialD]x\)\)
(* 0.459698 *)

Unfortunately, this looks a bit of a mess when pasted into the website, but should work when pasted back into Mathematica.

$\endgroup$

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.