Add options to upvalues

5

1

I have some objects that I want to present in TeXForm.

Thus, it is easily to do as follows:

YoungTableau/:TeXForm[YoungTableau[diagram_]]

However, what if I want to extend the functionality of TeXForm, but only when working with this particular object? Say that I wish to be able to specify if I want line breaks or not in the output.

I can do it like this,

YoungTableau/:TeXForm[YoungTableau[diagram_],OptionsPattern[]]

but then OptionValue[LineBreaks] will not work, since I have not added this as a possible option. It IS possible to do it as follows:

Unprotect[TeXForm];
Options[TeXForm] = {LineBreaks->True};
Protect[TeXForm];

but this feels too intrusive. Should I just give up, and invent my own version of TeXForm which can take options?

What alternatives are there that does something similar to what I want?

Per Alexandersson

Posted 2014-02-14T11:07:57.023

Reputation: 2 142

4OptionValue and OptionsPattern[] are magical constructs, which work by certain macro-like trick at run-time. So, I am not surprised that OptionValue did not work here. I would suggest to use it's long form: OptionValue[f, {opts}, optionName], and declare options as opts:OptionsPattern[], rather than just OptionsPattern[] - it will work then, and you can attach options to any symbol you want. – Leonid Shifrin – 2014-02-14T11:47:25.777

Ah, I suspected that it saw so sort of hack. Yes, this works. The only nuisance is that TeXForm[YoungTableau[...], LineBreak->True] looks like it is invalid, since ", LineBreak->True" appears in red in the front-end. But I guess I have to live with this, unless I want a new name instead of the TeXForm function. – Per Alexandersson – 2014-02-14T12:29:42.343

You could include options into YoungTableau instead, to avoid red coloring, although this is probably conceptually wrong, since it is probably not the business of YoungTableau object to manage options for presentation. So yes, the only other simple and reasonable alternative seems to be defining your own texform function. – Leonid Shifrin – 2014-02-14T12:37:27.313

I thought so... – Per Alexandersson – 2014-02-14T12:58:46.200

You could also Unprotect the TextForm function, and define your own, more liberal pattern for the SyntaxInformation for it. This is much more gentle interference with TexForm, and it will remove the red coloring. I'd actually probably go this way. – Leonid Shifrin – 2014-02-14T13:33:35.243

1@LeonidShifrin. Please write up your comments as an answer. I think what you wrote is extremely valuable and needs to be put on record as an answer. – m_goldberg – 2014-02-14T18:17:09.853

@m_goldberg All right, done. – Leonid Shifrin – 2014-02-14T21:24:50.177

Answers

6

OptionValue and OptionsPattern[] are magical constructs, which work by certain macro-like trick at run-time. So, I am not surprised that OptionValue did not work here. I would suggest to use it's long form:

OptionValue[f, {opts}, optionName], 

and declare options as

opts:OptionsPattern[], 

rather than just OptionsPattern[] - it will work then, and you can attach options to any symbol you want.

Regarding the red coloring problem for TexForm, there are a few choices. You could include options into YoungTableau instead, to avoid red coloring:

YoungTableau /: TeXForm[YoungTableau[diagram_ ,OptionsPattern[]]]

although this is probably conceptually wrong, since it is probably not the business of YoungTableau object to manage options for presentation.

You could also Unprotect the TexForm function, and define your own, more liberal pattern for the SyntaxInformation for it. This is much more gentle interference with TexForm, and it will remove the red coloring. I'd actually probably go this way.

Finally, you could define your own texform function, which would give you the most flexibility, and complete independence of built-ins.

Leonid Shifrin

Posted 2014-02-14T11:07:57.023

Reputation: 108 027