0
$\begingroup$

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

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

1 Answer 1

2
$\begingroup$

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} *)
$\endgroup$
1
  • $\begingroup$ StringReplace[ToString[k]," "->""] helps with subscripts that have spaces $\endgroup$
    – den.run.ai
    Commented Apr 22, 2014 at 17:26

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