2
$\begingroup$

Where is the problem?

enter image description here

Subscript[k, 0] := 1;
Subscript[k, 1] := 2;
ClearAll["Global`*"]
Subscript[k, 0]
Subscript[k, 1]
Clear[Subscript[k, 0]]
Clear[Subscript[k, 1]]
 
$\endgroup$
3
  • 5
    $\begingroup$ As a general rule I recommend that everyone avoid subscripts and use indexed variables instead, e.g., k[0] and k[1]. After assigning values the subscripted form would not appear in the output anyway. Prior to assigning values, if you want the subscripted form displayed, use Format[k[n_]] := Subscript[k, n] to control the subsequent display. $\endgroup$
    – Bob Hanlon
    Jun 21, 2021 at 18:18
  • 1
    $\begingroup$ @BobHanlon's advice is good advice. I wish subscripts were easier to deal with--they can be so darn useful sometimes. $vars = Indexed[x, #] & /@ Range[5]$ $Grad[f[vars], vars]$ $\endgroup$ Jun 21, 2021 at 19:07
  • 2
    $\begingroup$ Please use a descriptive title in the future. "Where is the problem?" gives no hint about the contents of your question. $\endgroup$
    – Szabolcs
    Jun 22, 2021 at 9:40

3 Answers 3

5
$\begingroup$

The problem is that your definitions are associated with Subscript not k.

Subscript is in the System context. I suspect that

(* ClearAll["System`*"] *)

would be a bad idea.

You could modify the definition of your symbols, so that they are associated with k

k /: Subscript[k, 0] := 1;
k /: Subscript[k, 1] := 2;

{Subscript[k, 0], Subscript[k, 1]}
(* {1, 2} *)

These can then be cleared

ClearAll["Global`*"]
{Subscript[k, 0], Subscript[k, 1]}
(* {Subscript[k, 0], Subscript[k, 1]} *)

Less drastically

Clear[k]

also works (don't specify the subscript, because the results isn't a symbol).

$\endgroup$
2
  • $\begingroup$ I got it Subscript is in the System context. Thank you. How to show all subscripts in a notebook? Is there any code such as ?Global`* and ?System`* $\endgroup$
    – RF_1
    Jun 21, 2021 at 20:13
  • 1
    $\begingroup$ ?Subscript will report subscripts - though I think you will need to click to expand the box containing the symbol information. $\endgroup$
    – mikado
    Jun 21, 2021 at 20:24
2
$\begingroup$

There is a very useful answer here: StackExchange 2017 by Michael E2 (I shouldn't get credit for this answer...)

Subscript[symbol, 1] = 1
Subscript[symbol, 2] = 2

Now you see it.

Subscript[symbol, 1]

Then you clear it by only retaining downvalues that don't depend on symbol

DownValues[Subscript] = 
 Cases[DownValues[Subscript], dv_ /; FreeQ[dv, symbol]]

Now you don't.

Subscript[symbol, 1]
$\endgroup$
0
$\begingroup$

One more way if you want to clear individual values.

Subscript[k, 0] := 1
Subscript[k, 1] := 2

You can clear one at a time this way:

Subscript[k, 0] =.

which clears the first one but leaves the second one alone.

Subscript[k, 0]
(*Subscript[k, 0]*)

Subscript[k, 1]
(*2*)
$\endgroup$

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