5
$\begingroup$

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]
$\endgroup$
  • 2
    $\begingroup$ Take a look at Szabolcs MaTeX package, which he wrote to be able to include LaTeX snippets in Mathematica code. $\endgroup$ – MarcoB Feb 28 '16 at 4:03
5
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ Perfect. Many thanks. $\endgroup$ – kaka Feb 28 '16 at 5:18
2
$\begingroup$

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

$\endgroup$
  • 2
    $\begingroup$ 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. $\endgroup$ – Jens Mar 6 '16 at 22:07
  • 1
    $\begingroup$ @Jens This is excellent! Thank you so much, it will save me quite a bit of trouble. $\endgroup$ – Szabolcs Mar 6 '16 at 22:12
  • $\begingroup$ Cool ! This will produce notations in standard Latex typeface. thanks. $\endgroup$ – kaka Mar 7 '16 at 0:28
  • $\begingroup$ @Szabolcs In relation to "2. Backslash characters must be escaped ..." one workaround that might be useful is to use MakeExpression on MaTeX. $\endgroup$ – Ronald Monson Jun 3 '17 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.