Convert Mathematica formula to TeX (MathJax) with color



I would like to customize TeXForm so that it includes color directives in the output. For example, here's a formula generated by TeXForm:

Nest[1 + 1/Style[#, Red] &, x, 5]




Notice that color information has been lost.

MathJax supports color directives, as can be seen in other answers, for example

There are more examples. I think it's helpful to highlight the questionable parts of a formula with color when asking a question on Math.SE or Mathematica.SE, as in the above answer, reproduced below:

Hint: Consider the analytic formulas relating the $\color{#00A000}{\text{vertices}}$ , $\color{#C00000}{\text{midpoints}}$, and $\color{#0000FF}{\text{median}}$:

$\hspace{3.5cm}$enter image description here


Posted 2013-06-29T07:19:49.223

Reputation: 7 917



The short answer is that, yes, it definitely is feasible. The most basic approach is to use the "BoxRules" option of Export[_,"TeX",___] to generate the TeX color commands. To do this with your example, we might use the following BoxRules function:

myBoxRule[s_String] := s;
myBoxRule[RowBox[stuff_List, ___]] := StringJoin[myBoxRule /@ stuff];
myBoxRule[FractionBox[numerator_String, denominator_]]  := 
  "\\frac{" <> numerator <> "}{" <> myBoxRule[denominator] <> "}";
myBoxRule[StyleBox[s_String, fColor_RGBColor, opts___]] := 
  "\\color[rgb]" <> latexColor[fColor] <> s <> "}";
myBoxRule[StyleBox[stuff_RowBox, fColor_RGBColor, opts___]] := 
  "\\color[rgb]" <> latexColor[fColor] <> myBoxRule[stuff] <> "}";

We can apply this to an example like yours as follows. (I modified your example a bit to emphasize the nested structure.)

expr = Nest[1 + 1/Style[#, 
   RGBColor[Random[], Random[], Random[]]] &, 
   x, 5];
ExportString[expr, "TeX", "UsePackages" -> {"color"}, "BoxRules" ->
  {box : (_StyleBox | _RowBox | _FractionBox) :> myBoxRule[box]}]

(* Out: 
%% AMS-LaTeX Created by Wolfram Mathematica 9.0 :






The TeXed output looks like so:

enter image description here

This does not seem to work with MathJaX on this site. It's possible that the color package simply isn't installed.

Mark McClure

Posted 2013-06-29T07:19:49.223

Reputation: 31 084

Nice--! Bad thing is I cannot download your NB files. :( How about inline cells, This is good, ExportString[Sqrt[3],"TeX","BoxRules" -> {SqrtBox[x_]:>x<>"^(1/2)"}],but this is failed,ExportString[Cell[Sqrt[3]],"TeX","BoxRules" -> {SqrtBox[x_]:>x<>"^(1/2)"}], how to deal with this case? – HyperGroups – 2013-06-30T03:07:40.050

1@HyperGroups I just tested the links and they work as far as I can tell. Why can't you download them? I think your second ExportString doesn't work because you don't have a well formed Cell. ExportString[Cell[Sqrt[3]],"TeX"] fails to produce useful TeX as well, so it's not the BoxRules. The notebooks contain code that deal with this situation. – Mark McClure – 2013-06-30T03:58:01.790

Fine, I'll do more tests. Maybe because my country filtered some other countries' websites, such like wordpress...and more... so I cannot download. – HyperGroups – 2013-06-30T04:17:51.917


You can modify a TeXForm internal function to get color output. Here is a function that converts an RGBColor object into the equivalent hex string:

tohex[RGBColor[r_, g_, b_]] := StringJoin["#", IntegerString[Round[255 {r,g,b}], 16, 2]]

Then, modify an internal function:

System`Convert`TeXFormDump`maketex[StyleBox[boxes_, ___, rgb_RGBColor, ___]] := StringJoin[

Now, using TeXForm with color works:

Style[Style[3, Blue]/Style[5, Green] + 2, Orange] //TeXForm


Mark's example:

expr = Nest[1 + 1/Style[#, RGBColor[Random[], Random[], Random[]]] &, x, 5];
HoldForm @ Evaluate @ expr //TeXForm

$1+\frac{1}{\color{#e6ac6b}{1+\frac{1}{\color{#a11240}{1+\frac{1}{\color{#870dbf}{1+\frac{ 1}{\color{#6b8306}{1+\frac{1}{\color{#3dd779}{x}}}}}}}}}}$

Carl Woll

Posted 2013-06-29T07:19:49.223

Reputation: 112 778