0

If I use the function Print, the result is not in a text cell. So I wonder If there's a function that I can use to put behind the function Print, to make the evaluated result be placed in a text cell?

enter image description here

Here's the testing code!

1 + x^2 + x^4 /. x^p_ -> f[p] // Print
Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
Lawrence
  • 29
  • 2
  • Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – Alexey Popkov Dec 23 '21 at 12:19

2 Answers2

2
CellPrint[TextCell[1 + x^2 + x^4 /. x^p_ -> f[p], "Text"]]
Mike Honeychurch
  • 37,541
  • 3
  • 85
  • 158
1

If you wish to print into plain text cells, you have the following possibilities:

  1. Instead of Printing into usual StandardForm Cell where the parsing and 2D formatting take significant processor's time you can print into plain text Cells:

    CellPrint[Cell["stuff=" <> ToString[stuff]]]
    

    Upon printing, you can assign the usual "Print", "Text", "Output" or another style to such a cell without changing its rendering from plain text:

    CellPrint[Cell["stuff=" <> ToString[stuff], "Text"]]
    

    The plain text rendering is determined by the fact that the Cell[...] object contains a simple String as the first argument and not a BoxData object.

  2. Instead of printing into separate cells you can append the output into the same plain text Cell as described here:

    stuff := RandomReal[];
    Do[WriteString["stdout", "stuff=" <> ToString[stuff], "\n"], {10}]
    

    This works extremely fast.

    Here "stdout" is the name of the standard output stream. You can get the corresponding stream object with Streams["stdout"]. The standard output channel is $Output, which by default sends everything to "stdout". You can read more about them in this tutorial.

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368