# How do I force a fractional coefficient to exclude non-numeric symbols?

Short version of question: Suppose I have an expression:

expression = a+3b;


When I divide by 2, I get:

expression/2


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

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.

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

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:

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

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:

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.

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"}]

• Can you make it part of the output formatting of LongRightArrow? (E.g. through MakeBoxes or Format.) – Michael E2 May 20 at 19:06
• @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. – Kevin Ausman May 20 at 19:10
• What is the FullForm of your problem part? – Carl Woll May 20 at 19:20
• @CarlWoll: Excellent question! I should have thought to include that information. Thank you! I will add that information to the main question. – Kevin Ausman May 20 at 19:22
• I have also added my Notation and style code to the OP. – Kevin Ausman May 20 at 19:34

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


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