If I have an expression, let us say something like this:

Cos[3 x] + π/cofe Exp[y^(c  z)]

how can I get all non-number symbols in the expression which are:

{x, cofe, y, c, z}

I tried this:

Cases[Cos[3 x] + π/cofe Exp[y^(c  z)], _Symbol]

but it didn't work.

expr = Cos[3 x] + \[Pi]/cofe Exp[y^(c z)];

Cases[expr, _Symbol?(! NumericQ[#] &), Infinity] // Union

(* {c, cofe, x, y, z} *)

In this case the Union is not necessary; however, in general it is needed to eliminate duplicates.

| improve this answer | |
  • $\begingroup$ This is a nice answer and I am puzzled there is no built-in for this sort of thing but Variables (for polynomial expressions) is the closest there is. (+1) $\endgroup$ – gpap Jun 5 '14 at 7:33
  • $\begingroup$ @Bob Hanion, thanks. this is nice answer. $\endgroup$ – Algohi Jun 5 '14 at 15:53
  • $\begingroup$ An alternative: Cases[expr, Except[_?NumericQ, _Symbol], ∞] // Union $\endgroup$ – m_goldberg Jun 6 '14 at 0:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.