LaTeX for Data Scientists, in Under 6 Minutes | by Andre Ye | Towards Data Science

As a data scientist, you work with data. Data is inherently mathematical — and it is imperative that you are able to communicate those ideas clearly. Even if you’re not developing algorithms, the ability the express common data science techniques — perhaps the result of a polynomial regression or performance of a box-cox transformation — is imperative.
That being said, LaTeX, the most popular mathematical typesetting language, comes with lots and lots and lots of frills. This tutorial will only show the most important parts of LaTeX as they pertain to data science, and by the end, you will know enough of LaTeX to incorporate it in your projects.
Here’s LaTeX, in 6 minutes or less. At the end are 5 practice LaTeX problems.
To display math inline with text, the expression must be in between dollar signs.
In physics, the mass-energy equivalence is stated  by the equation $E=mc^2$, discovered in 1905 by Albert Einstein.
Alternatively, the opening and closing \[ and \] can be used.
The mass-energy equivalence is described by the famous equation\[E=mc2\]
This will automatically center the math and display it on a new line.
A subscript is indicated using _, and a superscript using ^. $a^2$ will output a2. Superscripts and also be combined with subscripts by being called consecutively:
\[ a_1^2 + a_2^2 = a_3^2 \]
For longer superscript and subscripts, putting the superscript and subscript within brackets can clean the code:
\[ x^{2 \alpha} - 1 = y_{ij} + y_{ij}  \]
Note — LaTeX has many symbols that can be called using \name. In the example above, \alpha was used. Others include \infty, which is the infinity symbol.
Superscripts and subscripts can also be nested and combined in many ways, as long as the scope is clearly specified with parentheses:
\[ (a^n)^{r+s} = a^{nr+ns}  \]
Many mathematical operators require subscripts or/and superscripts. In these cases, the operator is treated as an object with normal superscript and subscript properties. Take, for example, the sigma/summation operator, which is called by \sum.
\[ \sum_{i=1}^{\infty} \frac{1}{n^s}  = \prod_p \frac{1}{1 - p^{-s}} \]
Even though superscripts and subscripts are called on a regular object with the sigma operator, it is automatically aligned.
Something else to note — when using \[ and \] opening and closing indicators, everything will be put on one line, so the code being put on several lines will not impact the end result.
Other operators that operate on a superscript and subscript include:
\int for integrals
\cup for union (upward facing u)
\cap for intersection (downward facing u)
\oint for curve integral
\coprod for coproduct
Trigonometric functions, logarithms, and other mathematical functions can be un-italicized and formatted by putting a \ before them.
\[ \sin(a + b ) = \sin(a)\cos(b) + \cos(a)\sin(b) \]
Some operators can take parameters via subscript, such as the limit operator.
\[ \lim_{h \rightarrow 0 } \frac{f(x+h)-f(x)}{h} \]
Note how a limit declaration includes a subscript and the calling of a symbol, \rightarrow. \frac{a}{b} is a method of creating a fraction a/b.
For some of the commands in this section to work, you must first include the amsmath package at the beginning of the file.
\usepackage{amsmath}
Fractions and binomial coefficients are very straightforward. They are called with \name{parameter1}{parameter2}.
\[     \binom{n}{k} = \frac{n!}{k!(n-k)!} \]
How fractions are displayed differs when they are used inline.
Fractions can be used alongside the text, for  example $frac{1}{2}$, and in a mathematical  display style like the one below:\[\frac{1}{2}\]
Fractions can be easily nested by using a fraction as a parameter.
\[   a_0+\cfrac{1}{a_1+\cfrac{1}{a_2+\cfrac{1}{a_3+\cdots}}} \]
The above command uses \cfrac{}{} from the amsmath package, which keeps the size of the fractions the same, even when they are nested. This can be substituted with \frac{}{}, which will make nested fractions smaller.
LaTeX supports many types of brackets:
(x+y) renders (x+y)
[x+y] renders [x+y]
\{ x+y \} renders {x+y}
\langle x+y \rangle renders ⟨x+y⟩
|x+y| renders |x+y|
\| x+y \| renders ∥x+y∥
Sometimes, the brackets and parenthesis will be too small for an expression, for example, if it has fractions.
Dynamic parentheses and brackets operate by \left[object] and \right[object] commands to signify the start and end.
\[  F = G \left( \frac{m_1 m_2}{r^2} \right) \]
In this case, the [object] was a parenthesis, so the expression was enclosed in \left( and \right). [object] can also be substituted with brackets (\left[ and \right]).
The amsmath package includes many commands to typeset matrices.
A matrix is defined by several values, separated in each row by &, each row separated by a \\. It is enclosed in standard \begin{object} and \end{object}.
For a plain matrix with no side brackets:
\begin{matrix}1 & 2 & 3\\a & b & c\end{matrix}
Substituting {matrix} in \begin{matrix} and \end{matrix} with {pmatrix} puts parenthesis around the matrix:
{bmatrix} puts brackets around it:
...and {Bmatrix} puts curly brackets around it.
Now, you have the basic skills of LaTeX. Find the code that will create the given output.
#1 (Loss):
\[Loss = Bias^2 + Variance^2 + Noise\]
#2 (Chi-Squared):
\[Chi = \frac{(\hat(y)-y)^2}{\sqrt{y}}= \frac{\delta^2}{\sqrt{y}}\]
#3 (KNN):
\[\hat(f)(x)\leftarrow \frac{\sum f(x)}{k}\]\[DE(x_i,x_j) = \sqrt{(x_i-x_j)^2 + (y_{xi}-y_{xj})^2}\]
#4 (Sigmoid):
\[\frac{1}{1+e^{-(wx+b)}}\]
#5 (R2):
\[R^2 = \frac{n \sum xy - \sum x. \sum y}{\sqrt{(n \sum x^2 - (\sum x)^2). (n \sum y^2 - (\sum y)^2)}}\]
In this tutorial, you learned...
How to format math inline and by itself
How to use superscripts and subscripts, as well as with operators
How to call certain operators
How to use parenthesis and brackets, including dynamic ones
How to create matrices with a variety of side bracket styles
Now you can format math beautifully! Most of learning LaTeX from now is memorizing the code for certain objects (\object), since you now know the foundational structure of LaTeX.
If you enjoyed this article, you may also be interested in other articles in this series:
SQL for Data Scientists, in 6 Minutes or Less