Re: StringReplace

*To*: mathgroup at smc.vnet.net*Subject*: [mg77801] Re: StringReplace*From*: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>*Date*: Sun, 17 Jun 2007 06:03:21 -0400 (EDT)*Organization*: The Open University, Milton Keynes, UK*References*: <f503n2$pc7$1@smc.vnet.net>

Bruce Colletti wrote: > Re Mathematica 6.0 under WinXP. > > Create variable X by: > > - Punching the summation button in the BasicMathInputPalette > > - Filling in the blanks: index i goes from 1 to n, with term f[i] > > - Enclose the whole thing between double quotes, thus making X a string variable (or so I thought) > > Now I want to replace n by 3, and so enter: > > StringReplace[X,"n"->"3"] > > The output includes a pink box that replaces the sigma and its bounds. Thankfully, the f[i] appears. > > What causes this output and how can I get X to appear, although with n replaced by 3? > > Thanks. > > Bruce Looking at the full form of the expression, one can see that Mathematica does not like the *UnderoverscriptBox (In[1]). If one uses the input form for the sum (In[4]) then the replacement works as expected. Note that it would be better to convert the expression into a string with the help of the function ToString (which is, indeed, the standard to do this sort of things). In[1]:= X = "\!\(\*UnderoverscriptBox[\"\[Sum]\", RowBox[{\"i\", \"=\", \"1\"}], \"n\"]\)f[i]" Out[1]= "\!\(\*UnderoverscriptBox[\"\[Sum]\", RowBox[{\"i\", \"=\", \"1\"}], \"n\"]\)f[i]" In[2]:= X // FullForm Out[2]//FullForm= \!\(\* TagBox[ StyleBox["\"\<\\!\\(\\*UnderoverscriptBox[\\(\\[Sum]\\), \\(i = 1\\), \ \\(n\\)]\\)f[i]\>\"", ShowSpecialCharacters->False, ShowStringCharacters->True, NumberMarks->True], FullForm]\) In[3]:= StringReplace[X, "n" -> "3"] Out[3]= " U3deroverscriptBox[\(\[Sum]\), \(i = 1\), \(3\)]f[i]" In[4]:= Y = "Sum[f[i],{\!\(\* StyleBox[\"i\", \"TI\"]\),1,n}]" Out[4]= "Sum[f[i],{\!\(\* StyleBox[\"i\", \"TI\"]\),1,n}]" In[5]:= Y // FullForm Out[5]//FullForm= \!\(\* TagBox[ StyleBox["\"\<Sum[f[i],{\\!\\(\\*\\nStyleBox[\\\"i\\\", \ \\\"TI\\\"]\\),1,n}]\>\"", ShowSpecialCharacters->False, ShowStringCharacters->True, NumberMarks->True], FullForm]\) In[6]:= StringReplace[Y, "n" -> "3"] Out[6]= "Sum[f[i],{\!\(\* StyleBox[\"i\", \"TI\"]\),1,3}]" Regards, Jean-Marc