Using subscripted symbols in functions

Using subscripts would significantly improve the structure of my code. Until now I used the following two lines to implement this.

Needs["Notation"];
Symbolize[ParsedBoxWrapper[SubscriptBox["_", "_"]]];


This allows me to define a symbol like

Subscript[a, r] = 10


This works fine up to the point where I want to define e.g.

f[r_] = r Subscript[a, r][r]


I want the result to be

f[x] = 10 x


The problem is that Mathematica recognizes the r in Subscript[a, r] as an variable. Hence I can not use the subscript in such a case. Is there an easy way to solve this?

• – dionys Jul 21 '15 at 13:04
• My experience tells me that you will disappointed with the approach you are taking -- introducing subscripted variables will cause more problems than improvements to the structure of your code. – m_goldberg Jul 21 '15 at 13:51

I think you may want

With[{u = Subscript[a, r]}, f[r_] := u r]


then

f[x]

10 x


Or perhaps you want to keep Subscript[a, r] as a free variable.

g[x_] := Subscript[a, r] x
g[x]

10 x

Subscript[a, r] = 42; g[x]

42 x
`