0
$\begingroup$

I am trying to generate a notation system to handle a variety of things in chemistry-standard notation. One key aspect of this is to be able to define symbols that have multiple subscripts, parentheses, and superscripts. Something like C6H6O, or Mg(NO3)2, or SO42-. Mathematica naturally treats the first couple as multiplications, and chokes on the superscript in the third. I can enter this type of notation in a string, but what I would really like to do is to be able to enter them as symbols, probably using the Notation package. Is there a way to assign a keystroke to something like a TagBox (where the tag could be "chemical" or something), where the entry within the box would allow me to do subscripts, superscripts, parentheses, etc. without parsing, allowing the C6H6O, for example, to not have the multiplication spaces between C6 and H6 and O? Thanks!

Edited to clarify: Some aliases, such as those used by the Notation package, produce an altered-color box that is represented internally with some hidden tag information. I am looking for a way to do something similar, where I can use an alias, have an altered-color box indicating this is a chemical, enter something as if it were in a string (so that there is no parsing and I can get spacing right) but have it displayed without the quotation marks, and then use that in a Notation call so that I can assign an internal symbol to the string. Then I want to be able to use that altered-color, no-quotation mark string as something I enter to refer to the symbol.

Further edited to add context:

First, some setup.

enter image description here

That defines reaction arrow notation as well as some helper functions that extract information from a chemical reaction. Examples of usage will be shown below. Next...

enter image description here

That gives me a way to generate symbols for a couple of types of labels. I can do a chemical formula like C60, for example, as long as there is only one subscript and it comes at the end. There is a special case where I also put a superscripted asterisk. I also have a relatively flexible method (though not flexible enough) for using strings, which I will use for rate constants. Again, examples of usage will be shown below.

Now to set up the symbols I will be using.

enter image description here

At this point, I can now enter chemical reactions directly using this chemistry-standard notation. For example, here is a reaction mechanism:

enter image description here enter image description here

Notice that I can enter the reactions in chemistry-standard notation, and that the displayed version is also in chemistry-standard notation, but that the internal representation is in simple symbols.

Next I identify my chemical species as chemicals; that adds two chemistry-standard notations. First, it adds [C60]t to be a time-dependent concentration of species C60, internally represented as C60[t]. Second, it adds the derivative notation for this same concentration, which you will see examples of in a moment.

enter image description here

Now the magic can start. Here is the automatic generation of the system of differential rate equations from this reaction mechanism, all in chemistry-standard notation.

enter image description here enter image description here

I can then assign initial concentrations and values for the rate constants, and then do an NDSolve to give the concentrations of all of the species as a function of time.

The key is that this is part of course materials for classes I am teaching within a university Department of Chemistry. The more chemistry-standard notation I allow, the more easily I will be able to teach students to use Mathematica as a tool, and the easier I will be able to convince colleagues to use these tools I am developing.

Yet another edit:

Based on Somos's suggestion, I started playing with palettes. I thought that maybe something like this would work:

enter image description here

The distinction between the display form and the internal representation form looked good. (And I am not doing the palette bit yet, though that looks pretty straightforward.) But I missed that the pasted version would be going into an input cell, and so it ends up looking like:

enter image description here

