Endorsing Szabolcs' excellent $MaTeX$ package as a way to manage $\LaTeX$ in $Mathematica$ but with an addition that for me at least has removed the significant irritant of having to escape backslashes in strings.

For example, given some previous $\LaTeX$

```
"\tilde{x}=\begin{cases}
(\frac{n+1}{2}) \text{th term} & \text{n odd} \\
((\frac{n}{2}) \text{th} + (\frac{n}{2}+1) \text{th term})/2 & \text{n even}
\end{cases}"
```

Applying $MaTeX$ out of the box means having to escape each occuring backslash:

```
Needs["MaTeX`"]
MaTeX@"\\tilde{x}=\\begin{cases}
(\\frac{n+1}{2}) \\text{th term} & \\text{n odd} \\\\
((\\frac{n}{2}) \\text{th} + (\\frac{n}{2}+1) \\text{th term})/2 & \\text{n even}
\\end{cases}"
```

It's not just the inconvenience of these mechanical modifications but that this also represents an impediment to programmatically modifying underlying $\LaTeX$. But following Simon Rochester's approach (while using a more efficient `StringTake`

and specific `MakeExpression`

- both as pointed out by Alexey Popkov in the comments), we can directly access the original string. Using `MaTeX`

as a wrapper for this interpretative intercept

```
MakeExpression[
RowBox@{"MaTeX", "[", str_String, "]"} |
RowBox@{"MaTeX", "@", str_String} |
RowBox@{str_String, "//", "MaTeX"} , StandardForm] :=
MakeExpression[
RowBox@{"MaTeX", "[", "StringTake", "[", ToString@InputForm[str],
",", "{", "2", ",", "-2", "}", "]", "]"}, StandardForm];
```

we get a more natural `MaTeX`

invocation

```
MaTeX@"\tilde{x}=\begin{cases}
(\frac{n+1}{2}) \text{th term} & \text{n odd} \\
((\frac{n}{2}) \text{th} + (\frac{n}{2}+1) \text{th term})/2 & \text{n even}
\end{cases}"
```

Some strings don't expect some of the harmless (in this context) escapes - e.g. `MaTeX["X \sim \mathcal{N}(1,0)"]`

so we'll turn this error message off: `Off[Syntax::stresc]`

For string pre-processing we can do a similar intercept with a `RawString`

wrapper to gain the ability to programmatically generate $\LaTeX$ for subsequent feeding into $MaTEX$

```
MakeExpression[
RowBox@{"RawString", "[", str_String, "]"} |
RowBox@{"RawString", "@", str_String} |
RowBox@{str_String, "//", "RawString"} , StandardForm] :=
MakeExpression[
RowBox@{"StringTake", "[", ToString@InputForm[str], ",", "{", "2",
",", "-2", "}", "]"
}, StandardForm];
```

as illustrated by

```
med[var_String] :=
StringTemplate[RawString["\tilde{`1`}=\begin{cases}
(\frac{n+1}{2}) \text{th term} & \text{n odd} \\
((\frac{n}{2}) \text{th} + (\frac{n}{2}+1) \text{th term})/2 & \text{n even}
\end{cases}"]][var];
vars = {"x", "y", "z"};
MaTeX[med /@ vars] // Column
```

Note that `MaTeX`

operates on (non-string) expressions in the normal way. Hence programmability is preserved for $Mathematica$ expressions and the frontend's 2D formatting/shortcuts can still be used to readily generate $\LaTeX$ code as desired.

```
mean[var_] := HoldForm[
\!\(\*OverscriptBox[\(var\), \(_\)]\) = \!\(TraditionalForm\`
\*FractionBox[\(
\*UnderoverscriptBox[\(\[Sum]\), \(i = 1\), \(n\)]
\*SubscriptBox[\(var\), \(i\)]\), \(n\)]\)];
MaTeX[mean /@ vars] // Column
```

$MaTeX$'s use-cases would seem to be consistency and customising of $\LaTeX$ graphics/formulae, or for presenting complex formulae within a *Mathematica* notebook. Sometimes however, the need may arise for the original *Mathematica* code snippet/function to be included either in a paper or in as part of a collation within a notebook. The obvious method, `HoldForm`

, does its holding *after* the underlying boxes have been parsed so to cut the parser off at the pass by way of showing how the code was originally entered, we can define a `RawHoldForm`

using this same idiom (I've adapted from Simon Rochester's other answer).

```
MakeExpression[
RowBox@{"RawHoldForm", "[", expr_, "]"} |
RowBox@{"RawHoldForm", "@", expr_} |
RowBox@{expr_, "//", "RawHoldForm"}, StandardForm] :=
HoldComplete[
ExpressionCell[RawBoxes@expr, "Input",
ShowStringCharacters -> True]]
```

and observe the difference

```
HoldForm[((x+"df") // f]
RawHoldForm[((x + "df") // f)]
(*
f[x + df]
((x+"df") // f)
*)
```

Putting it all together in a single code block that provides a more natural `MaTeX`

invocation, a string pre-processing, `RawString`

wrapper and a `RawHoldForm`

, the following can be loaded after $MaTeX$ (at least in its current version):

```
Needs["MaTeX`"];
MakeExpression[
RowBox@{"MaTeX", "[", str_String, "]"} |
RowBox@{"MaTeX", "@", str_String} |
RowBox@{str_String, "//", "MaTeX"} , StandardForm] :=
MakeExpression[
RowBox@{"MaTeX", "[", "StringTake", "[", ToString@InputForm[str],
",", "{", "2", ",", "-2", "}", "]", "]"}, StandardForm];
Off[Syntax::stresc]
MakeExpression[
RowBox@{"RawString", "[", str_String, "]"} |
RowBox@{"RawString", "@", str_String} |
RowBox@{str_String, "//", "RawString"} , StandardForm] :=
MakeExpression[
RowBox@{"StringTake", "[", ToString@InputForm[str], ",", "{", "2",
",", "-2", "}", "]"
}, StandardForm];
MakeExpression[
RowBox@{"RawHoldForm", "[", expr_, "]"} |
RowBox@{"RawHoldForm", "@", expr_} |
RowBox@{expr_, "//", "RawHoldForm"}, StandardForm] :=
HoldComplete[
ExpressionCell[RawBoxes@expr, "Input",
ShowStringCharacters -> True]]
```

**Addends**

While it is the `ToString@InputForm@str`

that permits the sought-after programmability, as shown from MrWizard's and Jen's answers, the original strings can also be worked with in the frontend for one-off insertions

The use of `StringTake`

avoids the infinite recursion in `MakeExpression`

's definition

Thus far no downsides have been observed (escapes like \n, \t are not really relevant in latex formatting) although heavier users of $MaTeX$ might notice/uncover issues

From the originator's comments the original syntax was not designed and hence this would seem to offer an improved syntax for latex formatting in $MaTeX$

5

Theoretically you

– Szabolcs – 2012-01-26T09:22:42.080canget Mathematica to use Computer Modern, but it's a lot of trouble for little gain an imperfect results. You could start by downloading Latin Modern in OpenType format. Mathematica exports to PDF 1.4, a PDF specification too old to be able to embed OpenType fonts, so you'd need to convert them to TrueType. Then install the appropriate font (e.g. "LM Roman 10" is upright, optimized for 10 pt viewing), and make the plot with`BaseStyle -> {FontFamily -> "LM Roman 10", FontSize -> 10}`

.1This is not a satisfactory answer I know, but I thought it would be good to write it down. Even if you do this, mathematical text (formulae) won't appear in the same style as when typeset by LaTeX, you unless all you need is numbers for the axes, it's probably not worth the trouble. – Szabolcs – 2012-01-26T09:24:07.273

2

For

– Simon – 2012-01-26T12:39:15.540`Graphics3D`

objects, there is the Fortran code Polygone that reduces the size of postscript graphics by merging unnecessary polygons.@Simon that could have been a good answer for my question here: http://mathematica.stackexchange.com/questions/644/how-can-all-those-tiny-polygons-generated-by-regionplot-be-joined-into-a-single I encourage you to post it.

– Szabolcs – 2012-01-26T14:25:47.857@Szabolcs: problem is, I've never used Polygone - only heard about others using it. If you get it working and post an answer, I promise to upvote it! – Simon – 2012-01-27T01:47:06.640

3Personally I use Mathematica to generate data for the plots, and then create the plots themselves using a LaTeX package like PGFPlots. A couple extra steps, sure, but I like the extra control that you get. – 2012rcampion – 2015-03-10T15:04:54.083