Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to use CellPrint to print a cell that asks a user to solve an equation. This equation is randomly generated right beforehand, and I would like to have it print in Blue. The whole Cell should be in the "Section" style. Here is my first attempt:

equat = ToString[
Expand[Product[(x - RandomInteger[{-10, 10}]), {i, 
  3 + RandomInteger[]}]], TraditionalForm];
RowBox[{StyleBox["Solve the equation "], 
  StyleBox[equat, FontColor -> Blue]}]], "Section"]];

This does in fact create the cell, and the last part is blue, but it is hindered by the TraditionalForm "markup": I won't put any color here, but the command above gives the output

Solve the equation \!\(TraditionalForm\`\(x\^4 - \(\(73\ x\^2\)\) - \(\(72\ x\)\)\)\)

Is there a simple way to fix this? I have tried using ToBoxes, but then I don't see how to color the output Blue. Any tips or pointers would be much appreciated.

share|improve this question
How about equ = TraditionalForm@ Expand[Product[(x - RandomInteger[{-10, 10}]), {i, 3 + RandomInteger[]}]];CellPrint[ Cell[CellGroupData[{StyleBox["Solve the equation "], BoxData[ToBoxes@equ]}]]] – DavidC Feb 16 '13 at 17:15
@DavidCarraher: That prints two different cells, no? – Steve D Feb 16 '13 at 17:18
Ah but this does give me a way to work it out: equ = TraditionalForm@ Expand[Product[(x - RandomInteger[{-10, 10}]), {i, 3 + RandomInteger[]}]]; CellPrint[ Cell[RowBox[{StyleBox["Solve the equation ", "Section"], StyleBox[ToBoxes@equ, FontColor -> Blue, FontWeight -> Bold, "Section"]}]]] – Steve D Feb 16 '13 at 17:25
Now the hard part :) - how to put an "=0" at the end (in blue)? – Steve D Feb 16 '13 at 17:25
Maybe...equ = TraditionalForm[ Expand[Product[(x - RandomInteger[{-10, 10}]), {i, 3 + RandomInteger[]}]] == HoldForm[0]]; – DavidC Feb 16 '13 at 17:49
up vote 3 down vote accepted

You need to use BoxData. Because ToString creates something strange you also obviously have to change "\\" -> "". I don't know if this is a bug or working as designed.

equat = (StringReplace[ToString[#1, TraditionalForm], "\\" -> ""] & )[
    Product[x - RandomInteger[{-10, 10}], {i, 3 + RandomInteger[]}]]]; 
  Cell[BoxData[{RowBox[{"Solve the equation ", 
       StyleBox[equat, FontColor -> Blue]}]}], "Section"]];

Mathematica graphics

share|improve this answer
Thanks so much! This is exactly what I wanted. – Steve D Feb 16 '13 at 23:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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