$\endgroup$
  • $\begingroup$ What about Interpretation[Rasterize["C6H6O",C6H6O] where the string has subscripted 6s? $\endgroup$ – Somos Apr 10 at 0:51
  • 1
    $\begingroup$ Could you please explain, what is your final aim? What are you going to do with those notations? I can only imagine myself using such notations as a part of the text, or a text placed somewhere in images. For these purposes, however, such troubles seem to be unnecessary. So, please explain your aim. $\endgroup$ – Alexei Boulbitch Apr 10 at 8:14
  • $\begingroup$ There are a wide variety of things to do with this notation. For example, I have already generated a notational system that allows me to write chemical reactions using standard chemistry notation (as long as the reactants and products have at most a single subscript at the end). These reactions can then be manipulated. In particular, I have functions written that take a list of such reactions that constitute a reaction mechanism, and then from that mechanism automatically generate the system of differential equations that constitutes the mechanism’s rate law. There are thermodynamic apps too. $\endgroup$ – Kevin Ausman Apr 10 at 16:27
  • $\begingroup$ @Somos: That doesn’t allow me to enter the symbol in an input cell using the desired notation, does it? $\endgroup$ – Kevin Ausman Apr 10 at 16:32
  • 1
    $\begingroup$ @KevinAusman Well, if you select it from a palette that would work I think. But this is just a possible solution. You might want something more complicated. $\endgroup$ – Somos Apr 10 at 17:05
1
$\begingroup$

And I have found a way to make it work! It requires mixing several ideas, many of which are not well documented. And I suspect there are still some ways to improve it. Here we go:

<<Notation`

SetOptions[EvaluationNotebook[], StyleDefinitions -> 
  Notebook[{
    Cell[StyleData[StyleDefinitions -> 
      FrontEnd`FileName[{"Report"}, "StandardReport.nb", 
      CharacterEncoding -> "UTF-8"]]], 
    Cell[StyleData["chemical"], StyleMenuListing -> None, 
      TagBoxOptions -> {SyntaxForm -> "symbol"}, 
      ShowStringCharacters -> False, 
      TemplateBoxOptions -> {DisplayFunction -> (StyleBox[#, 
      FontColor -> RGBColor[0, 0, 0]] &), 
      SyntaxForm -> "symbol"}]}]]

Loading the Notation package, and then defining a style named "chemical," which we will use later as a tag in a TemplateBox. This was modeled on the tag "NotationTemplateTag," but I made four changes to it. I removed the yellow background so that the display looks more natural and seamless. Same rationale for removing the FrameBox. I set ShowStringCharacters to False so that I can display a string (to maintain formatting), but lose the quotation marks. And I tried to set the font color to black, though that doesn't always seem to work. You could do this through a stylesheet, but I prefer to have it handled programatically. That way I can give my students code to copy in and they don't have to futz with stylesheets.

Now I define a function to take a list of pairs {{"formatted label string",symbol},...} and do several things. First, I loop through the list. For each element, I (1) define my notation as a TemplateBox (with Editable and Selectable set to false) and assign it to mean the symbol, (2) define my [chemical]t for chemical[t] notation, and (3) define my derivative notation. Then after looping, I create a palette of PasteButtons. Clicking on any of the labels pastes into the notebook at my cursor position the formatted symbol. Here's the code:

AssignChemicals[list_] := 
  Block[{}, 
    Do[Notation[DoubleLongLeftRightArrow[
         ParsedBoxWrapper@TemplateBox[{"\""<>list[[i,1]]<>"\""}, 
         "chemical", Editable -> False, Selectable -> False], 
         ParsedBoxWrapper@SymbolName[list[[i, 2]]]]]; 
       With[{n = ToBoxes[list[[i, 2]], StandardForm]}, 
         Notation[ParsedBoxWrapper@SubscriptBox[RowBox[{"[", n, "]"}], 
           "t_"]\[DoubleLongLeftRightArrow]ParsedBoxWrapper@
           RowBox[{n, "[", "t_", "]"}]]; 
         Notation[ParsedBoxWrapper@FractionBox[SubscriptBox[RowBox[{"d", 
           "[", n, "]"}], "t_"], "dt"]\[DoubleLongLeftRightArrow]
           ParsedBoxWrapper@RowBox[{RowBox[{n, "'"}], "[", "t_", "]"}]]], 
   {i, Length[list]}]; 
   CreatePalette[Column[PasteButton @@@ (list), Spacings -> 0]]]

Now I assign some test chemicals:

enter image description here

And we are off to the races.

enter image description here

In all of these examples, the chemicals were entered using the palette in order to ensure that they had all the right tags and associations. My only quibble is that the text color is off in places.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.