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[3]^2==0, la[1]*la[3]-6*la[3]*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[3] + la[1] la[3] == 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[3]^2 == 0, -6 ps la[3] + la[1] la[3] == 0} *)
x_i
seeBlank
and seeFullForm[x_i]
. See alsoBlankSequence
,BlankNullSequence
. These characters have very special meanings. $\endgroup$