# Add options to upvalues

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?

-
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. –  Leonid Shifrin Feb 14 at 11:47
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 Feb 14 at 12:29
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 Feb 14 at 12:37
I thought so... –  Per Alexandersson Feb 14 at 12:58
@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 Feb 14 at 18:17

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.

-