2
$\begingroup$

Update: the following code proposed by Lukas works perfectly. I have tested both fraction or $a/b$ form, both work. I have revised the code a little bit, to make it more flexible:

  MakeBoxes[Sin[Rational[1, 2] Subscript[\[Alpha]_, i_]], 
  StandardForm] := 
 MakeBoxes[Subscript["s", Subscript[Overscript[\[Alpha], _], i]], 
 StandardForm]
MakeBoxes[Cos[Rational[1, 2] Subscript[\[Alpha]_, i_]], 
  StandardForm] := 
 MakeBoxes[Subscript["c", Subscript[Overscript[\[Alpha], _], i]], 
  StandardForm]

I learnt that we can use Makeboxes to display Sin[\alpha_i] as $s_{\alpha_i}$ as follows:

  (*MakeBoxes[Sin[Subscript[\[Alpha],i_]],StandardForm]:=
 MakeBoxes[Subscript[Subscript["s",\[Alpha]],i],StandardForm]

but can't find a way to make Sin[Subscript[[Alpha],i]/2] display as $s_{\bar{\alpha}_i}$, regardless of in fraction form or in $a/b$ form. I have been looking for this for quite a long time, but with no luck. I checked the documentation, but can't understand how Makeboxes works, especially in this context.

Could someone help to produce such a way so that it can take effect on both calculations involving fraction form or $a/b$ form? It'd be even better if some explanation on how to do it is provided.

$\endgroup$

1 Answer 1

2
$\begingroup$

The issue you are facing is that the FullForm of x/2 changes upon evaluation, combined with the HoldAllComplete attribute of MakeBoxes:

Hold[a/2]//FullForm
(* Hold[Times[a,Power[2,-1]]] *)

a/2//FullForm
(* Times[Rational[1,2],a] *)

Note how $a\cdot2^{-1}$ changes to $a\cdot\frac 12$ when allowed to evaluate. As mentioned above, this leads to problems due to the HoldAllComplete attribute of MakeBoxes: You are defining a rule for the form you are inputting, which means it will not be applied to the evaluated form:

MakeBoxes[Sin[Subscript[α, i] / 2], StandardForm] =
  MakeBoxes[Subscript[s, Subscript[Overscript[α, _], 2]], StandardForm]

Sin[Subscript[\[Alpha], i]/2]

$\sin(\frac\alpha2)$

HoldForm[Sin[Subscript[\[Alpha], i]/2]]

$s_{\bar{\alpha}_2}$

To fix this, you need to define a formatting rule for the evaluated form:

MakeBoxes[Sin[Rational[1, 2] Subscript[α, i]], StandardForm] = 
  MakeBoxes[Subscript[s, Subscript[Overscript[α, _], 2]], StandardForm]

Sin[Subscript[\[Alpha], i]/2]

$s_{\bar{\alpha}_2}$

Alternatively, you can also inject the evaluated form into MakeBoxes:

With[
  {evaluated = Sin[Subscript[α, i] / 2]},
  MakeBoxes[evaluated, StandardForm] =
   MakeBoxes[Subscript[s, Subscript[Overscript[α, _], 2]], StandardForm]
]

Sin[Subscript[\[Alpha], i]/2]

$s_{\bar{\alpha}_2}$

Note that using Evaluate would not work, since HoldAllComplete prevents any and all forms of evaluation, including Evaluate and up-values.

$\endgroup$
1
  • $\begingroup$ Thanks for your comment, which helps me solve the problem successfully. Notice that I made a typo in the subscript, I wanted it to be "i". I have revised it, and revised your code a little bit, to make it more flexible. Thanks! $\endgroup$
    – larry
    Dec 7, 2019 at 15:15

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.