MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: parenthesis in reactions...

  • To: mathgroup at smc.vnet.net
  • Subject: [mg69411] Re: parenthesis in reactions...
  • From: "Chris Chiasson" <chris at chiasson.name>
  • Date: Mon, 11 Sep 2006 05:39:08 -0400 (EDT)
  • References: <acbec1a40605261652r5e126c9dg9b61561c44b86a6e@mail.gmail.com>

Here is a way to do it without the Notation package, though I had to
assign a Format value to Times to make it work.

MakeBoxes[ChemPlus,_]="+";
MakeBoxes[ChemMinus,_]="-";
Format[ChemSuperscript[z_?NumberQ]/;Element[z,Reals]&&z\[NotEqual]0]:=
    Module[{abz=Abs[z]},
      StyleForm[
        SequenceForm[If[abz\[Equal]1,Unevaluated[Sequence[]],abz],
          If[z>0,ChemPlus,ChemMinus]],AutoSpacing\[Rule]False]];
Format[ChemSubscript[y_]]=StyleForm[y,AutoSpacing\[Rule]False];
Format[ChemSuperscript[z_]]=StyleForm[z,AutoSpacing\[Rule]False];
SetElementFormat[x_Symbol]:=
    Module[{y,z},Format[x[y_,z_]/;y\[Equal]1&&z\[Equal]0]=x;
      Format[x[y_,z_]/;z\[Equal]0]=Subscript[x,ChemSubscript@y];
      Format[x[y_,z_]/;y\[Equal]1]=Superscript[x,ChemSuperscript@z];
      Format[x[y_,z_]]=Subsuperscript[x,ChemSubscript@y,ChemSuperscript@z]];
SetElementFormat[x_Symbol,abbrev_String]:=(SetElementFormat[x];
      MakeBoxes[x,_]=abbrev;x);
Format[Molecule[element_]]=element;
Format[Molecule[elements__]]=
    StyleForm[SequenceForm[elements],ZeroWidthTimes\[Rule]True];
Unprotect[Times];
Format[HoldPattern[Times[left___,Molecule[elements__],right___]]]=
    Times[left,PrecedenceForm[Molecule[elements],1000],right];
Protect[Times];

elementnotations={{Hydrogen,"H"},{Chlorine,"Cl"},{Zinc,"Zn"}};
SetElementFormat@@@elementnotations

rxns[1]={2 Molecule[Hydrogen[1,0],
          Chlorine[1,0]]\[Rule]2 Molecule[Hydrogen[1,1]]+2 Molecule[
            Chlorine[1,-1]],
    Zinc[1,0]+2 Hydrogen[1,1]+2 Chlorine[1,-1]\[Rule]
      Zinc[1,2]+2 Chlorine[1,-1]+Hydrogen[2,0],
    Zinc[1,2]+2 Chlorine[1,-1]+Hydrogen[2,0]\[Rule]
      Molecule[Zinc[1,0],Chlorine[2,0]]+Hydrogen[2,0]}

