Convert Mathematica formula to TeX (MathJax) with color

Case1: formula generated by functions.

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




Case2: formula typed by hand(or copy from elsewhere) in inline InlineFormula style cell or in DisplayFormula style cell.

This is an inline formula with coloring

Some posts or answers at Math.SE you would see are with colored TeXForm output rendered.
Here is one sample

If you are interested that, you may find some more samples, it’s helpful by highlighting questionable part of the formulas when asking in Math.SE or Mathematica.SE.

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

midpoints\color{#C00000}{\text{midpoints}}, and median\color{#0000FF}{\text{median}}:



1 Answer


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:

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

Clearly, this could be incorporated into a larger TeX export program, i.e. one that exports entire notebooks to LaTeX form. I mentioned the beginning of such a project in my answer to this question and I’ve updated the links that I placed there to include color. For completeness, those links are:, which implements the minimal amount of code necessary to get an interesting example working., which is a sample paper for use with the previous notebook.

If you download those two notebooks, open both of them and then execute the code in the first, you should generate a folder containing a LaTeX and other documents. The notebook and resulting output (with new color information) will hopefully look something like so:



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
Jun 30 ’13 at 3:07



@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
Jun 30 ’13 at 3:58



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
Jun 30 ’13 at 4:17