0
$\begingroup$

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
$\endgroup$
1
  • $\begingroup$ 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! $\endgroup$ Dec 23, 2021 at 12:19

2 Answers 2

2
$\begingroup$
CellPrint[TextCell[1 + x^2 + x^4 /. x^p_ -> f[p], "Text"]]
$\endgroup$
1
  • $\begingroup$ Cool, thanks, bravo $\endgroup$
    – Lawrence
    Dec 23, 2021 at 7:18
1
$\begingroup$

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.

$\endgroup$
5
  • $\begingroup$ Your answer is really informative, which is more than what I expected, really instructive! $\endgroup$
    – Lawrence
    Dec 23, 2021 at 10:44
  • $\begingroup$ WriteString[channel,expr1,expr2], so the "stdout" means the channel? Actually, I don't know what is channel. Can you give some words about it? $\endgroup$
    – Lawrence
    Dec 23, 2021 at 10:46
  • 1
    $\begingroup$ @Lawrence "stdout" is the name for 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". $\endgroup$ Dec 23, 2021 at 11:16
  • $\begingroup$ @Lawrence You can read more about them in this tutorial. $\endgroup$ Dec 23, 2021 at 11:31
  • $\begingroup$ Ok, got it, thanks bro $\endgroup$
    – Lawrence
    Dec 23, 2021 at 12:13

Your Answer

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

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