On 5/26/06, Chris Chiasson <chris at chiasson.name> wrote:
> (side note: Google New won't let me reply to my post with the same
> subject from a little over a month ago, so I quoted it below)
>
> Here is the solution:
>
> Instructions:
> Remove the Format statement for Molecule in the previous post. Add a
> cell with the code below (use Ctrl+Shift+E). Now execute the reaction
> statement from the previous post; there should be no parenthesis in
> front of the first molecule this time.
>
> Notes:
> I derived this idea from section 1.7.3.2 of NotationSource.nb. I
> noticed that when makeHeldRowBoxOfBoxes created a RowBox, no
> parenthesis were added even when I had a multiplier on the Molecule.
> The only problem was that my elements had commas between them. When I
> changed the separator, the parenthesis returned. I already knew about
> the SyntaxForm option of TagBox, but I had been trying to use
> SyntaxForm->"*". Eventually, I put things together, using an
> \[InvisibleSpace] for the separator, and SyntaxForm->",". This works
> well. I have also tried to create an InfixNotation version, but to no
> avail. If you know how to create an InfixForm version, I'd like to
> hear it (using Block on NotationMakeExpression can be useful in this
> regard).
>
>
> Don't forget to load the Notation packge:
> Utilities`Notation`AutoLoadNotationPalette = False;
> << Utilities`Notation`
>
> Code:
>
> Cell[BoxData[
>     RowBox[{"Notation", "[",
>       RowBox[{
>         RowBox[{
>           TagBox[
>             TagBox[
>               RowBox[{"TagBox", "[",
>                 RowBox[{
>                   RowBox[{"RowBox", "[",
>                     RowBox[{
>                       RowBox[{
>                         RowBox[{"Drop", "[",
>                           RowBox[{"#", ",",
>                             RowBox[{"-", "1"}]}], "]"}], "&"}], "@",
>                       RowBox[{"Flatten", "@",
>                         RowBox[{"Thread", "@",
>                           RowBox[{"{",
>                             RowBox[{
>                               RowBox[{
>                                 RowBox[{"Function", "[",
>                                   RowBox[{
>                                     RowBox[{"{", "term", "}"}], ",",
>                                     RowBox[{"MakeBoxes", "[",
>                                       RowBox[{"term", ",",
> "StandardForm"}], "]"}], ",",
>                                     RowBox[{"{", "HoldAll", "}"}]}],
> "]"}], "/@",
>                                 RowBox[{"Unevaluated", "@",
>                                   RowBox[{"{", "blank__", "}"}]}]}],
> ",", "\"\<\[InvisibleSpace]\>\""}], "}"}]}]}]}], "]"}], ",",
> "MoleculeTag", ",",
>                   RowBox[{"SyntaxForm", "->", "\"\<,\>\""}]}], "]"}],
>               NotationMadeBoxesTag,
>               TagStyle->"NotationMadeBoxesWrapperStyle"],
>             NotationBoxTag,
>             TagStyle->"NotationTemplateStyle"], " ",
> "\[DoubleLongLeftArrow]", " ",
>           TagBox[
>             RowBox[{"Molecule", "[", "blank__", "]"}],
>             NotationBoxTag,
>             TagStyle->"NotationTemplateStyle"]}], ",",
>         RowBox[{"WorkingForm", "\[Rule]", "StandardForm"}]}], "]"}]], "Input"]
>
>
> Old post quotation:
> What is the proper way to eliminate the (output) parenthesis generated
> around the first molecule as it is multiplied by the number two? I
> could change the data structure if need be.
>
> ElementSubscript[1|1.]:="";
> ElementSubscript[x_]:=x;
> ElementSuperscript[0|0.]:="";
> ElementSuperscriptMod[1|1.]:="";
> ElementSuperscriptMod[x_]:=x;
> ElementSuperscript[x_?NumericQ]:=
>     RowBox[{ToBoxes@ElementSuperscriptMod@Abs@x,"+"}]/;x>0;
> ElementSuperscript[x_?NumericQ]:=
>     RowBox[{ToBoxes@ElementSuperscriptMod@Abs@x,"-"}]/;x<0;
> ElementSuperscript[x_]:=x;
> ElementFormat[symblist:{{_Symbol,_Symbol}..}]:=
>     Module[{x},
>       ReleaseHold[
>         Function[
>             Hold[SetDelayed[Format[#1[x_,y_]],
>                 DisplayForm[
>                   Subsuperscript[#2,ElementSubscript[x],
>                     ElementSuperscript[y]]]]]]@@@symblist]];
> Format[Molecule[blah:_Symbol[_,_]..]]:=DisplayForm[RowBox[ToBoxes/@{blah}]]
> elementnotations={{Hydrogen,H},{Chlorine,Cl},{Zinc,Zn}};
> ElementFormat[elementnotations];
>
> rxns[1]={2 Molecule[Hydrogen[1,0],
>           Chlorine[1,0]]\[Rule]2 Molecule[Hydrogen[1,1]]+2 Molecule[
>             Chlorine[1,-1]],
>     Zinc[1,0]+2 Hydrogen[1,1]+2 Chlorine[1,-1]\[Rule]
>       Zinc[1,2]+2 Chlorine[1,-1]+Hydrogen[2,0],
>     Zinc[1,2]+2 Chlorine[1,-1]+Hydrogen[2,0]\[Rule]
>       Molecule[Zinc[1,0],Chlorine[2,0]]+Hydrogen[2,0]}
>
> --
> http://chris.chiasson.name/
>


-- 
http://chris.chiasson.name/


  • Prev by Date: Re: Null's not null?
  • Next by Date: Re: Re: Boolean algebra
  • Previous by thread: Re: Re: Wolfram Workbench
  • Next by thread: extract homogenous solution from DSolve?