$\LaTeX$ in labeling: TimelinePlot

5

1

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]

kaka

Posted 2016-02-28T03:55:50.420

Reputation: 331

2

Take a look at Szabolcs MaTeX package, which he wrote to be able to include LaTeX snippets in Mathematica code.

– MarcoB – 2016-02-28T04:03:02.533

Answers

5

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"]

latex look

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.

Jens

Posted 2016-02-28T03:55:50.420

Reputation: 93 191

Perfect. Many thanks. – kaka – 2016-02-28T05:18:00.863

3

I don't think that using $\LaTeX$ notation for notation's sake is the best of ideas with Mathematica. You can type "two-dimensional" math notation directly in strings if you wish: select a character in the string and then use Cell -> Convert To -> TraditionalForm (keyboard shortcut Command-Shift-T on Mac or Control-Shift-T on other platforms). (Thank you Jens!) Then type the equation as you would normally.

The result looks like this:

enter image description here

You can also use my MaTeX package which integrated LaTeX with Mathematica and requires using LaTeX notation. Keep in mind though that the primary purpose of the package is not to allow you to use LaTeX notation. The aim of the package is simply to provide better quality typesetting that what Mathematica can already do.

Here's how it works.

Load the package:

<< MaTeX`

I prefer sans-serif for this case. The cmbright package provides a sans-serif font.

SetOptions[MaTeX, 
 "Preamble" -> {"\\usepackage{cmbright}", "\\usepackage{amsmath,amssymb}"}]

You need to use standard LaTeX notation with MaTeX, but keep in mind that: 1. by default everything is interpreted in math mode, so we need \text to get back to text mode and 2. Backslash (\) characters must be escaped Mathematica strings, i.e. we need to use "\\" to type a string containing a single backslash.

TimelinePlot[{DateObject[{2016, 3, 6}] -> 
   MaTeX@"\\text{Start }\\sum_{i=0}^{10} f(x_i)", 
  Interval[{DateObject[{2015, 6, 1}], DateObject[{2016, 2, 29}]}] -> 
   MaTeX@"\\text{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]

enter image description here

Szabolcs

Posted 2016-02-28T03:55:50.420

Reputation: 213 047

2

To type two-dimensional input directly into an input string, just type a single letter, then highlight it and use the menu command "convert to TraditionalForm" (or the keyboard shortcut, of course). Then you can edit two-dimensionally inside the resulting FormBox. I show the relevant steps (although with a slightly different starting point) in the movie in this answer.

– Jens – 2016-03-06T22:07:44.763

1@Jens This is excellent! Thank you so much, it will save me quite a bit of trouble. – Szabolcs – 2016-03-06T22:12:15.060

Cool ! This will produce notations in standard Latex typeface. thanks. – kaka – 2016-03-07T00:28:02.813

@Szabolcs In relation to "2. Backslash characters must be escaped ..." one workaround that might be useful is to use MakeExpression on MaTeX.

– Ronald Monson – 2017-06-03T08:12:15.483