Skip to main content
added 4 characters in body
Source Link

If you have even more complicated expressions, you might want to use Heads -> True.

expr = {f, Subscript[g, i], h[i[j[a, b]]], s'[t] == u[t] + v[t]};

Union@Cases[exprUnion @ Cases[expr, Except[__Symbol?(Context@#Context @ # === "System`" &), _Symbol],
    {1, Infinity},
              Heads -> True]
{a, b, f, g, h, i, j, s, t, u, v}

Without checking heads:

Union@Cases[exprUnion @ Cases[expr, Except[__Symbol?(Context@#Context @ # === "System`" &), _Symbol], {1, Infinity}]
{a, b, f, g, i, t}

If you have even more complicated expressions, you might want to use Heads -> True.

expr = {f, Subscript[g, i], h[i[j[a, b]]], s'[t] == u[t] + v[t]};

Union@Cases[expr, Except[__Symbol?(Context@# === "System`" &), _Symbol],
    {1, Infinity}, Heads -> True]
{a, b, f, g, h, i, j, s, t, u, v}

Without checking heads:

Union@Cases[expr, Except[__Symbol?(Context@# === "System`" &), _Symbol], {1, Infinity}]
{a, b, f, g, i, t}

If you have even more complicated expressions, you might want to use Heads -> True.

expr = {f, Subscript[g, i], h[i[j[a, b]]], s'[t] == u[t] + v[t]};

Union @ Cases[expr, Except[__Symbol?(Context @ # === "System`" &), _Symbol], {1, },
              Heads -> True]
{a, b, f, g, h, i, j, s, t, u, v}

Without checking heads:

Union @ Cases[expr, Except[__Symbol?(Context @ # === "System`" &), _Symbol], {1, }]
{a, b, f, g, i, t}
Source Link
István Zachar
  • 47.2k
  • 20
  • 145
  • 293

If you have even more complicated expressions, you might want to use Heads -> True.

expr = {f, Subscript[g, i], h[i[j[a, b]]], s'[t] == u[t] + v[t]};

Union@Cases[expr, Except[__Symbol?(Context@# === "System`" &), _Symbol],
    {1, Infinity}, Heads -> True]
{a, b, f, g, h, i, j, s, t, u, v}

Without checking heads:

Union@Cases[expr, Except[__Symbol?(Context@# === "System`" &), _Symbol], {1, Infinity}]
{a, b, f, g, i, t}