Lab08

Markdown Syntax

Course
Quarter

PSTAT 5A, with Ethan Marzban

Spring 2023


In this week’s lab, we will take a quick break from Python and instead learn some tools for making our Jupyter Notebooks look a little nicer (and help incorporate some mathematical equations into them!).

IMPORTANT

For this lab in particular:

  • You must submit a .PDF file
  • You must change the metadata in your notebook in accordance with the procedures outlined in Lab01.

Also, the format of this lab is a little different in that there aren’t any numbered “Tasks”; instead, there are “Action Items” which you need to perform in a .ipynb file. As such, you don’t need to label your tasks; we will only be looking at your final product (i.e. your final PDF).

Introduction to Markdown Syntax

Remember way back in Week 1, when we started adding “Markdown Cells” to our Jupyter Notebooks? Unbeknownst to you, you’ve actually been using a different computing language than Python! Wikipedia defines Markdown as:

[…] a lightweight markup language for creating formatted text using a plain-text editor.

What this means is that Markdown is a language that is specifically designed to communicate with your computer and facilitate the formatting of text and equations.

Markdown Syntax

As with Python, Markdown has some specific syntax as well. We will take a few moments to explore some of this syntax.

1. Headers

Important Information

There are several different “levels” of headers:

  • A main (first-level) header
  • A second-level header
  • A third-level header
  • And so on and so forth

In Markdown, the level of the header is specified by how many hashtags (#’s) we include before the header text.

For example: all of the headers used in this explanation document are either second- or third-level headers.

Action Item

Add a first-level (main) header right after the setup code chunk, and have it say Section 1: Markdown Syntax. Run this cell, and ensure it displays properly.

Action Item

After your first-level (main) Section 1: Markdown Syntax header, add a second-level header that says Subsection 1.1. Again, run this cell and ensure that this header appears smaller than the first-level (main) header you created in the action item above.

2. Text Formatting

As we have previously seen, text written in a Markdown cell will display exactly as it is written in your final PDF. We can, however, also spice up the formatting of our text!

Note

There are several different text formatting options available to us in Markdown:

  • Boldface Text: **Boldface Text**
  • Italicized Text: _Italicized Text_ or *Italicized Text*
  • Fixed-Width Text: `Fixed-Width Text`
Action Item

Underneath your second-level header "Subsection 1.1", write the following sentence including all formatting as it appears here:

The quick brown fox jumps over the lazy dog

By the way, notice how we managed to display the sentence above as a block-quote? The syntax to do that in Markdown is using a single > symbol, followed by a space. For example,

This text

was generated using the code > This text.

Caution

Be mindful about line breaks and spaces when using Markdown syntax.

Action Item

Underneath your “quick brown fox” text, add a block-quote that says

That was a really cool sentence!

Run your Markdown cell, and ensure the block-quote is displaying properly. (Again: line breaks and indentation will be important!)

3. Itemized and Enumerated Lists

Important Information

There are two main types of lists: itemized and enumerated lists. Itemized lists have no explicit ordering: these are sometimes called “bulleted” lists, and look like this:

  • Item 1
  • Item 2
  • Item 3

Enumerated lists do have an explicit ordering:

  1. Item 1
  2. Item 2
  3. Item 3

In Markdown, itemized lists are created by navigating to a new line, writing a dash (-) or an asterisk (*), followed by a space, followed by the text you wish to have displayed in the corresponding item. For instance: the itemized list above was generated using the code

- Item 1
- Item 2
- Item 3

In Markdown, enumerated lists are created by navigating to a new line, writing the number of the item (e.g. 1, 2, etc.) either either a close-parenthesis ()), or a period (.) For instance: both of the following codes will generate an enumerated list:

1) Item 1
2) Item 2
3) Item 3
1. Item 1
2. Item 2
3. Item 3

To add sublevel items, navitage to a new line, hit the space bar 4 times, and then add the corresponding item symbol (i.e. - or * for itemized lists; 1) or a) or 1. or a. for enumerated lists; etc.)

  • As an example, this is a main-level item
    • Followed by a sublevel item!

This was generated using

- As an example, this is a main-level item
    - Followed by a sublevel item!
Action Item

Add a new second-level header to your document that says Subsection 1.2: Itemized and Enumerated Lists. Underneath this, include the following list (making sure it displays properly when you run the cell!)

  • It is very important to check the Binomial Conditions before using the Binomial Distribution!
    • Failure to check the necessary conditions can lead to incorrect results.
    • Incorrect results are not good!

4. Equations in Markdown

One of the very nice things about Markdown is that it is able to (very nicely) combine both plain-text as well as equations!

The language used to specify equations in Markdown is called LaTeX (pronounced “lay-TECH”, or “lah-TECH”). LaTeX equations have two main styles: inline, like \(x + y\), and “display-style”, like \[ \sum_{i=1}^{k} x_i \]

