The PlainForm.m package
By Ted Ersek
      ersektr@navair.navy.mil

The trouble with formatting output for email

Suppose you want to paste the output below in an email message.

((Sqrt[2.0 x])/(1 + (x^2))) + ∫(Sqrt[3.0]) (x^x) \[DifferentialD]x

If you simply copy and paste the cell in your email you probably get the following.
    \(\(\(\\)\(\(! \(\(1.4142135623730951`  \\@x\) \/ \(1 + 
                      x \^ 2\) + 
                1.7320508075688772`  \\ \((\[Integral]\(x \^ 
                            x\)\ \[DifferentialD]x\)\)\)\(\)\)\)\)\)

The mess above is most difficult to read. Instead you could use InputForm in which case the expression above would be written as follows.
   (1.4142135623730951*Sqrt[x])/(1 + x^2) + 1.7320508075688772*Integrate[x^x, x]

The expression above much more readable, but we see that the InputForm of real numbers often includes sixteen digits.  Hence the InputForm of an expression with lots of real numbers can also be difficult to read.  What we really need is a form that looks like InputForm with real numbers rounded to six decimal places. In that case the expression above would be formatted as follows.
    (1.41421*Sqrt[x])/(1 + x^2) + 1.73205*Integrate[x^x, x]
    
You might also want to copy a whole series of  In / Out cells to an email message.  In that case you might make InputForm the default format for output, but then the next two cells return very long output you probably don't want to see.  This another disadvantage of InputForm. One can obviously avoid the lengthy output by ending the expressions with a semi-colon, but sometimes you might forget.

Plot[Sin[1/x], {x, 0.05, π}]

Clear[x, y];
NDSolve[{x y''[x] == Sin[x], 
        y[1] == 0, y'[1] == 1}, y[x], {x, 1, 4}]

PlainForm solves the problem

The (PlainForm.m) package defines functions that format Mathematica output in a way that's easy for people to read in email.  Before evaluating the examples below you should put the (PlainForm.m) package in one of the directories returned by evaluating $Path in the next cell.  I normally put packages in the \(TraditionalForm\`\(\(\\)\(AddOns\)\)\ExtraPackages\)  folder.

$Path

Once (PlainForm.m) is stored in an appropriate folder it can be loaded by evaluating the next cell.

<< PlainForm.m

The (PlainForm.m) package includes definitions of PlainForm and FewerDigits and their usage messages are shown below.

?FewerDigits

?PlainForm

PlainForm Examples

In the cell below the example from above gives an output formatted in a way that's easy for people to read in email.

((Sqrt[2.0 x])/(1 + (x^2))) + ∫(Sqrt[3.0]) (x^x) \[DifferentialD]x   // PlainForm

In the next two cells the same example has the output formatted using only the first eight and first four digits of real numbers.

PlainForm[((Sqrt[2.0 x])/(1 + (x^2))) + ∫(Sqrt[3.0]) (x^x) \[DifferentialD]x  , 8]

PlainForm[((Sqrt[2.0 x])/(1 + (x^2))) + ∫(Sqrt[3.0]) (x^x) \[DifferentialD]x  , 4]

In the next example we see PlainForm does the right thing with:
   - Scientific notation
   - Real numbers with fewer than six digits
   - Complex numbers with approximate imaginary parts
In this example we see that PlainForm will display Mathematica characters such as θ. You will notice the named characters are converted to their long name when they are copied into a plain text editor.

PlainForm[
      Exp[600.0] + 0.25  θ + (Sqrt[-3.0]) (θ^2) + 
        Exp[2.0 I] (θ^3) + ((Sqrt[2])/(π + (θ^2)))]

Evaluate the next cell and all your output is formatted using PlainForm.

$PrePrint = PlainForm;

tst = (Sqrt[2.0]) ((x^2)/((x^2) + 1))

After evaluating ($PrePrint=PlainForm) the output above is formatted in PlainForm. You can edit the output above and the extra digits will not be added to the approximate numbers.  PlainForm only effected the expression sent to the display not the expression assigned to (tst).  In the next cell we see (tst) is stored with more than six digits for approximate numbers.

FullForm[tst]

After evaluating ($PrePrint=PlainForm) all output not wrapped in FullForm is formatted using PlainForm, but PlainForm doesn't affect output that include graphics (of any kind), Sound, InterpolatingFunction, or CompiledFunction. As a result the five examples below give the StandardForm output.

Plot[Sin[1/x], {x, 0.05, π}, 
      DisplayFunction -> Identity]

Play[Sin[x], {x, 0, 2  π}, 
      DisplayFunction -> Identity]

Clear[y, x];
NDSolve[{y'[x]==y[x],y[1] == 2},y,{x, 0, 3}]

Magnitude = 
      Compile[{{vector, _Real, 
            1}}, (Sqrt[Plus @@ (vector^2) ])]

Series[Sin[x], {x, 0, 5}]

I would like to have PlainForm format the output of Series in a way that is easy to read in a text editor, but I couldn't find a way to make that happen.

FewerDigits Examples

FewerDigits is the function PlainForm uses to format approximate numbers, and it may have other uses. Before demonstrating FewerDigits we clear the value of ($PrePrint) in the next cell.

$PrePrint =.

In the next example we see FewerDigits does more than format a result for output.  FewerDigits returns an expression that is rounded to six digits when it can.

Clear[t];
t = FewerDigits[π/2.0];
FullForm[t]

<< PlainForm.m

In the next cell FewerDigits rounds real numbers and complex numbers with approximate imaginary parts to six decimal places.  However, any expression that isn't an inexact number is returned with no change.

Clear[x];
FewerDigits /@ {(x/(1 + (x^2))), 2/3, 2.2, π/2.0, Exp[1.2 I], 
        Exp[100.0]}

In the next line FewerDigits rounds numbers to three decimal places.

FewerDigits[#, 3] &   /@ {(x/(1 + (x^2))), 2/3, 2.2, π/2.0, 
        Exp[1.2 I], Exp[100.0]}

In the next line FewerDigits rounds numbers to nine decimal places.  Recall StandardForm would only display the first six digits, so we use InputForm to ensure nine digits are displayed.

InputForm[FewerDigits[#, 9] &   /@ {(x/(1 + (x^2))), 2.2, 
          2/3, π/2.0, Exp[1.2 I], Exp[100.0]}]