{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to LaTeX\n", "\n", "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", "Whenever LaTeX expressions are showed, the raw Markdown/LaTeX is shown beneath it. (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", "\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. Note 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 backward 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", "\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", "\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 if you take the second term of this course,\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", "\n", "Compare the following equations.\n", "\n", "\\begin{align}\n", "\\text{good: } &\\sum_{i=1}^n i^3 = \\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 = \\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} \\\\\n", "\\sigma_{12} & \\sigma_2^2 \n", "\\end{pmatrix}.\n", "\\end{align}\n", "\n", " \\begin{align}\n", " \\sigma^2 = \\begin{pmatrix}\n", " \\sigma_1^2 & \\sigma_{12} \\\\\n", " \\sigma_{12} & \\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 piecewise linear function like this.\n", "\n", "\\begin{align}\n", "\\text{rectifier}(x) = \\left\\{\n", "\\begin{array}{cl}\n", "0 & x \\le 0 \\\\\n", "x & x > 0.\n", "\\end{array}\n", "\\right.\n", "\\end{align}\n", "\n", " \\begin{align}\n", " \\text{rectifier}(x) = \\left\\{\n", " \\begin{array}{cl}\n", " 0 & x \\le 0 \\\\\n", " x & x > 0.\n", " \\end{array}\n", " \\right.\n", " \\end{align}\n", "\n", "The `array` environment allows 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": {}, "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", "`\\langle x \\rangle` | $\\langle x \\rangle$\n", "`\\left\\langle \\frac{x}{y} \\right\\rangle` | $\\left\\langle \\frac{x}{y} \\right\\rangle$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Computing environment" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPython 3.8.3\n", "IPython 7.16.1\n", "\n", "jupyterlab 2.1.5\n" ] } ], "source": [ "%load_ext watermark\n", "%watermark -v -p jupyterlab" ] } ], "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.8.3" } }, "nbformat": 4, "nbformat_minor": 4 }