Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This question already has an answer here:

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

share|improve this question

marked as duplicate by Jacob Akkerboom, Sjoerd C. de Vries, m_goldberg, bobthechemist, Kuba Apr 22 at 20:59

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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

1 Answer 1

up vote 2 down vote accepted

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} *)
share|improve this answer
    
StringReplace[ToString[k]," "->""] helps with subscripts that have spaces –  denfromufa Apr 22 at 17:26

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