Important Information

In LaTeX, inline equations are specified by single dollar signs and display-style equations are specified by double-dollar signs. For instance:

  • $x + y$ will display as \(x + y\)
  • $$x + y$$ will display as \[x + y\]

Inside either an inline or display-style equation environment, we can use LaTeX-specific syntax to generate equations.

Important Information
  • Standard mathematical symbols display in LaTeX as they would in text: this includes addition (+), subtraction (-), division (/), and multiplication (*).
  • The symbol for “less than or equal to” (≤) is typeset as \leq; the symbol for “greater than or equal to” (≥) is typeset as \geq.
  • Exponents are generated using the caret (^): for example, $x^2$ displays as \(x^2\)
  • Subscripts are generated using the underscore (_): for example, $x_2$ displays as \(x_2\)
Action Item

Add a second-level (main) header that says "Subsection 1.3: Typesetting Equations", and underneath it write the following sentence (including all LaTeX formatting!):

The Pythagorean Theorem states that \(a^2 + b^2 = c^2\).

Run this cell, and ensure it displays properly.

We can also typeset more advanced formulas and symbols:

Important Information
  • Sums (using sigma-notation) are generated using the \sum command: for example, $\sum_{x = 0}^{1} x$ displays as \(\sum_{x = 0}^{1} x\)

  • Integrals are generated using the \int command: for example, $\int_{a}^{b} f(x) \ dx$ displays as \(\int_{a}^{b} f(x) \ dx\)

  • Fractions are generated using the \frac command: for example, $\frac{1}{2}$ displays as \(\frac{1}{2}\)

  • Square-roots are generated using the \sqrt command: for example, $\sqrt{2}$ displays as \(\sqrt{2}\)

  • The symbol \(\pi\) is generated using $\pi$

  • Adding a bar on top of a variable can be done using \overline{}; e.g. $\overline{y}$ typesets as \(\overline{y}\)

  • The probability symbol can be typeset using $\mathbb{P}$; e.g. \(\mathbb{P}\).

  • The symbol for Expected Value can be typeset using $\mathbb{E}$; e.g. \(\mathbb{E}\).

Note

In LaTeX, curly braces ({, }) are used to delineate “chunks” of code/text. For example, if you just write $x^-2$, this displays as \(x^-2\). If we want to display \(x^{-2}\), we need to use $x^{-2}$.

Note

When writing fractions inside parentheses, it is important to use \left( and \right) to ensure the size of the parentheses scale with the fractions. For example, \[ (\frac{1}{2}) \] which was generated using $$ (\frac{1}{2}) $$, looks a bit worse than \[ \left(\frac{1}{2}\right) \] which was generated using $$ \left(\frac{1}{2}\right) $$.

Action Item

Add the following equation to your document: \[ f_X(x) = \frac{1}{\sigma \sqrt{2\pi}} e^{-\frac{1}{2} \left( \frac{x - \mu}{\sigma} \right)^2} \]

There is nothing stopping us from including equations in itemized and enumerated lists!

Action Item

Add the following list to your document, ensuring that it displays properly when the cell is run (pay attention to the formatting of the text as well!):

  • Pythagorean Theorem: \(a^2 + b^2 = c^2\)
  • Euler’s Identity: \(e^{i \pi} + 1 = 0\)

It may also be useful to learn how to make piecewise-defined equations using LaTeX. To typeset piecewise-defined functions, we use the cases environment: \[ \begin{cases} a & \text{if condition 1} \\ b & \text{if condition 2} \\ c & \text{if condition 3} \\ \end{cases} \] was generated using the code

$$ \begin{cases} 
    a   & \text{if condition 1}   \\
    b   & \text{if condition 2}   \\
    c   & \text{if condition 3}   \\
\end{cases} $$
Action Item

Add the probability density function \(f_X(x)\) of the \(\mathrm{Unif}(a, \ b)\) distribution to your Notebook: \[ f_X(x) = \begin{cases} \frac{1}{b - a} & \text{if } a \leq x \leq b \\ 0 & \text{otherwise} \\ \end{cases} \]

Finally, we should talk about how to align equations. The easiest way to align display-style equations is using an align* environment. For example: \[\begin{align*} a + b & = c + d + e \\ a + b + 0 & = c + d \end{align*}\] was typeset using

\begin{align*}
  a + b       & = c + d + e   \\
  a + b + 0   & = c + d + 0
\end{align*}
Action Item

Add the following set of equations into your Markdown document, taking care to use appropriate alignment.

\[\begin{align*} \overline{x} & = \frac{1}{n} \sum_{i=1}^{n} x_i \\ n \overline{x} & = \sum_{i=1}^{n} x_i \end{align*}\]

This is only just the surface of what LaTeX can do! If you are planning on going into any STEM-related field, we highly encourage you to delve deeper into the world of LaTeX as it will be a very valuable skill to have.