I am struggling to use $\LaTeX$ in labels in TimelinePlot, but was not successful.
For instance in the following code how can I insert $\sum_{i=0}^{10} f(x_i)$ in the label:
TimelinePlot[{DateObject[{2016, 3, 6}] -> "Start $\sum_{i=0}^{10} f(x_i)$ ",
Interval[{DateObject[{2015, 6, 1}], DateObject[{2016, 2, 29}]}] ->
"End ", {Interval[{DateObject[{2015, 1, 15}],
DateObject[{2015, 9, 9}]}], DateObject[{2016, 7, 31}],
Interval[{DateObject[{2016, 7, 27}], DateObject[{2016, 8, 6}]}]}},
AxesOrigin -> Center]
Answer
In order to have a decent chance of getting $\LaTeX$ converted into native Mathematica box expressions for display, you have to obey some rather restrictive rules. For example, you have to escape backslashes, so you must use "\\sum" instead of "\sum".
With these preliminaries, here is a small function that can be used to programmatically inset $\LaTeX$ math equations - but only the part inside a math environment such as the one delimited by $.
displayLaTeX[string_] :=
DisplayForm[ToBoxes@
TraditionalForm@ToExpression[string, TeXForm, HoldForm]]
Here I apply this function to get the output you want:
TimelinePlot[{DateObject[{2016, 3, 6}] ->
Row[{"Start ", displayLaTeX["\\sum_{i=0}^{10} f(x_i)"]}],
Interval[{DateObject[{2015, 6, 1}], DateObject[{2016, 2, 29}]}] ->
"End ", {Interval[{DateObject[{2015, 1, 15}],
DateObject[{2015, 9, 9}]}], DateObject[{2016, 7, 31}],
Interval[{DateObject[{2016, 7, 27}], DateObject[{2016, 8, 6}]}]}},
AxesOrigin -> Center, PlotTheme -> "Classic"]
Here, I chose a PlotTheme that corresponds to the classic $\LaTeX$ typeset look. Otherwise, the TraditionalForm output that I chose to mimic $\LaTeX$ will have a more "modern" look. If you're using a version below 9, then you don't need the PlotTheme option.
I'm using ToExpression to translate to the built-in box language. The advantage is that your labels can be styled and scaled arbitrarily. Ultimately, however, it's best to learn how to typeset such labels in Mathematica directly, not using $\LaTeX$ syntax. The reason is that there are limitations to the translation from $\LaTeX$, see for example the list of rules I collected here.

Comments
Post a Comment