# Conditional Formatting based on Option

Consider the function Foo[symbol_String, opt:OptionsPattern[]] which has as one of its options Label which defaults None.

My question is: how do I change the display format of this function so that if Label is None, then it is displayed as just symbol but if the Label is not None, then it becomes Superscript[symbol, label]?

As far as I know, there is no way to do pattern matching against anything in collected in OptionsPattern (though feel free to correct me); and using an If clause doesn't seem to work either as the second call to OptionValue[Foo, Label] seems to mess things up.

Here's the code of what I have tried:

Options[Foo] = {Label -> None};
Format[Foo[symbol_String, opt:OptionsPattern[]]] :=
Superscript[symbol, OptionValue[Foo, Label]];
Format[Foo[symbol_String, opt:OptionsPattern[]]] :=
If[OptionValue[Foo, Label] == None,
symbol,
Superscript[symbol, OptionValue[Foo, Label]]];

• Would Module[{lbl = OptionValue[Foo, Label]}, If[lbl === None, symbol, Superscript[symbol, lbl]]] suit your needs? – J. M. will be back soon Feb 24 '16 at 10:05
• @J.M. Well... that was rather simple and I probably should've tried it :/ If you want to write that as an answer and I'll accept it. Also, why is the second call of OptionValue problematic? – JP-Ellis Feb 24 '16 at 10:09
• Actually, it worked once but then displayed lbl\$1067. I'm confused as to what's going on. EDIT: My bad, I missed the === (I wrote == instead). – JP-Ellis Feb 24 '16 at 10:12
• "why is the second call of OptionValue problematic" - this is more of trying not to do the same thing more than once, actually. With respect to the weird display, then it would seem your minimal example is a bit too minimal; try to come up with an example where this problem of yours shows up. – J. M. will be back soon Feb 24 '16 at 10:15
• @J.M. Thanks (the weird display was my mistake as mentioned above). Feel free to write up the answer and I'll accept it (or this question can be closed if it doesn't really belong here). – JP-Ellis Feb 24 '16 at 10:20

Another option would be to set the default value of the Label option to an empty string,

Options[Foo] = {Label -> ""};
Foo[symbol_String, opt : OptionsPattern[]] :=
Superscript[symbol, OptionValue[Label]]

Foo["bob"]
Foo["bob", Label -> "does not care for these jokes"]


Options[Foo] = {Label -> None};
Foo[symbol_String, opt : OptionsPattern[]] :=
Superscript[symbol,
If[OptionValue[Label] === None, "", OptionValue[Label]]]


gives the same output.

Or, with no empty superscript,

Options[Foo] = {Label -> None};
Foo[symbol_String, opt : OptionsPattern[]] :=
If[OptionValue[Label] === None, symbol,
Superscript[symbol, OptionValue[Label]]]


Again, same results

• I considered doing that; however, I intend to export things to TeX often and unfortunately Foo["\[Phi]"] // TeXForm gives \phi ^{\text{}} which isn't ideal. – JP-Ellis Feb 24 '16 at 10:24
• Jason, if you want, you have my express permission to expand on what I wrote in the comments. ;) Oops, apparently you did it already; maybe use Module[] like I did in the comments to avoid duplication of effort? – J. M. will be back soon Feb 24 '16 at 10:25
• @J.M. - a guy could have a nice SE reputation just following you around and writing up your comment answers lol - I did basically the same as you but without the Module – Jason B. Feb 24 '16 at 10:27
• Actually, the bigger mistake (that neither you seems to have mentioned) is that I have OptionValue[Label] == None whereas you have OptionValue[Label] === None. That seems to the underlying cause of my issues... – JP-Ellis Feb 24 '16 at 10:28
• @JP-Ellis, we just didn't want to embarrass you (slow day around here, is why I'm cracking wise) – Jason B. Feb 24 '16 at 10:30