3
$\begingroup$

Given a list of operators and integers, for instance

oper = {Subtract, Times, Plus}
{a, b, c, d} = {3, 5, 4, 9}

I would like to print

oper[[2]][oper[[1]] @@ {a, b}, oper[[3]] @@ {c, d}]

as $(3-5)\times (4+9)$ (set aside the $\LaTeX$ formatting, I just mean the numbers and the operators in a classical way). Whenever I use Hold, Unevaluate or Inactivate, the oper[[2]] and similar are not evaluated. Also the operator names are not replaced with their symbols.

Any hints?

$\endgroup$
6
$\begingroup$
ClearAll[a, b, c, d, oper]
oper = {Subtract, Inactive@Times, Inactive@Plus} /.
   Subtract -> (HoldForm[Plus[#, -#2]] &);
{a, b, c, d} = {3, 5, 4, 9};
oper[[2]][oper[[1]] @@ {a, b}, oper[[3]] @@ {c, d}]

enter image description here

$\endgroup$
1
$\begingroup$
(3 - 5)*(4 + 9) // Inactivate

$(3\ {\color{#b39980}+}{\text{ -}5})*(4\ {\color{#b39980}+}\ 9)$

If you only want to inactivate the heads that appear in oper while others occur:

hold = Function[e, Unevaluated[e] /. HoldPattern[oper[[a_]]] :>
                                      Inactive[Evaluate[oper[[a]]]], HoldAllComplete];

oper[[2]][oper[[1]] @@ {a, b}, oper[[3]] @@ {c, d}] // hold

${\color{#b39980}S\color{#b39980}u\color{#b39980}b\color{#b39980}t\color{#b39980}r\color{#b39980}a\color{#b39980}c\color{#b39980}t}[3,5]{\ \color{#b39980}*\ }(4{\ \color{#b39980}+\ }9)$

% /. Inactive[Subtract][a_, b_] :> (HoldForm[Plus[a, -b]])

$(3-5){\ \color{#b39980}*\ }(4{\ \color{#b39980}+\ }9)$

$\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.