Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

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.

share|improve this question

marked as duplicate by Mr.Wizard Jun 6 '14 at 1:10

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Possible duplicate: Is there an analogue of the Variables command for general expressions?. In short, does this do what you want?: Variables @ Level[expr, {-1}] (where expr is your expression). – Mr.Wizard Jun 5 '14 at 8:02
@Mr.Wizard thank you. this is what I am looking for. – Algohi Jun 5 '14 at 15:47
up vote 11 down vote accepted
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.

share|improve this answer
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) – gpap Jun 5 '14 at 7:33
@Bob Hanion, thanks. this is nice answer. – Algohi Jun 5 '14 at 15:53
An alternative: Cases[expr, Except[_?NumericQ, _Symbol], ∞] // Union – m_goldberg Jun 6 '14 at 0:53

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