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$
BaseStyle -> {FontFamily -> "LM Roman 10", FontSize -> 10}
. $\endgroup$Graphics3D
objects, there is the Fortran code Polygone that reduces the size of postscript graphics by merging unnecessary polygons. $\endgroup$