Explain Codes LogoExplain Codes Logo

How can I mix LaTeX in with Markdown?

python
pandoc
latex
markdown
Anton ShumikhinbyAnton Shumikhin·Oct 30, 2024
TLDR

For incorporating LaTeX inside Markdown, utilize the $...$ delimiters for inline mathematics and $$...$$ for block equations. Example:

  • Inline: $E=mc^2$ → (E=mc^2)
  • Displayed: $$x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}$$ → [ x=\frac{-b\pm\sqrt{b^2-4ac}}{2a} ]

Make sure your Markdown processor supports LaTeX, for example, via MathJax or KaTeX.

Convert documents with Pandoc

To convert LaTeX embedded Markdown documents into other formats like HTML, PDF, etc, Pandoc comes to the rescue. Use the following command-line instruction:

pandoc document.md -f markdown -t latex -s -o output.pdf

For a slick professional look, dabble with the LaTeX templates provided with Pandoc, or tailor your own. Play around with fancyhdr to customize document headers and footers, and leverage Tikz and pgfplots for impressive figures. Advanced layout requirements? Check out the pandoc-latex-template.

Leverage MultiMarkdown and Org-mode

Explore Org-mode and MultiMarkdown for an enhanced LaTeX and Markdown interplay. MultiMarkdown supports features like metadata and tables, while Org-mode in Emacs offers a Markdown-like environment supercharged with LaTeX abilities. Utilize preview-latex in Org-mode to preview math expressions as they render in the output.

# Inside Org-mode. # As seen in real life! Preview rendered LaTeX inside your Markdown-like document.

Advanced Integration and Proper Practices

Basic integration will serve in most scenarios. But in case of complex documents, these tips might come in handy:

  1. Use Git for version controlling your documents — change is the only constant!
  2. For advanced usage of LaTeX in Markdown, pull up your socks and do some homework.
  3. Get finer output by tuning Pandoc’s options.
  4. It won't hurt to try out more LaTeX processors like MiKTeX.
  5. Familiarize yourself with tools like KaTeX for efficient LaTeX rendering.

When doubts linger, dive into the treasure called pandoc.org.