Let Expr be an expression that can be practically any combination of variables. Now I can check whether this expression contains any subscripts or superscripts with help of


However, in a next step I would like to read out a list of all appearing sub- and superscripts explicitly without introducing more constraints on how the expression should look like specifically. Is there any function that does this? Or maybe one can implement it? Thanks for any suggestion!


Expr = (h*Superscript[u, g]*Superscript[Subscript[a, b, c], z])^Pi;


The point is, that it should not matter how exactly the expression is organized. The function should simply return all appearing Superscripts and Subscripts.

  • $\begingroup$ Not sure what you want. Would Cases do? $\endgroup$ – Sjoerd C. de Vries Nov 26 '14 at 21:52
  • $\begingroup$ No, I'm afraid Cases is too specific. I will add an example to the question to make it more clear. $\endgroup$ – Kagaratsch Nov 26 '14 at 21:56
GetSubAndSuperscripts[expr_] := 
    Cases[expr, Superscript[_, s__] | Subscript[_, s__] :> s, Infinity]

Expr = (h*Superscript[u, g]*Superscript[Subscript[a, b, c], z])^Pi;


{g, b, c, z}

| improve this answer | |
  • $\begingroup$ Wow! I did not know one could use this function like that. Looks very promising, thank you! $\endgroup$ – Kagaratsch Nov 26 '14 at 22:26

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.