LaTeX Demonstration
This is not required for this course
Using LaTeX code is not a requirement for this course. However, I want to give you the tools to use and practice with it if you wish, in order to have more tools to write up professional looking documents.
If you do not want to use LaTeX code, writing things like mu, pi, p-hat etc. is perfectly acceptable. As long as you articulate that you have an understanding of the notation, you will earn full credit.
Walkthrough
LaTeX code is a markup language used to write up mathematical equations. It is the de facto standard for the communication and publication of scientific documents.
LaTeX code DOES NOT go in r code chunks. LaTeX code goes in the body of the document in-between dollar signs. There are two different ways we can use LaTeX code:
– in-line
– It’s own centered chunk
An example of inline code would be to put a mathematical symbol \(\mu\) into a sentence. In order to do this, we need to put the code for the symbols we are going to use between dollar signs: $LaTeX code$
You will notice that the dollar signs change colors when you place the second one. Think about this as create an open and closed space to write LaTeX code.
An example of a LaTex chunk looks like:
\[ H_o: \mu = 25 \] To do this, we need to start the chunk with two dollar signs, and close it with another two dollar signs. When you do this, the dollar signs will change color, and the background within the LaTeX chunk will change color (similar to a R-code chunk).
$$
LaTeX code
$$
NOTE: Not closing LaTeX chunks will cause issues when rendering your document, but like not closing code chunks. Be mindful of this.
LaTeX code for 511
There is a lot that we can do in LaTeX. Here is some code to create common symbols we will use in this course.
In the first example, we use _ to specify that we want a subscript.
$H_o
$ = \(H_o\)
$H_a
$ = \(H_a\)
In the next examples, we call mathematical symbols. When doing so, we need to make sure we have a backslash before the code.
$\neq
$ = \(\neq\)
$\mu
$ = \(\mu\)
$\pi
$ = \(\pi\)
In the next examples, we can call a mathematical symbol to be used on text. Much like functions, anything that goes inside the brackets is what the action will be performed on. However, LaTeX uses curly brackets.
$\bar{x}
$ = \(\bar{x}\)
$\hat{p}
$ = \(\hat{p}\)
Random Variables
To specify random variables, we can use cases
. The double backslash is used to drop down to a new line when you are in the environment such as cases (or split). The \text
code is to tell LaTeX we want to write text. The default in LaTeX is to NOT put in spaces (even if we put them in), because it’s largely a mathematical language (and not a language for text).
$$
X =
\begin{cases}
1 & \text{if yes}\\
0 & \text{if no}
\end{cases}
$$
\[ X = \begin{cases} 1 & \text{if yes}\\ 0 & \text{if no} \end{cases} \]
Dropping down to a new line in a LaTeX code chunk
You can use the double backslash \\
to drop down to a new line, if you are in a correct environment. We can use the \split
environment to do this. Use the split environment to break an equation and to align it in columns, just as if the parts of the equation were in a table.
To do this:
– Start the chunk with \begin{split}
– Put \\
at the end of a line (where you want to stop and start a new line underneath)
– End the chunk with \end{split}
$$
\begin{split}
H_o: \mu = 25 \\
H_a: \mu \neq 25
\end{split}
$$
\[ \begin{split} H_o: \mu = 25 \\ H_a: \mu \neq 25 \end{split} \]
Fractions + Square roots
To make a fraction, we can use the \frac{}{}
code. What goes into the first set of curly brackets is your numerator. What goes into the second set of brackets is your denominator.
Example:
$$
\frac{37}{100}
$$
\[ \frac{37}{100} \]
To make a square root, we can use the \sqrt{}
code.
$$
\sqrt{37}
$$
\[ \sqrt{37} \]
Flush LaTeX output left
If you are setting up a LaTeX chunk, and want the output to be flushed left, we are going to do three things:
– Start the chunk with \begin{flalign}
– Put &&
at the end of each row in the equation
– End the chunk with \end{flalign}
$$
\begin{flalign}
H_o: \mu&&
\end{flalign}
$$
\[ \begin{flalign} H_o: \mu&& \end{flalign} \]
Putting things together
Here is an example of putting multiple ideas together from above:
$$
\begin{flalign}
\sqrt{\frac{\pi_o - (1-\pi_o)}{n}} = value&&
\end{flalign}
$$
\[ \begin{flalign} \sqrt{\frac{\pi_o - (1-\pi_o)}{n}} = value&& \end{flalign} \]