8
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ For extending TeXForm (rather than creating a completely new one), there's some useful information here. $\endgroup$
    – Szabolcs
    Commented May 25, 2012 at 9:59

3 Answers 3

4
$\begingroup$

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<--)"
$\endgroup$
2
$\begingroup$

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.

$\endgroup$
3
  • 1
    $\begingroup$ 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] $\endgroup$
    – Rojo
    Commented Feb 8, 2015 at 14:33
  • $\begingroup$ @Rojo Nice! I tried to figure out those cell tags once and failed. Handy! Thanks, unlikely. $\endgroup$
    – Mr.Wizard
    Commented Feb 8, 2015 at 16:34
  • $\begingroup$ unlikely, this solves something I wondered about for a while: (44189) -- you can now answer this question. Please do so! $\endgroup$
    – Mr.Wizard
    Commented Feb 8, 2015 at 16:39
1
$\begingroup$

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.

$\endgroup$
8
  • $\begingroup$ 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... $\endgroup$
    – Rojo
    Commented Mar 1, 2012 at 0:28
  • $\begingroup$ @Rojo I'm not sure I understand your comment. Does my edit help clarify? $\endgroup$ Commented Mar 1, 2012 at 0:32
  • $\begingroup$ 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. $\endgroup$
    – Szabolcs
    Commented Mar 1, 2012 at 0:33
  • 2
    $\begingroup$ @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. $\endgroup$
    – Szabolcs
    Commented Mar 1, 2012 at 0:34
  • $\begingroup$ @Szabolcs I see. Yes my suggestion is not quite sufficient. $\endgroup$ Commented Mar 1, 2012 at 0:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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