Re: subscripts and strings
- To: mathgroup at smc.vnet.net
- Subject: [mg28326] Re: subscripts and strings
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Wed, 11 Apr 2001 02:01:04 -0400 (EDT)
- Organization: Universitaet Leipzig
- References: <9arhrk$q8o@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi, water = {Subscript[H, 2], O} StringReplace[ ToString[TeXForm[water]], {"," -> "", "\\" -> "", "{" -> "", "}" -> ""}] ?? Regards Jens Matt.Johnson at autolivasp.com wrote: > > Hello everybody... > > Here's something quirky...suppose I have some subscripted variables. Then, > I want to combine these into a string, such as in a chemical formula. So I > have the element names subscripted by the number of atoms, such as > water={Subscript[H,2],O} > > then I want to combine the list into a string, H_2O (let the underscore > denote a subscript). This is where I run into problems. For some reason, > when ToString is mapped through the list, Mathematica interperets the subscripts as > newlines. So the question is, either how do I get the subscripted element > expression to be a string, or is there another way to combine these > altogether? > > Thanks, > > -matt > > Here's a quick synopsis: > > In[41]:= > \!\(\(list = {s\_1, s\_2, s\_3};\)\n > StringJoin[list]\) > \!\(List::"string" \(\(:\)\(\ \)\) > "String expected at position \!\(1\) in \!\({s\_1, s\_2, s\_3}\)."\) > \!\(List::"string" \(\(:\)\(\ \)\) > "String expected at position \!\(2\) in \!\({s\_1, s\_2, s\_3}\)."\) > \!\(List::"string" \(\(:\)\(\ \)\) > "String expected at position \!\(3\) in \!\({s\_1, s\_2, s\_3}\)."\) > General::"stop": "Further output of \!\(List :: \"string\"\) will be \ > suppressed during this calculation." > Out[42]= > \!\(s\_1 <> s\_2 <> s\_3\) > In[43]:= > StringJoin[ToString /@ list] > Out[43]= > "s\n 1s\n 2s\n 3" > In[44]:= > % // InputForm > Out[44]//InputForm= > "s\n 1s\n 2s\n 3" > In[45]:= > \!\(\(list2 = {\*"\"\<\!\(s\_1\)\>\"", \*"\"\<\!\(s\_2\)\>\"", \ > \*"\"\<\!\(s\_3\)\>\""};\)\[IndentingNewLine] > StringJoin[list2]\) > Out[46]= > \!\("\!\(s\_1\)\!\(s\_2\)\!\(s\_3\)"\) > In[47]:= > InputForm[%] > Out[47]//InputForm= > "\!\(s\_1\)\!\(s\_2\)\!\(s\_3\)" > > ************************************************************************************************************************************** > All Autoliv e-mails remain Company property and shall be used for > business-related purposes only. Do not forward any material contained > within this e-mail without prior, written permission from the author's > manager. Autoliv disclaims all responsibility and accepts no liability > (including negligence) for the consequences for any person acting, or > refraining from acting on this information prior to the receipt by those > persons of subsequent written confirmation. > **************************************************************************************************************************************