# How to do substitution of variable with power in Mathematica?

I want to replace $x^{i+1}$ with $z_i$.

EDIT: I have some latex equations, and wish to import them to MMa.

How to replace x_i (NOT $x_i$) with $x[i]$

But it seems the underscore "_" has a special meaning (function), can we do such substitution in MMa?

EDIT2:

For example, I want to convert the following two latex equations:

        {la_3^2=0, la_1la_3-6la_3ps=0}


into

        {la^2==0, la*la-6*la*ps==0}


so that I could apply Simplify or Solve to these equations later.

Great thanks.

• You shouldn't use x_i see Blank and see FullForm[x_i]. See also BlankSequence, BlankNullSequence. These characters have very special meanings. – Artes Jun 19 '12 at 19:58

To put in the offset by 1 that was desired in the question, one could do this:

x^(i+1) /. x^exponent_ :> Subscript[z, exponent - 1]


The rule works independently of whether i is an integer or not. I didn't take care of the special case i=0, but that would be doable just like in Artes' answer.

The difference in my answer is that I use RuleDelayed (:>) which allows me to do the subtraction of 1 on the right-hand side of the rule., instead of looking for the +1 in the pattern on the left-hand side (which could equally be a valid approach).

Edit

If your actual application is to import a $\LaTeX$ string, then the usual approach would be something like this:

latexString = "x_i + x^{i+1}";
ToExpression[latexString, TeXForm]


$x_i + x^{i+1}$

The output is of course an expression that no longer contains the Blank symbol _. So your substitution would miss the target if it were now to look for a _. Instead, it has to look for the translated expression pattern involving Subscript $x_i$.

The general rule when you're not sure what pattern to look for in performing substitutions on Mathematica expressions is: take an example expression, and wrap it in InputForm[expression] to see how it is represented internally. For more complicated cases, you'll have to inspect FullForm[expression].

On the other hand, when importing $\LaTeX$ one can also run into a different kind of trouble: the input may not correspond to a valid expression, which will cause an error when doing ToExpression. In such cases it's sometimes necessary to do StringReplace.

That would also be something you could do in your example.But I'm not sure if that's what you want, so I'll leave it out for now.

Edit 2

I Mathematica, $\LaTeX$ code has to be put into strings by surrounding it with quotation marks. Otherwise you're asking for trouble (syntax errors).

When $\LaTeX$ code is inside a string, you furthermore have to escape the backslash character so that \sin becomes \\sin etc. Even with these precautions, it isn't guaranteed that Mathematica will understand your $\LaTeX$ code, as I mention on this web page.

One useful trick that you can always try, though, is to take an example Mathematica expression that you would like to produce from $\LaTeX$, and find out what the correct $\LaTeX$ source for it would be by doing this:

mmaCode = {la^2==0, la*la-6*la*ps==0}
ToString[TeXForm[mmaCode]]


This will tell you what the input string should be, to get the expression in mmaCode.

Now you can copy the output of this command, and when you paste it again it will have the escaped backslashes appear automatically.

This is what I'm doing now, by pasting the last result back into a ToExpression:

ToExpression[
"\\left\\{\\text{la}(3)^2=0,\\text{la}(1)
\\text{la}(3)-6 \\text{la}(3) \\text{ps}=0\\right\\}",
TeXForm
]

(* ==> {9 la == 0, -6 ps la + la la == 0} *)


Notice how equals signs are treated differently in $\LaTeX$ and Mathematica (= versus ==), and how variable names with more than one character have to be wrapped in \text in order to be correctly recognized as a single symbol in Mathematica. If you don't do that, every character is interpreted as a separate variable name.

So what does this mean for your example input? First we have to write it in quotation marks because of what I just said, and then we have to escape the curly brackets as you see in the previous output of TeXForm. Lastly, the variable names have to be wrapped in \\text:

inputString =
"\\{\\text{la}_3^2=0, \\text{la}_1\\text{la}_3-6\\text{la}_3\\text{ps}=0\\}"


With this, you can finish the conversion to Mathematica:

mmaCode2 = ToExpression[inputString, TeXForm]


On the result, we can now do the pattern replacements that started this post.

mmaCode2 /. Subscript[la, i_] -> la[i]

(* ==> {la^2 == 0, -6 ps la + la la == 0} *)

• cool. I just edited my original post with more details. It seems when I use ToExpression["la_1la_2"], MMa will treat l and a_i as separate variables, but I want to treat la as one variable. Any idea? – Osiris Xu Jun 19 '12 at 20:55
• I've added a discussion of this to my answer. – Jens Jun 19 '12 at 22:50
x^3 + 3 x^2 - 5 x /. {x^(i_Integer) -> Subscript[z, i], x -> Subscript[z, 1]} As rcollyer suggested in the comment it can be shortened to

x^3 + 3 x^2 - 5 x /. x^(i_Integer: 1) :> Subscript[z, i]

• I prefer RuleDelayed for this, having the HoldRest attribute. Also, it can be shortened to x^(i_Integer:1):> Subscript[z,i]. – rcollyer Jun 19 '12 at 19:48
• @rcollyer Thanks for your suggestion. – Artes Jun 19 '12 at 19:53