1
$\begingroup$

Given the following code:

test[p_] := 
 With[{str = If[p > 0, "+", "-", "+"]}, 
  DisplayForm@
   RowBox[{DisplayForm[SuperscriptBox["J", "P"]], "=", 
     ToString[DisplayForm[SuperscriptBox["3/2", str]]]}]]

I expect an output like

$$J^P=3/2^\pm$$

depending on the sign of $p$.

However, I get the following output:

RowBox[List[DisplayForm[SuperscriptBox["J","P"]],"=","   +\n3/2"]]

Why is the output so mangled? Note the "3/2" appearing last and behind whitespaces and specifically a linebreak \n. How can I fix this?

Version 13.1.0.0 Student Edition (64-bit on Ubuntu 22.04)

$\endgroup$

1 Answer 1

1
$\begingroup$

You have a superfluous "ToString". Without this:

test[p_] := 
 With[{str = If[p > 0, "+", "-", "+"]}, 
  DisplayForm@
   RowBox[{DisplayForm[SuperscriptBox["J", "P"]], "=", 
     DisplayForm[SuperscriptBox["3/2", str]]}]]

test[1]

enter image description here

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