{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Tutuorial 0c: Introduction to $\\LaTeX$ for Jupyter notebooks"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*This tutorial was generated from a Jupyter notebook. You can download the notebook [here](t0c_intro_to_latex.ipynb).*"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Standard workhorses\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# Seaborn, useful for graphics\n",
"import seaborn as sns\n",
"\n",
"# Magic function to make matplotlib inline; other style specs must come AFTER\n",
"%matplotlib inline\n",
"\n",
"# This enables SVG graphics inline (only use with static plots (non-Bokeh))\n",
"%config InlineBackend.figure_formats = {'svg',}\n",
"\n",
"# JB's favorite Seaborn settings for notebooks\n",
"rc = {'lines.linewidth': 2, \n",
" 'axes.labelsize': 18, \n",
" 'axes.titlesize': 18, \n",
" 'axes.facecolor': 'DFDFE5'}\n",
"sns.set_context('notebook', rc=rc)\n",
"sns.set_style('darkgrid', rc=rc)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this tutorial, you will learn some of the basics on how to use $\\LaTeX$ to display equations in Jupyter notebooks. For looking up symbols you may need, you can use any of the many [cheat sheets](http://users.dickinson.edu/~richesod/latex/latexcheatsheet.pdf) you can find by asking Google. I have provided a few that will come up often in this course at the [end of this tutorial](#Useful-Latex-symbols-for-BE/Bi-103).\n",
"\n",
"(The word Latex is generally stylized as $\\LaTeX$, but I get tired of reading that, so going forward, I will just write \"Latex.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Basic inline Latex\n",
"To embed Latex within text, simply encapsulate the Latex portions in dollar signs (`$`). MathJax takes care of the rest. As an example, consider the sentence below and the markdown/Latex code to render it.\n",
"\n",
">Einstein told us that $E = mc^2$.\n",
">\n",
">`Einstein told us that $E = mc^2$.`\n",
"\n",
"Notice how the equation is properly rendered, with mathematical variables in italics. Not also how `^2` was used to exponentiate. If the exponential has more than one character in it, it should be enclosed in braces (`{}`). In fact, braces are used to generally group symbols in Latex.\n",
"\n",
">Euler told us that $\\mathrm{e}^{i \\pi} - 1 = 0$.\n",
">\n",
">`Euler told us that $\\mathrm{e}^{i \\pi} - 1 = 0$.`\n",
"\n",
"Aside from the grouping braces, there are several other syntactical items of note. First, notice that I made the special character $\\pi$ with `\\pi`. In general, a forward slash precedes special symbols or commands in Latex. If we want another Greek letter, like $\\theta$, we use `\\theta`. Now, also note that I used \"`\\mathrm{e}`\" for the base of the natural logarithm. I was signaling to Latex that I wanted the character written in Roman font, and not italics, so I used `\\mathrm`. Anything in the braces following the function `\\mathrm` is rendered in Roman font. Note the difference.\n",
"\n",
">This is $e$. This is $\\mathrm{e}$.\n",
">\n",
">`This is $e$. This is $\\mathrm{e}$.`\n",
"\n",
"Now, back to grouping things in braces. We can do similar groupings using braces with with subscripts.\n",
"\n",
">The dot product of two $n$-vectors is $\\mathbf{a} \\cdot \\mathbf{b} = \\sum_{i=1}^n a_i b_i$.\n",
">\n",
">`The dot product of two $n$-vectors is $\\mathbf{a} \\cdot \\mathbf{b} = \\sum_{i=1}^n a_i b_i$.`\n",
"\n",
"Here, I have used `$\\mathbf{a}$` to make the character `a` boldface, denoting a vector. Note that we denote subscripts with an underscore. Notice also that the bounds of the sum use the same underscore and caret notation as for subscripts and superscripts. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Displaying equations on separate lines\n",
"The bounds on the summation in the above example may look a little funny to you because they are not above and below the summation symbol. This is because this particular equation is written inline. If we had separated it from the text, it renders differently.\n",
"\n",
">We can make an equation appear centered on a new line, like \n",
">\n",
"\\begin{align}\n",
"\\mathbf{a} \\cdot \\mathbf{b} = \\sum_{i=1}^n a_i b_i.\n",
"\\end{align}\n",
">\n",
">`We can make an equation appear centered on a new line, like `\n",
">\n",
">`\\begin{align}\n",
"\\mathbf{a} \\cdot \\mathbf{b} = \\sum_{i=1}^n a_i b_i.\n",
"\\end{align}`\n",
"\n",
"The `align` environment in Latex specifies that you want centered equations, separated from the text. It is called `align` because it allows you to align the equations. You separate lines in the equations with a double backslash (`//`). Insert an ampersand (`&`) in each line at the alignment point. All equations will be aligned at the location of the ampersand symbols (and, of course, the ampersands will not appear in the rendered equations).\n",
"\n",
">For a three-vector consisting of $x$, $y$, and $z$ components,\n",
">\n",
"\\begin{align}\n",
"\\mathbf{a} \\cdot \\mathbf{b} &= \\sum_{i=1}^n a_i b_i \\\\\n",
"&= a_x b_x + a_y b_y + a_z b_z.\n",
"\\end{align}\n",
">\n",
">`For a three-vector consisting of $x$, $y$, and $z$ components,`\n",
">\n",
">`\\begin{align}\n",
"\\mathbf{a} \\cdot \\mathbf{b} &= \\sum_{i=1}^n a_i b_i \\\\\n",
"&= a_x b_x + a_y b_y + a_z b_z.\n",
"\\end{align}`\n",
"\n",
"Note that I always put an extra blank line before the `\\begin{align}` statement. This is not necessary, but I think things look better with the extra space."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Fractions (and an example of fine-tuning)\n",
"To display fractional quantities, we use the `\\frac{}{}` command. `\\frac` is always followed by two sets of braces; the numerator is contained in the first, and the denominator is contained in the second. As an example, we can write an equation you will become intimately familiar with,\n",
"\n",
">\\begin{align}\n",
"P(A \\mid B) = \\frac{P(B \\mid A) \\, P(A)}{P(B)}\n",
"\\end{align}\n",
">\n",
">`\\begin{align}\n",
"P(A \\mid B) = \\frac{P(B \\mid A) \\, P(A)}{P(B)}\n",
"\\end{align}`\n",
"\n",
"The right hand side has a nicely-formatted fraction. I did a little extra fine-tuning in this equation. I'll show the equation again without the fine-tuning, which used the `\\mid` and `\\,` commands.\n",
"\n",
">\\begin{align}\n",
"P(A | B) = \\frac{P(B | A) P(A)}{P(B)}.\n",
"\\end{align}\n",
">\n",
">`\\begin{align}\n",
"P(A | B) = \\frac{P(B | A) P(A)}{P(B)}.\n",
"\\end{align}`\n",
"\n",
"First, the `\\mid` command should be used in conditional probabilities. Just using a vertical bar (`|`) results in crowding. Similarly, I used the `\\,` command to insert a little extra space between the two probabilities in the numerator. This makes the equation a bit easier to read. This `\\,` operator is especially important when defining integrals. We can put a little space between the $\\mathrm{d}x$ and the integrand.\n",
"\n",
">\\begin{align}\n",
"\\text{good: } &\\int_0^{2\\pi} \\mathrm{d}x \\, \\sin x. \\\\[1em]\n",
"\\text{bad: } &\\int_0^{2\\pi} \\mathrm{d}x \\sin x.\n",
"\\end{align}\n",
">\n",
"`\\begin{align}\n",
"\\text{good: } &\\int_0^{2\\pi} \\mathrm{d}x \\, \\sin x. \\\\[1em]\n",
"\\text{bad: } &\\int_0^{2\\pi} \\mathrm{d}x \\sin x.\n",
"\\end{align}`\n",
"\n",
"Note that I inserted extra space after the new line. Specifically, `\\\\[1em]` instructs Latex to insert a space equation to the width of an M character between the equations. I often do this to keep things clear.\n",
"\n",
"It is also very important to note that I used $\\sin$ and not $sin$. Mathematical functions should be in Roman font and are invoked with a backslash. Otherwise, the characters are interpreted as separate variables. To be clear:\n",
"\n",
">\\begin{align}\n",
"\\text{good: } &\\sin x. \\\\[1em]\n",
"\\text{bad: } & sin x.\n",
"\\end{align}\n",
">\n",
"`\\begin{align}\n",
"\\text{good: } &\\sin x. \\\\[1em]\n",
"\\text{bad: } & sin x.\n",
"\\end{align}`\n",
"\n",
"Finally, notice that I was able to put text in the equation like this: `\\text{good: }`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Grouping operators (and more fine-tuning)\n",
"Compare the following equations.\n",
"\n",
">\\begin{align}\n",
"\\text{good: } &\\sum_{i=1}^n i^3 \n",
" = \\left(\\sum_{i=1}^n i\\right)^2. \\\\[1em]\n",
"\\text{bad: } &\\sum_{i=1}^n i^3 = (\\sum_{i=1}^n i)^2.\n",
"\\end{align}\n",
">\n",
">`\\begin{align}\n",
"\\text{good: } &\\sum_{i=1}^n i^3 \n",
" = \\left(\\sum_{i=1}^n i\\right)^2. \\\\[1em]\n",
"\\text{bad: } &\\sum_{i=1}^n i^3 = (\\sum_{i=1}^n i)^2.\n",
"\\end{align}`\n",
"\n",
"In the second equation, I did not use the `\\left(` and `\\right)` construction for parentheses and the result looks pretty awful. In Latex, the height of anything that is encapsulated by `\\left(` and `\\right)` scales the parentheses appropriately. You can use `\\left` and `\\right` with many symbols. An important example is `\\left\\{`. Note that to display braces in an equation, you have to use `\\{` because just a plain brace (`{`) has a different meaning.\n",
"\n",
"(By the way, that equation is true, and pretty amazing. It says that the sum of the first $n$ *cubes* of integers is equal to the sum of the first $n$ integers *squared*!)\n",
"\n",
"Finally, if you use `\\left.` or `\\right.`, Latex will simply scale the opposite symbol to match the height of the text, but will suppress printing the other. For example,\n",
"\n",
">\\begin{align}\n",
"\\left. \\frac{1}{x + 2} \\right|_0^2 = -\\frac{1}{4}.\n",
"\\end{align}\n",
">\n",
">`\\begin{align}\n",
"\\left. \\frac{1}{x + 2} \\right|_0^2 = -\\frac{1}{4}.\n",
"\\end{align}`\n",
"\n",
"This is also useful if you are going to use `/` for a division operation. Compare the following.\n",
"\n",
">\\begin{align}\n",
"\\text{good: } & \\left. x^2 \\middle/ y^2 \\right. \\\\[1em]\n",
"\\text{bad: } & x^2 / y^2\n",
"\\end{align}\n",
">\n",
">`\\begin{align}\n",
"\\text{good: } & \\left. x^2 \\middle/ y^2 \\right. \\\\[1em]\n",
"\\text{bad: } & x^2 / y^2\n",
"\\end{align}`\n",
"\n",
"Here, we used the `\\middle` operator to scale the length of the division sign."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Matrices and arrays\n",
"On occasion, you'll need to express matrices. This is most easily done using the `pmatrix` environment. For example, a covariance matrix for two variables might be written as\n",
"\n",
">\\begin{align}\n",
"\\sigma^2 = \\begin{pmatrix}\n",
"\\sigma_1^2 & \\sigma_{12}^2 \\\\\n",
"\\sigma_{12}^2 & \\sigma_2^2 \n",
"\\end{pmatrix}.\n",
"\\end{align}\n",
">\n",
">`\\begin{align}\n",
"\\sigma^2 = \\begin{pmatrix}\n",
"\\sigma_1^2 & \\sigma_{12}^2 \\\\\n",
"\\sigma_{12}^2 & \\sigma_2^2 \n",
"\\end{pmatrix}.\n",
"\\end{align}`\n",
"\n",
"Once in the `pmatrix` environment, each row has entries separated by an ampersand. The row ends with a `\\\\`. Each row must have the same number of entries.\n",
"\n",
"You may also need to represent an values stacked on top of each other. For example, we might specify a Jeffreys prior like this.\n",
"\n",
">\\begin{align}\n",
"P(\\sigma) = \\left\\{\n",
"\\begin{array}{cl}\n",
"\\sigma^{-1} & \\sigma > 0 \\\\\n",
"0 & \\sigma \\le 0.\n",
"\\end{array}\n",
"\\right.\n",
"\\end{align}\n",
">\n",
">`\\begin{align}\n",
"P(\\sigma) = \\left\\{\n",
"\\begin{array}{cl}\n",
"\\sigma^{-1} & \\sigma > 0 \\\\\n",
"0 & \\sigma \\le 0.\n",
"\\end{array}\n",
"\\right.\n",
"\\end{align}`\n",
"\n",
"The `array` environment allow arrays of text. The `{cl}` after `\\begin{array}` indicates that two columns are wanted, with the first column being centered and the second being left-aligned. If we chose instead `{lr}`, the first column is left-aligned and the second column is right-aligned."
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## Using Latex in figures\n",
"Fortunately, Matplotlib allows use of Latex in axis labels and other text in figures. You simply need to include the dollar signs in your string."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Make a plot of JB's favorite function\n",
"x = np.linspace(0.0, 2.0 * np.pi, 200)\n",
"y = np.exp(np.sin(x))\n",
"plt.plot(x, y)\n",
"plt.xlabel('$x$')\n",
"plt.ylabel('$\\mathrm{e}^{\\sin x}$')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Unfortunately, Bokeh does not yet have Latex support, but will in the near future. So, for now, you will have to avoid using Latex in the text in figures if you want them to be interactive."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Useful Latex symbols for BE/Bi 103\n",
"Following is a list of some symbols you may find useful in this class.\n",
"\n",
"Latex | symbol\n",
"--- | :---: \n",
"`\\approx` | $\\approx$ \n",
"`\\sim` | $\\sim$ \n",
"`\\propto` | $\\propto$\n",
"`\\le` | $\\le$\n",
"`ge` | $\\ge$\n",
"`\\pm` | $\\pm$\n",
"`\\in` | $\\in$\n",
"`\\ln` | $\\ln$\n",
"`\\exp` | $\\exp$\n",
"`\\prod_{i\\in D}` | ${\\displaystyle \\prod_{i\\in D}}$\n",
"`\\sum_{i\\in D}` | ${\\displaystyle \\sum_{i\\in D}}$\n",
"`\\frac{\\partial f}{\\partial x}` | ${\\displaystyle \\frac{\\partial f}{\\partial x}}$\n",
"`\\sqrt{x}` | $\\sqrt{x}$\n",
"`\\bar{x}` | $\\bar{x}$\n",
"`\\hat{x}` | $\\hat{x}$\n",
"`\\langle x \\rangle` | $\\langle x \\rangle$\n",
"`\\left\\langle \\frac{x}{y} \\right\\rangle` | $\\left\\langle \\frac{x}{y} \\right\\rangle$"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}