# User defined output forms

I have a mathematica program which performs some manipulation which I then want to continue in another language, as I already have some code written in it. Syntactically this language is very similar to InputForm and so I wanted to try and build my own output form based upon InputForm which would transform an expression into something understandable in this other language.

The type of transformations would be things like swapping square brackets with parentheses, and renaming of functions. That is, a mapping like:

Den[x]*Den[y]+Den[x]*Den[z]  --> D(x)*D(y) + D(x)*D(z)


My naive attempts at doing something like this were to try and copy InputForm into MyForm and then try and apply various calls to Format, that is something like (just using the renaming in this example):

MyForm = InputForm;
Format[Den[x_], MyForm] := D[x];


But as MyForm is simply pointing towards InputForm, I have not managed to copy InputForm and so the Format calls to MyForm affect InputForm as well.

At time of writing I do not have a complete list of the kind of output style transformations I wish to apply, but I am not looking for a complete solution, just an idea of how to get started with my own version of InputForm (without breaking the original).

Any ideas on how to proceed would be appreciated. Thanks!

Edit: eldo's answer has made me realise that there is a loophole in what I have written - All of my symbols/expressions already have StandardForm set up, and I don't want to break that either. That is, for example, I have:

Format[Den[x_], StandardForm] := 1/x^2;


In principle, this is why I'm looking to define a NEW output form, I don't want to get it tangled with any other that is already defined.

Format[Den[n_, OutputForm]] := "D(" <> ToString@n <> ")"

Format[Den[x_], StandardForm] := 1/x^2;

Den[x]


1 / x^2

Den[x] // OutputForm


D(x)

$Post = OutputForm Den[x]*Den[y] + Den[x]*Den[z]  D(x) D(y) + D(X) D(z) $Post =.

Den[x]*Den[y] + Den[x]*Den[z] • My apologies, I should have specified that I already have StandardForm set up for Den as well, so I'm not allowed to touch that either. – VolatileStorm Jun 20 '14 at 10:46
• @VolatileStorm - In this case please edit your question correspondingly :) – eldo Jun 20 '14 at 10:48
• I was just doing that! :) – VolatileStorm Jun 20 '14 at 10:49
• So, with this new response is the implicit answer that it is not possible to define new output forms, and one has to use one of the pre-defined ones? This will work, but strikes me as difficult to understand when reading back the code. OutputForm is no longer doing its original job of outputting "a two-dimensional representation of expr using only keyboard characters" (from the docs), but instead giving an output formatted for this other language. – VolatileStorm Jun 20 '14 at 11:18
• A further comment, I realise now that the current form of this answer does not base this new form on InputForm (instead OutputForm). Most noticeably the infix multiplication operators are missing, which are quite necessary. (Also the power notation circumflex would be wanted, hence wanting to start with InputForm.) – VolatileStorm Jun 20 '14 at 11:26

I usually use conditioned Format definitions to do this sort of thing. For example, define:

Format[Den[x_], InputForm] /; $MyForm := SequenceForm[OutputForm["D("], x, OutputForm[")"]]  The OutputForm hides the quotes, and the SequenceForm is like Row for InputForm. Then, define your MyForm function: MyForm[x_] := Block[{$MyForm=True}, ToString[x, InputForm]]


Den[x]*Den[y]+Den[x]*Den[z] //MyForm