Typevia
Format Conversion

Introduction to LaTeX and Markdown

In the world of technical writing and documentation, two tools stand above the rest: Markdown and LaTeX.

Markdown is beloved for its simplicity and readability. It allows you to write formatted text using plain-text syntax that is easy to read even before it is rendered. LaTeX, on the other hand, is the industry standard for high-quality typesetting, especially when it comes to complex mathematics, bibliographies, and professional document layouts.

When you combine them, you get the "best of both worlds." You can use Markdown for the bulk of your writing—headings, lists, and paragraphs—and lean on LaTeX for its powerful mathematical notation and structural precision. This guide will show you how to integrate LaTeX into Markdown documents effectively.


1. The Core Concept: Math Mode in Markdown

The most common way to use LaTeX within Markdown is through Math Mode. Most modern Markdown editors (like Obsidian, VS Code, and Typora) and platforms (like GitHub and Jupyter Notebooks) use a library called MathJax or KaTeX to render LaTeX code.

There are two primary ways to write LaTeX in Markdown:

Inline Math

Use single dollar signs ($) to include math within a sentence.

  • Syntax: $your-latex-code$
  • Example: The Pythagorean theorem is $a^2 + b^2 = c^2$.

Display Math

Use double dollar signs ($$) to place math on its own line, centered and slightly larger.

  • Syntax:
    $$
    your-latex-code
    $$
  • Example:
    $$
    e = mc^2
    $$

Note: Some Markdown parsers require the $$ symbols to be on their own lines, while others allow them to be on the same line as the code. For maximum compatibility, put your opening and closing $$ on separate lines.


2. Common LaTeX Commands for Markdown

When writing math in Markdown, you use standard LaTeX syntax. If you are a beginner, here are the most essential commands you will use:

Fractions and Square Roots

To create a fraction, use \frac{numerator}{denominator}. For square roots, use \sqrt{value}.

$$
\frac{1}{2} + \sqrt{x} = y
$$

Superscripts and Subscripts

Use the caret (^) for exponents and the underscore (_) for subscripts. If the content is longer than one character, wrap it in curly braces {}.

$$
x_{i} = y^{2n + 1}
$$

Greek Letters

Simply type the name of the letter preceded by a backslash. Capitalize the first letter for the uppercase version.

$$
\alpha + \beta = \Gamma
$$

Sums and Integrals

LaTeX handles large operators beautifully.

$$
\int_{a}^{b} x^2 \,dx \quad \text{and} \quad \sum_{i=1}^{n} i
$$

3. Working with LaTeX Environments

While Markdown handles simple text, you can use LaTeX environments inside Markdown for more complex structures like matrices or multi-line equations.

Matrices

To create a matrix, you use the \begin{matrix} command (or pmatrix for parentheses, bmatrix for brackets).

$$
\begin{bmatrix}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1
\end{bmatrix}
$$

Aligned Equations

If you want to show a step-by-step derivation where the equals signs are lined up, use the aligned environment. Use the ampersand (&) to mark the alignment point and the double backslash (\\) to start a new line.

$$
\begin{aligned}
(a+b)^2 &= (a+b)(a+b) \\
        &= a^2 + ab + ba + b^2 \\
        &= a^2 + 2ab + b^2
\end{aligned}
$$

4. Converting Markdown to PDF via Pandoc

If you want to create a professional PDF from a Markdown file containing LaTeX, Pandoc is the most powerful tool available. Pandoc acts as a "universal document converter."

When Pandoc converts Markdown to PDF, it actually converts the Markdown into a temporary LaTeX file first and then uses a LaTeX engine (like pdflatex or xelatex) to generate the PDF.

Using YAML Front Matter

You can pass LaTeX-specific configurations to Pandoc using a "YAML block" at the very top of your Markdown file. This allows you to set margins, fonts, and document classes.

---
title: "My Research Report"
author: "Jane Doe"
geometry: margin=1in
fontsize: 12pt
header-includes:
   - \usepackage{amsmath}
---

## Introduction
This document was written in Markdown, but utilizes LaTeX for formatting.

The Conversion Command

To convert your file, you would run a command like this in your terminal:

pandoc input.md -o output.pdf

5. Best Practices and Common Pitfalls

Using LaTeX inside Markdown is powerful, but it can be finicky. Follow these tips to ensure your documents render correctly:

  • Avoid Spaces in Inline Math: Some Markdown parsers will fail if there is a space immediately after the opening $ or before the closing $.
    • Correct: $x=y$
    • Incorrect: $ x=y $
  • Escape Special Markdown Characters: If your LaTeX code contains characters that Markdown uses for formatting (like underscores _ or asterisks *), it can sometimes cause conflicts. If your math isn't rendering, try wrapping the math block in a more rigid environment or checking for conflicting symbols.
  • Use \text{} for Words: LaTeX treats every letter in math mode as a variable, which makes them italicized and oddly spaced. Use \text{...} for plain text inside math.
    • Example: $x = 10 \text{ units}$
  • Don't Overcomplicate: If you find yourself writing hundreds of lines of pure LaTeX inside a Markdown file, it might be time to switch to a pure .tex file. Markdown is meant for simplicity; use it for the structure and LaTeX for the "heavy lifting."

Warning: Not all Markdown viewers support LaTeX by default. While Obsidian, GitHub, and Discord do, basic text editors or older website CMS platforms may require a plugin or specific configuration to display math correctly.


6. Real-World Application: Academic Blogging

Imagine you are writing a blog post about statistics. Markdown allows you to quickly draft the headers and explanations, while LaTeX ensures your formulas look professional.

Input:

## The Normal Distribution

The probability density function for the normal distribution is defined by the following formula:

$$
f(x) = \frac{1}{\sigma\sqrt{2\pi}} e^{-\frac{1}{2}\left(\frac{x-\mu}{\sigma}\right)^2}
$$

Where:
- $\mu$ is the mean or expectation of the distribution.
- $\sigma$ is the standard deviation.

By using this hybrid approach, you spend less time worrying about how to format a fraction in HTML and more time focusing on the technical content of your work. As you grow more comfortable with LaTeX commands, you'll find that adding them to your Markdown files becomes second nature.