Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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..

share|improve this question
    
For extending TeXForm (rather than creating a completely new one), there's some useful information here. –  Szabolcs May 25 '12 at 9:59

2 Answers 2

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<--)"
share|improve this answer

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)$:

enter image description here

Alternatively, you could define an UpValue for Log:

Unprotect[Log];
Log /: MakeBoxes[Log[x_], TraditionalForm] :=
  RowBox[{"ln", "(", MakeBoxes[x, TraditionalForm], ")"}];
Protect[Log];

The result should be the same. Of course, you could do something similar of your myForm.

share|improve this answer
    
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
    
@Rojo I'm not sure I understand your comment. Does my edit help clarify? –  Mark McClure Mar 1 '12 at 0:32
    
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
2  
@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
    
@Szabolcs I see. Yes my suggestion is not quite sufficient. –  Mark McClure Mar 1 '12 at 0:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.