0
$\begingroup$

I'm trying to define MakeBoxes for a bunch of functions all at once, but wish to do so within a loop as in the following example:

{
  {a[1], "Foo"},
  {a[2], "\!\(\*SubscriptBox[\(Goo\), \(``\)]\)"},
  {b[1], "\!\(\*SubsuperscriptBox[\(Hoo\), \(``\), \(``\)]\)"}
};
Do[
 Block[{class, number, template},
  class = Head[tmp[[1]]];
  number = First[tmp[[1]]];
  template = tmp[[2]];

  MakeBoxes[class[number], TraditionalForm] = StringTemplate[template] @@ {};
  MakeBoxes[class[number, {args__}], TraditionalForm] := StringTemplate[template] @@ {args};
  ],
 {tmp, %}
 ]

The problem is that the use of SetDelayed both class[number, {args__}] on the left and template on the right unevaluated, which makes subsequent calls to TraditionalForm[a[2, {i}] not work.

I have tried adding some combinations of Evaluate here and there, but so far nothing I have tried has worked.

Is it possible to achieve what I want?

$\endgroup$
3
$\begingroup$
{
  {a[1], "Foo"},
  {a[2], "\!\(\*SubscriptBox[\(Goo\), \(``\)]\)"},
  {b[1], "\!\(\*SubsuperscriptBox[\(Hoo\), \(``\), \(``\)]\)"}
};
Do[
 Block[{class, number, template},
  class = Head[tmp[[1]]];
  number = First[tmp[[1]]];
  template = tmp[[2]];
  With[{class = class, number=number, template = template},
    MakeBoxes[class[number], TraditionalForm] = StringTemplate[template] @@ {};
    MakeBoxes[class[number, {args__}], TraditionalForm] := StringTemplate[template] @@ {args};
  ]
  ],
 {tmp, %}
 ]
$\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.