3
$\begingroup$

Say we have the following expression:

$r_{1,3} r_{1,2}^3 m^2-r_{1}r_{4,2,1}$

(Sorry I had to use TeX, read as Mathematica input notation)

I need a function that lists all the subscripts of $r$.

The result should be: {{1,3},{1,2},{1},{4,2,1}}

InputForm for the input expression:

expr = ToExpression["r_{1,3} r_{1,2}^3 m^2-r_{1}r_{4,2,1}", TeXForm];
InputForm @ expr

m^2*Subscript[r, 1, 2]^3*Subscript[r, 1, 3] - Subscript[r, 1]*Subscript[r, 4, 2, 1]

$\endgroup$
  • $\begingroup$ Can't properly write subscripts here. Had to use tex. Sorry. $\endgroup$ – Ivan May 27 '18 at 5:52
2
$\begingroup$
expr = ToExpression["r_{1,3} r_{1,2}^3 m^2-r_{1}r_{4,2,1}", TeXForm];
Cases[expr, Subscript[_, s__] :> {s}, {0, Infinity}]

{{1, 2}, {1, 3}, {1}, {4, 2, 1}}

Cases[ToExpression["r_{1,2}", TeXForm], Subscript[_, s__] :> {s}, {0, Infinity}]

{{1, 2}}

$\endgroup$
  • $\begingroup$ Close enough! Fails when expr has only one term, e.g. r_{1,2} $\endgroup$ – Ivan May 27 '18 at 6:11
  • $\begingroup$ @Ivan, adding level 0 to the last argument fixes that issue. $\endgroup$ – kglr May 27 '18 at 6:22

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.