2
$\begingroup$

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]]];
$\endgroup$
  • $\begingroup$ Would Module[{lbl = OptionValue[Foo, Label]}, If[lbl === None, symbol, Superscript[symbol, lbl]]] suit your needs? $\endgroup$ – J. M. is away Feb 24 '16 at 10:05
  • $\begingroup$ @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? $\endgroup$ – JP-Ellis Feb 24 '16 at 10:09
  • $\begingroup$ 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). $\endgroup$ – JP-Ellis Feb 24 '16 at 10:12
  • $\begingroup$ "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. $\endgroup$ – J. M. is away Feb 24 '16 at 10:15
  • $\begingroup$ @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). $\endgroup$ – JP-Ellis Feb 24 '16 at 10:20
2
$\begingroup$

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

enter image description here

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

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – JP-Ellis Feb 24 '16 at 10:24
  • $\begingroup$ 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? $\endgroup$ – J. M. is away Feb 24 '16 at 10:25
  • $\begingroup$ @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 $\endgroup$ – Jason B. Feb 24 '16 at 10:27
  • $\begingroup$ 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... $\endgroup$ – JP-Ellis Feb 24 '16 at 10:28
  • 1
    $\begingroup$ @JP-Ellis, we just didn't want to embarrass you (slow day around here, is why I'm cracking wise) $\endgroup$ – Jason B. Feb 24 '16 at 10:30

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.