1
$\begingroup$

Short version of question: Suppose I have an expression:

expression = a+3b;

When I divide by 2, I get:

expression/2

enter image description here

It is important for my application, however, to have this result displayed as:

enter image description here

If it helps at all, in context the quantities a and b are being displayed using a custom Notation from the Notation package and using a custom style that has been added to the stylesheet which already contains a custom DisplayFunction, so a solution that involves modifying either of those would be perfectly fine.

I recognize that this question is similar to this one. It is distinct, however, in that my situation is needing formatting done to the result of a calculation.

Application: My symbols are chemical formulas and the coefficients are stoichiometric coefficients, and standard notation in chemistry says that the coefficients are separately in front of the formulas.

Full Problem in Context: There are a number of pieces that have come together to create the full problem, but I think my minimal example above captures the essence. For context, however, here is what I am really working with.

First, I define string-based labels for my chemical species using my custom notations and styles through a function AssignSpecies, and then create a palette that allows me to enter those symbols easily.

enter image description here

Then I enter a couple of chemical reactions using those symbols and a custom-modified operator based on LongRightArrow (same precedence as ==).

enter image description here

I have set up some algebra rules for LongRightArrow to allow reactions to be added, subtracted, and multiplied and divided by a constant. So for example:

enter image description here

I have a function that will remove species on both sides of the reaction:

enter image description here

Very often, though, we want to have the reaction expressed such that one particular species (in this example, the main product) has a coefficient of 1. And this will lead to fractional coefficients in many cases. For example:

enter image description here

The H2(g) product is the problem here. For the notation to be as chemistry-standard as possible, I need that species to have a 3/2 coefficient in front of the species.

Edited to Add: Answering @CarlWoll's question about the FullForm of this result:

LongRightArrow[Plus[complexsymbol13, complexsymbol14,
  Times[11, complexsymbol15], Times[2, complexsymbol18]],
  Plus[Times[Rational[3, 2], complexsymbol17], complexsymbol19]]

Second Edit: Some further information:

AssignSpecies calls AssignLabels, which looks like this:

AssignLabels[list_]:=Block[{symbollist={},tmpsymbol},
  Do[AppendTo[symbollist,tmpsymbol=constructnextsymbol];
     AssociateTo[runningsymbollist,tmpsymbol->list[[i]]];
     Notation[DoubleLongLeftRightArrow[
     ParsedBoxWrapper@TemplateBox[{"\""<>list[[i]]<>"\""},"complexSymbols",
                   Editable->False,Selectable->False],
     ParsedBoxWrapper@SymbolName[tmpsymbol]]],
  {i,Length[list]}];
  symbollist]

constructnextsymbol creates a symbol with the next index value. Other than the nature of the "complexSymbols" tag, everything else should be self-explanatory above. The complexSymbols style is:

Cell[StyleData["complexSymbols"],StyleMenuListing->None,
  TagBoxOptions->{SyntaxForm->"symbol"},ShowStringCharacters->False,
  ShowAutoStyles->False,ZeroWidthTimes->True,FontWeight->Plain,
  FontFamily->"Times", TemplateBoxOptions->
    {DisplayFunction->(RowBox[{#}]&),SyntaxForm->"symbol"}]
$\endgroup$
  • $\begingroup$ Can you make it part of the output formatting of LongRightArrow? (E.g. through MakeBoxes or Format.) $\endgroup$ – Michael E2 May 20 at 19:06
  • $\begingroup$ @MichaelE2: Possibly. I would love a pointer to how to approach that as a potential solution. I can possibly see cases where I would be dealing with just reactants or just products (i.e., in the absence of LongRightArrow), so if there were a way to tag the symbol itself as something that should always be held apart, that might be preferable. If that isn't feasible, your approach would at least handle the most common place this problem is likely to occur. $\endgroup$ – Kevin Ausman May 20 at 19:10
  • $\begingroup$ What is the FullForm of your problem part? $\endgroup$ – Carl Woll May 20 at 19:20
  • $\begingroup$ @CarlWoll: Excellent question! I should have thought to include that information. Thank you! I will add that information to the main question. $\endgroup$ – Kevin Ausman May 20 at 19:22
  • $\begingroup$ I have also added my Notation and style code to the OP. $\endgroup$ – Kevin Ausman May 20 at 19:34
2
$\begingroup$

This is strictly a formatting issue, and unfortunately, the following natural approach doesn't work because the tag is buried too deep:

sym /: MakeBoxes[r_Rational sym, StandardForm] := MakeBoxes[Defer[r] sym]

TagSetDelayed::tagpos: Tag sym in MakeBoxes[r_Rational sym,StandardForm] is too deep for an assigned rule to be found.

$Failed

So, you will have to change the formatting of MakeBoxes instead. Something like:

SetAttributes[cQ,HoldAll]
cQ[complexsymbol17] = True;

MakeBoxes[r_Rational s_?cQ, StandardForm] := MakeBoxes[Defer[r] s]

Then:

3/2 complexsymbol17

enter image description here

$\endgroup$
  • $\begingroup$ That is a truly beautiful solution, and it works perfectly! $\endgroup$ – Kevin Ausman May 20 at 21:22

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.