# Formatted text in a Dynamic or Manipulate control

I am using a Manipulate control to show different calculations for the same user-specified parameters. I would like to show the results for the calculation the user selected and also provide some formatted text to describe how I arrived at the result.

So as a test I placed the formatted text in a Cell set as Text and assigned that cell to a variable. I then show in a Table the results of the calculation and the text cell. What I'm finding is that the text displays the Mathematica functions which are used to format the text instead of just displaying the formatted text.

If I use CellPrint for this text variable I can get the cell to display properly in the notebook. CellPrint however returns Null so I can't pass it's value to my Dynamic or Manipulate controls.

Let me show an example of my code:

testText1 :=
Cell[TextData[{Cell[
FormatType -> "TraditionalForm"], " = Rate of star formation = 10 "}],
"Text", CellChangeTimes -> {{3.547210260347546*^9, 3.547210296541616*^9}}]

CellPrint[testText1]

Manipulate[
Switch[testStatChoice, 1,
testText1], {{testStatChoice, 1,
"Select Calculation"}, {1 -> "1: First Calculation Example"},


I have hunted around for other methods or properties that will allow me to display my paragraphs of text with equations within an Manipulate but haven't yet found a solution.

Edit: So I am now headed in the right direction. So a trimmed down version of what I have so far is the following:

[Code sample deleted. Look at final solution below.]

My issue is with the 2nd, 3rd and 4th paragraphs. The text continues onto the next line but it gets indented.

Edit2: I solved the line indenting issue with RowBoxes that continue onto another line by wrapping the RowBox with a StyleBox and setting the LineIndent property to 0.0 for that StyleBox.

You may have also noticed that I was using spaces for my indenting of my code lines. That was ugly and I replaced that with AdjustmentBoxes that have BoxMargins set so that the line was indented to my preference.

I am going to delete my large code insert above and replace it below with my fixed end result. I still have an issue with my last paragraph but I think I should be able to solve it.

stat1TestText :=
DisplayForm[
Column[{Cell[
"This statistic is calculated by adding up from the beginning \
of the Milky Way the time it took for the first Sun-like stars to \
come into existence, the time that elapsed on Earth for life to start \
on Earth once the Sun had formed and the time it takes for new life \
to come into existence based on the given set of Drake Equation \
Parameters."],
StyleBox[
RowBox[{"The last number to calculate is the amount of time (on \
average) that it would take for life to come into existence given the \
first 4 parameters of the Drake Equation", " (",
SuperscriptBox["R", "*"], " = rate of star formation, ",
SubscriptBox["f", "p"], " = fraction of stars with planets, ",
SubscriptBox["n", "e"],
" = # of planets that could support life, and ",
SubscriptBox["f", "l"],
" = fraction that develop life).  So Frank Drake\
\[CloseCurlyQuote]s original values for these parameters were:"}],
LineIndent -> 0.0],
RowBox[{SuperscriptBox["R", "*"], "= 10 new stars per year  "}],
BoxMargins -> {{4, 0}, {0, 0}}],
RowBox[{SubscriptBox["f", "p"],
"= 0.50 fraction of stars with planets", " = ",
RowBox[{"50", "%"}]}], BoxMargins -> {{4, 0}, {0, 0}}],
RowBox[{SubscriptBox["n", "e"],
"= 2 planets that could support life for each star"}],
BoxMargins -> {{4, 0}, {0, 0}}],
RowBox[{SubscriptBox["f", "l"],
"= 1.00 fraction of stars that develop life = ",
RowBox[{"100", "%"}]}], BoxMargins -> {{4, 0}, {0, 0}}],
Cell["Using the above parameters,every year, the number of stars \
to have the genesis of life in the Milky Way can be calculated by \
mulitplying those 4 numbers together."],
RowBox[{SuperscriptBox["R", "*"], "\[Times]",
SubscriptBox["f", "p"], "\[Times]", SubscriptBox["n", "e"],
"\[Times]", SubscriptBox["f", "l"], "=",
RowBox[{"10", "\[Times]", "0.50", "\[Times]", "2.0",
"\[Times]", "1.00"}],
" = 10 stars in the Milky Way have life begin on a habitable \
planet in their solar system each year."}],
BoxMargins -> {{4, 0}, {0, 0}}],
StyleBox[
RowBox[{"This means that every ", FractionBox[1, 10], "th",
Cell["of a year new life will show up in the galaxy. For \
Drake Equation Parameters that are not so optimistic, such as the \
numbers I label More Reserved, you can estimate life coming into \
existence in the galaxy once every 150 years up to 28000 years. \
Really this number is small enough that it doesn't play a significant \
role in our calculation of microbial life first began in the Milky \
Way but I will use it anyway."]}], LineIndent -> 0.0],
BoxMargins -> {{0, 0}, {0, 0}}],
Cell["This means that every 1/10th of a year new life will show \
up in the galaxy. For Drake Equation Parameters that are not so \
optimistic, such as the numbers I label More Reserved, you can \
estimate life coming into existence in the galaxy once every 150 \
years up to 28000 years. Really this number is small enough that it \
doesn't play a significant role in our calculation of microbial life \
first began in the Milky Way but I will use it anyway."],}, Left,
1.5]];

-

## migrated from stackoverflow.comMay 28 '12 at 17:24

This question came from our site for professional and enthusiast programmers.

You can place your desired output in a Row and then put it into DisplayForm.

Manipulate[
Switch[testStatChoice, 1, testText1], {{testStatChoice, 1,
"Select Calculation"}, {1 -> "1: First Calculation Example"}, ControlType -> PopupMenu},

Initialization :> {testText1 :=
Row[{SuperscriptBox["R", "*"]// TraditionalForm, " = ", "Rate of star formation",
" = ", 10}] // DisplayForm}]


In the above case, I broke up the expression into various parts under the assumption that, in practice, you would have to do some work to obtain each of them. But we could have used something like

testText1 :=
Row[{SuperscriptBox["R", "*"]// TraditionalForm, " = Rate of star formation = 10"}]
// DisplayForm


to the same effect.

-
Thanks for your comment it set me in the right direction. I am now creating my formatted text using Boxes, Columns, and DisplayForm. I am now getting caught on how to create a paragraph that wraps lines without indenting while allowing me to use SuperscriptBoxes in the paragraph. Let me know if you want to see my text or if I should break this out into a new post. –  BrianWaMc Jun 1 '12 at 0:15
You could paste your solution on to the end of your question or add it as an additional response. –  David Carraher Jun 1 '12 at 0:36
I edited my original question so that it has my updated question in regards to formatting text. –  BrianWaMc Jun 1 '12 at 10:46

The problem here is independent of Manipulate or Dynamic. It is about how to display a Cell object without using a CellPrint statement. testText1 in itself is always displayed as Cell[...]. Therefore I suggest reconstructing the expression as something else, not wrapped in Cell.

testText1 := TraditionalForm@Row[{Superscript[R, "*"], " = Rate of star formation = 10 "}];

testText1


In a Manipulate a Style wrapper might be needed:

Manipulate[
Switch[testStatChoice, 1,
Style[testText1, "Panel"]], {{testStatChoice, 1,
"Select Calculation"}, {1 -> "1: First Calculation Example"},