Background & Question
So if you're reading this you probably think Mathematica is pretty great - at least I do. The function Table
by itself take someone pretty far and TableForm
gives a clean presentation of the data. After processing my data I may want to write-up my finding. I prefer to write my documents in LaTeX
, and Mathematica gives a great way to convert your work to LaTeX
notation via TeXForm
.
This is especially great when working with matrices or tables, because typing them by hand in LaTeX
is pretty awful, especially if you may later find yourself editing them. But what if you want to get a bit fancy? For example, it is common that one may color coat values either by magnitude or direction (positive / negative); likewise, one may color-coat rows via groups.
Unfortunately, Mathematica does not automatically keep these color style options when TeXForm
is invoked. Thus the crux of this question is:
QUESTION: How can I add color-styling to TeXForm
output?
LaTeX
Preliminaries
This is Mathematica StackExchange so a detailed discussion of LaTeX
will be excluded. However, for bare-bone basics you will need at least the following two packages:
\usepackage{fontspec}
\usepackage{xcolor}
Specifying color of a font in a block
can be done quite simply. For example, to add a vibrant blue:
{\addfontfeature{Color=0000FF99} Colored text goes here}
Approaches
Initially two approaches came to mind.
1.) Simply included the String
of the desired LaTeX
command when building the Table
2.) If TeXForm
was a String
use a combination of StringCases
and Replace
to replace the not styled LaTeX
form with the styled version.
Example
A super simple Table
TableForm[{
{"one", Style["red", FontColor -> Red]},
{"two", Style["blue", FontColor -> Blue]}
}]
Get the TeXForm:
%//TeXForm
\begin{
array
}{cc}\text{one} & \text{red} \
\text{two} & \text{blue} \
\end{
array
}
Unfortunately neither approach as described above work. For example, forego Style
and just add the LaTeX
command:
TableForm[{
{"one", "{\addfontfeature{Color=FF0000}red}"},
{"two", "{\addfontfeature{Color=0000FF}Blue}"}
}]
Gives a warning about the unknown escape character \a
so:
TableForm[{
{"one", "{\\addfontfeature{Color=FF0000}red}"},
{"two", "{\\addfontfeature{Color=0000FF}Blue}"}
}]
No warning. But both give very bad LaTeX
versions as Mathematica: obviously. Why? Because here we have to specify the LaTeX
commands as a String
, since characters like {
,}
, and \
have special meaning in Mathematica, especially when constructing lists. Therefore TeXForm
takes our string thus converts it to be a string in LaTeX
, which is not close to what we want e.g.
\begin{array}{cc}
\text{one} & \text{$\{\backslash \backslash $addfontfeature$\{$Color=FF0000$\}$red$\}$} \\
\text{two} & \text{$\{\backslash \backslash $addfontfeature$\{$Color=0000FF$\}$Blue$\}$} \\
\end{array}
Yuck.
TeXForm
Documentation and Format
Mathematica's TeXForm
documentation suggests that TeXForm
can be modified via Format
. Here is the only example provided.
bin[x, y] // TeXForm
\text{bin}(x,y)
Format[bin[x_, y_], TeXForm] := MatrixForm[{{x}, {y}}]
bin[x, y] // TeXForm
\left(
\begin{
array
}{c}x \\
y
\end{
array
}\right)
Thoughts?