2
$\begingroup$

When using Solve for example, I find myself having to use it in conjunction with other types of functions, like Flatten or Simplify, like:

Flatten[Simplify[Solve[x^2 + 3 x == 2, x]]]

Since I am a complete beginner in building macros in Mathematica, I was trying to see if it were possible to create a macro, where you would type say fss, and it automatically converts it into Flatten[Simplify[Solve[]]], where if possible the cursor is placed right inside the Solve automatically.

I tried to do something like:

InputAutoReplacements -> {"fss" -> Flatten[Simplify[Solve[]]]};

Unfortunately, this did not work...

$\endgroup$
  • 2
    $\begingroup$ related: live-code-templates $\endgroup$ – Kuba Feb 20 '19 at 15:11
  • 2
    $\begingroup$ Why a macro? Just define fss[e_,v_]:=Flatten@Siimplify@Solve[e,v]; $\endgroup$ – Somos Feb 20 '19 at 16:13
5
$\begingroup$

With an input auto replacement like "fss", you need to have some way of telling Mathematica that "fss" is a complete token. That is, "fss" only gets replaced when the next character is not a letter. The usual input auto replacements typically get replaced when you type a letter, e.g., a->b only becomes $a\to b$ after you type the letter "b".

So, to make the input auto replacement behave the way you probably want, you could do the following:

CurrentValue[EvaluationNotebook[], {InputAutoReplacements, "fss"}] := RowBox[{
    "Flatten", "[", RowBox[{"Simplify", "[", RowBox[{"Solve", "\[SelectionPlaceholder]", "]"}], "]"}], "]"
}]

Then, typing "fss[" will perform the desired replacement, since "[" is not a letter. The "[" character will persist, which is why it is not included in the replacement code.

|improve this answer|||||
$\endgroup$
  • $\begingroup$ Amazing! Thank you very much. $\endgroup$ – Patrick.B Feb 21 '19 at 21:42

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.