# Append subscript to variable name [duplicate]

I'm importing equations from Word document and transforming them into input form. But subscripts look ugly.

Is is possible to programmatically append subscript to variable name?

e.g.

AppendSub[Subscript[a, 1]]=a1

• or even Array[x, 10] /. x_[k_] :> Subscript[x, k]?
– chuy
Commented Apr 22, 2014 at 16:47
• sorry for confusion, I clarified the question Commented Apr 22, 2014 at 16:55

## 1 Answer

Sure, one way is to use patterns and replacement rules (check out Symbol):

{Subscript[x, 1], Subscript[x, 2], Subscript[x, 3],
Subscript[x, 4], Subscript[x, 5], Subscript[x, 6], Subscript[x, 7],
Subscript[x, 8], Subscript[x, 9], Subscript[x, 10]} /.
Subscript[x_, k_] :> Symbol[ToString[x] <> StringReplace[ToString[k]," "->""]]
(* {x1, x2, x3, x4, x5, x6, x7, x8, x9, x10} *)


as a function you can use:

appendSub[exp_] :=
exp /. Subscript[x_, k_] :> Symbol[ToString[x] <> StringReplace[ToString[k]," "->""]]

appendSub[{Subscript[x, 1], Subscript[x, 2], Subscript[x,
3], Subscript[x, 4], Subscript[x, 5], Subscript[x, 6], Subscript[x,
7], Subscript[x, 8], Subscript[x, 9], Subscript[x, 10]}]
(* {x1, x2, x3, x4, x5, x6, x7, x8, x9, x10} *)

• StringReplace[ToString[k]," "->""] helps with subscripts that have spaces Commented Apr 22, 2014 at 17:26