10

2

## 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?

1The TeX converter is really due for an update ... – Szabolcs – 2016-09-19T12:53:37.213

4@Szabolcs It sure is. Taking into account number of several years old bugs that are not fixed even if fixes are one-liners, I would call TeX conversion an unsupported feature. – jkuczm – 2016-09-19T12:59:57.410

@jkuczm I have a followup question. Your code above makes it clear how to sum \color with any other command. How can one swap the default for table? e.g. instead of an array, which is defaulted as a math environment, I may prefer ctable... – SumNeuron – 2016-09-27T03:15:38.397

@SumNeuron Take a look at TeXForm output in a tabular environment. In general, assumption that we're in $\TeX$ math mode is used extensively in

– jkuczm – 2016-09-30T09:18:32.160`TeXForm`

implementation, so anything other than post-processing of`TeXForm`

of individual table elements, as done by`TeXTableForm`

package, would be a lot of work.