55
$\begingroup$

I tried

Subscript[a, 0] = 1
(* 1 *)

Clear[Subscript[a, 0]]
       During evaluation of Clear::ssym: Subscript[a, 0] is not a symbol or a string. >>
Clear[a]
Subscript[a, 0]
(* 1 *)

Any idea?

$\endgroup$
2
  • 1
    $\begingroup$ This probably should have been marked as a duplicate question on stackoverflow. See [1] (and maybe [2]) Should we also migrate the other post and/or merge? Or are we happy with the question being covered on both sites? Of course, @Spartacus' answer covers most variations of this question. +1! $\endgroup$
    – Simon
    Commented Jan 30, 2012 at 23:24
  • 1
    $\begingroup$ This is contained in Mr. Wizard's answer below, but the short and sweet of it that does exactly what I want is: Type your variable name, e.g. "a", then CTRL and - to type your subscript. You can assign a value using ":=", but to remove the assignment the Clear command won't work, but Unset command will work. $\endgroup$
    – user7713
    Commented May 7, 2015 at 7:47

2 Answers 2

39
$\begingroup$

Yes you can, with limitations.

You have at least three different ways to make an assignment to a subscripted symbol a0 :

  1. make a rule for Subscript

  2. make a rule for a

  3. "symbolize" a0 using the Notation package/palette

In each case below, when I write e.g. Subscript[a, 1] this can also be entered as a1 by typing a then Ctrl+_ then 1.

When you write:

Subscript[a, 1] = "dog";

You make an assignment to Subscript:

DownValues[Subscript]

{HoldPattern[a1] :> "dog"}

You make a rule for a by using TagSet:

a /: Subscript[a, 2] = "cat";

UpValues[a]

{HoldPattern[a2] :> "cat"}

If you use the Notation palette you mess with underlying Box forms behind the scenes, allowing for assignment to OwnValues:

Mathematica graphics

Each of these can be cleared with either Unset or TagUnset:

Subscript[a, 1] =.

a /: Subscript[a, 2] =.

Mathematica graphics

$\endgroup$
6
  • 1
    $\begingroup$ It's worth noting that you can directly type a shortcut for subscripts, but I don't know how to show this here. Namely: a<sub>3</sub> = "lion"; where "<sub>3</sub> should be an actual subscript, typed in the Mathematica Front End by first typing Ctrl _ . $\endgroup$
    – murray
    Commented Jan 30, 2012 at 18:38
  • 3
    $\begingroup$ is it good MMA form to convert subscript as quickly as possible to something like a5 and then at the last step for output output the result with a subscript, so do the majority of manipulations with identifiers like a5? $\endgroup$ Commented Jan 5, 2015 at 19:00
  • 4
    $\begingroup$ @Manuel--Moe--G That would work but usually it is better to use e.g. a[5] which makes conversion in either direction much simpler and faster. However there are places (like Module) that actual Symbols are needed. Also, I haven't looked at the internals of the Notation palette in some years but I seem to recall that the use of Symbolize is performing this kind of translation itself, such that Symbols are used in place of Subscripts during computation, then formatted again as Subscripts for output, so look at that before you "reinvent the wheel" as it were. $\endgroup$
    – Mr.Wizard
    Commented Jan 6, 2015 at 3:59
  • $\begingroup$ Is there any way that we could clear by ClearAll["Global*"]? $\endgroup$ Commented Jul 14, 2016 at 16:18
  • 1
    $\begingroup$ @H.R. If you use the TagSet method the rule is attached to e.g. a, and if a is in the Global context ClearAll["Global*"] will clear that rule. $\endgroup$
    – Mr.Wizard
    Commented Jul 15, 2016 at 1:11
12
$\begingroup$

You can also do this:

<< Notation`
Symbolize[ParsedBoxWrapper[SubscriptBox["_", "_"]]]

If you want to, you can import the Notation package first, then use the Symbolize function, so you don't have to use the ParsedBoxWrapper function, and just enter _,(ctrl+_),_. What this does, is set a pattern matching a subscripted character as a symbol. Mathematica will then treat expressions matching this form as a single variable.

This doesn't seem to affect functions that use subscripts in their definition such as the partial differential function.

$\endgroup$
5
  • $\begingroup$ For what it's worth this is mentioned as point #3 in my answer, then illustrated further down. $\endgroup$
    – Mr.Wizard
    Commented Jan 14, 2016 at 22:34
  • 4
    $\begingroup$ I saw, and it was very useful. I just thought I would expand it so that any subscripted pattern would be interpreted as a symbol. $\endgroup$
    – Alex DB
    Commented Jan 18, 2016 at 17:54
  • $\begingroup$ Oh, I see. Okay +1 :-) $\endgroup$
    – Mr.Wizard
    Commented Jan 19, 2016 at 3:18
  • 1
    $\begingroup$ (+1) Nicely done! :) I was looking for this since the day I started using Mathematica! :) $\endgroup$ Commented Jul 14, 2016 at 23:04
  • $\begingroup$ Works like a charm. However it kind of breaks calls to TraditionalForm and TeXForm. $\endgroup$
    – guru10at
    Commented Aug 11, 2021 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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