# Creating a TeXForm/TraditionalForm/etcForm - like function

How could we go about creating a function that behaves like those?

After a while thinking, my best try is with CellPrint printing an Output cell with the famous CellLabel of Out[blah]//myForm. This solution is good enough for me for now, but I'm using it as an excuse to understand all these issues better... This mimics the behaviour, its not the same... For example, you have to manually get the $Line, you get the GeneratedCell option to True, and I don't know what else I'm missing. In fact, the kernel actually doesn't seem to do anything in the real form functions. So this solution would behave wrongly, for example, if I wrapped it other things... FullForm[myForm[stuff]] should return myForm[stuff] in a FullForm-tagged cell.. • For extending TeXForm (rather than creating a completely new one), there's some useful information here. – Szabolcs May 25 '12 at 9:59 ## 3 Answers This is far from fully integrated into the system but it is a first order approximation of the behavior of other forms. Perhaps it will inspire someone else with a better method. formfunc = StringReplace[ ToString @ #, {"[" -> "(", "]" -> ")", x : DigitCharacter .. :> "-->" <> x <> "<--"} ] &; MakeBoxes[myForm[expr_], StandardForm] := InterpretationBox[#, expr] & @ ToBoxes @ formfunc @ expr ToString[expr_, myForm] ^:= formfunc[expr]  This provides output that can be re-evaluated to recover the original expression: 2^(1/2) // myForm  "Sqrt(-->2<--)"  This produces a normal String: ToString[2^(1/2), myForm]  "Sqrt(-->2<--)"  This might be of help:$OutputForms is a list of the formatting functions that get stripped off when wrapped around the output.

$OutputForms= {InputForm,OutputForm,TextForm,CForm,Short,Shallow,MatrixForm,TableForm,TreeForm,FullForm,NumberForm,EngineeringForm,ScientificForm,QuantityForm,PaddedForm,AccountingForm,BaseForm,DisplayForm,StyleForm,FortranForm,MathMLForm,TeXForm,StandardForm,TraditionalForm}  I recently discovered while working on a way to better show rational matrices. • They include the PrintForms which include the BoxForms. I did some digging at the time. Thanks! I think the output forms are those heads that end up showing applied to the cell label such as in mmaOut[34]//InputForm. Try Unprotect@$OutputForms;AppendTo[$OutputForms, la]; la[5] – Rojo Feb 8 '15 at 14:33 • @Rojo Nice! I tried to figure out those cell tags once and failed. Handy! Thanks, unlikely. – Mr.Wizard Feb 8 '15 at 16:34 • unlikely, this solves something I wondered about for a while: (44189) -- you can now answer this question. Please do so! – Mr.Wizard Feb 8 '15 at 16:39 Understanding now that you want to define a completely new format, but still not sure what you want that format to look like, perhaps this does what you want: Format[myForm[x_]] := {x, x} x = blah; y = myForm[x] Head[y]  {blah,blah} myForm  Note that the result printed as {blah,blah} but the Head of the result is myForm. First attempt I'm not sure exactly how you want myForm to behave but the standard way to do this is to define a value of Format. For example, Unprotect[Log]; Format[Log[x_], TraditionalForm] := ln[x] Protect[Log];  Now, TraditionalForm[Log[x]] will print like$ln(z)\$:

Alternatively, you could define an UpValue for Log:

Unprotect[Log];

The result should be the same. Of course, you could do something similar of your myForm.
• I was actually thinking of another form. In fact, the Format documentation suggests "You can add your own forms for formatted output. " apart from CForm, FortranForm, and friends... – Rojo Mar 1 '12 at 0:28
• Mark, MakeBoxes is assignable without the need to use UpValues. Just use MakeBoxes[something] = somethingElse (see the docs). I also found it less confusing than format, but never managed to figure out either of them completely. – Szabolcs Mar 1 '12 at 0:33
• @Mark He means make a MyForm so that ToString[expr, MyForm] will work. E.g. make a PythonForm which outputs things formatted correctly for Python, or something similar. – Szabolcs Mar 1 '12 at 0:34