I'm looking for a procedural way to define a symbol for a variable, based on a string. For instance, ToExpression["A^B"]
gives us Power[A,B]
(which, might be enough if we know that B
will never get any value throughout the code).
However, as soon as we assign a number to B
, we start getting troubles. For example, ToExpression["A^0"]
evaluates to 1
, ToExpression["A^(-1)"]
evaluates to 1/A
, and so on, but I would like to get Superscript[A,0]
and Superscript[A,-1]
, and so on instead. How can I achieve that?
Superscript
orSubscript
? $\endgroup$Format[A[n_]] := Superscript[A, n]
then{A[0], A[1], A[-1]}
$\endgroup$