Markdown to LaTeX: Convert with Pandoc (Tested)
September 11, 2026 · 10 min read
Markdown to LaTeX: Convert with Pandoc
Converting markdown to LaTeX takes one command: pandoc -s input.md -o output.tex. Pandoc turns headings into \section, pipe tables into longtable, footnotes into \footnote, and leaves your math untouched. This tutorial converts one sample document, shows the .tex it produces element by element, and covers the flags you need for a compilable file.
How Can I Convert a Markdown File to LaTeX?
Install pandoc from pandoc.org, then run it against your file. The -s flag (short for --standalone) matters more than any other. With it, pandoc writes a complete document with a \documentclass, a preamble, and \begin{document}. Without it, you get a fragment that only makes sense pasted into an existing .tex file.
# complete, compilable document
pandoc -s notes.md -o notes.tex
# fragment for \input{} into a larger project
pandoc notes.md -o notes-body.tex
Pandoc picks the output format from the .tex extension, so -t latex is optional. In our testing with pandoc 3.8.2, the standalone file for a two-page document runs about 170 lines, of which roughly 120 are preamble. That preamble loads longtable, booktabs, graphicx, hyperref, and amsmath for you, which is why tables, images, and equations compile on the first try.
If you'd rather skip the .tex step and go straight to PDF, pass -o notes.pdf and pandoc calls a LaTeX engine for you. That workflow has its own page on converting Markdown to PDF, so this post stays on producing the .tex source.
The sample Markdown document
Every example below comes from this file. It contains a heading, a list, a pipe table, a fenced code block, an image, a footnote, and both inline and display math. The YAML block at the top feeds \title and \author; the rest is the pandoc Markdown dialect with its default extensions.
---
title: Heat Transfer Notes
author: Dana Ortiz
---
# Introduction
Conduction moves heat through a solid.[^1] Fourier's law says $q = -k \nabla T$.
## Materials tested
| Material | k (W/m K) | Sample |
|:---------|----------:|:------:|
| Copper | 401 | A |
| Glass | 1.0 | B |
- Steady state only
- Room temperature
```python
def flux(k, grad):
return -k * grad
```

$$
\frac{\partial T}{\partial t} = \alpha \nabla^2 T
$$
[^1]: Convection and radiation are out of scope.
Save it as notes.md, run pandoc -s notes.md -o notes.tex, and open the result. The next section walks through what you'll find. If you want to try a different sample, keep the YAML block: without a title field pandoc still compiles, but \maketitle is left out and the document starts with the first heading.
How Markdown Elements Map to LaTeX Output
Here is the body pandoc 3.8.2 wrote for the sample, trimmed to the parts that matter. Compare it line by line with the Markdown above.
\section{Introduction}\label{introduction}
Conduction moves heat through a solid.\footnote{Convection and radiation
are out of scope.} Fourier's law says \(q = -k \nabla T\).
\subsection{Materials tested}\label{materials-tested}
\begin{longtable}[]{@{}lrc@{}}
\toprule\noalign{}
Material & k (W/m K) & Sample \\
\midrule\noalign{}
\endhead
\bottomrule\noalign{}
\endlastfoot
Copper & 401 & A \\
Glass & 1.0 & B \\
\end{longtable}
\begin{itemize}
\tightlist
\item Steady state only
\item Room temperature
\end{itemize}
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{def}\NormalTok{ flux(k, grad):}
\ControlFlowTok{return} \OperatorTok{{-}}\NormalTok{k }\OperatorTok{*}\NormalTok{ grad}
\end{Highlighting}
\end{Shaded}
\begin{figure}
\centering
\pandocbounded{\includegraphics[keepaspectratio,alt={Test rig}]{rig.png}}
\caption{Test rig}
\end{figure}
\[
\frac{\partial T}{\partial t} = \alpha \nabla^2 T
\]
The mapping is predictable once you've seen it:
| Markdown | LaTeX pandoc writes | Needs cleanup? |
|---|---|---|
# Heading | \section{Heading}\label{heading} | No |
## Heading | \subsection{...} | No |
[^1] footnote | \footnote{...} inline at the reference | No |
| Pipe table | longtable with booktabs rules | Column widths for long text |
- item | itemize with \tightlist | No |
| Fenced code | Shaded + Highlighting (skylighting macros) | Only if you want listings |
 | figure with \includegraphics and \caption | Placement and width |
$...$ | \(...\) | No |
$$...$$ | \[...\] | Add equation if you need numbers |
The \label on every heading is a bonus you don't get from most converters: it means \ref{introduction} works in raw LaTeX later.
Two of those rows deserve a second look. Headings map to \section because the default documentclass is article. Pass -V documentclass=report and the same # heading becomes \chapter, with ## moving down to \section. Images get a figure environment with the alt text reused as the caption, but no placement specifier, so LaTeX floats them wherever it likes. Add fig-pos or edit the .tex if a figure lands three pages away.
The footnote syntax itself is covered in the Markdown footnotes guide. What matters here is that pandoc inlines the note text, so the [^1] definition at the bottom of your file disappears.
Which Pandoc Flags Produce a Compilable Document?
The bare -s command already compiles. These flags handle the things a real paper needs: a contents page, a document class, page margins, and a bibliography. Each one changes the preamble pandoc writes, so you never edit the .tex by hand for these settings. We prefer to keep them in a shell script next to the Markdown so the build is repeatable.
pandoc -s notes.md -o notes.tex \
--toc \
-V documentclass=article \
-V geometry:margin=1in \
--bibliography=refs.bib \
--citeproc
--toc inserts \tableofcontents after the title. Combine it with --toc-depth=2 to stop at subsections.
-V documentclass=... sets the class in \documentclass[]{...}. Any other template variable works the same way: -V geometry:margin=1in adds \usepackage[margin=1in]{geometry}, and -V fontsize=12pt changes the class option.
--bibliography=refs.bib --citeproc turns [@smith2020] citations into formatted text plus a reference list, using Chicago author-date unless you pass --csl. If you'd rather have native LaTeX citations, swap --citeproc for --natbib or --biblatex. Pandoc then writes \citep{} or \autocite{} commands and adds the package to the preamble, and you run BibTeX or Biber yourself.
Code block styling is the one flag that changed recently. Older tutorials say --listings; pandoc 3.8 prints a deprecation warning and asks for --syntax-highlighting=idiomatic instead, which produces a lstlisting environment with the language set. --syntax-highlighting=none gives you a plain verbatim block. The pandoc manual lists the accepted values.
The same conversion in Python with pypandoc
The related search "markdown to latex python" has a two-line answer. pypandoc (version 1.17, March 2026) wraps the pandoc binary; install pypandoc_binary if you want pandoc bundled with the package.
import pypandoc
tex = pypandoc.convert_file(
"notes.md", "latex",
extra_args=["-s", "--toc", "-V", "documentclass=article"]
)
open("notes.tex", "w").write(tex)
Keep -V and its value as separate list items; pypandoc's docs warn that "-V documentclass=article" as one string fails. convert_text() does the same job on a string when the Markdown is generated rather than read from disk.
Preview the Source Before You Convert
Check the Markdown side before running pandoc, especially the math. The editor below renders $...$ and $$...$$ with KaTeX, so a missing brace shows up here as an error instead of as a LaTeX compile failure later. It does not render pandoc footnotes, so the [^1] note is left out of this copy of the sample. Edit the sample, then copy it back to your file.
If you write math-heavy Markdown often, the Markdown equation post covers the syntax rules and which platforms render it. This post assumes you already have the math and just need it in .tex.
Can I Use Markdown in LaTeX?
Yes, and it's the opposite direction from everything above. The CTAN markdown package (version 3.16, June 2026) lets a .tex document contain Markdown directly, either inline or from a separate file.
\documentclass{article}
\usepackage[hybrid,fencedCode]{markdown}
\begin{document}
\begin{markdown}
# Introduction
Conduction moves heat through a solid. Fourier's law says $q = -k \nabla T$.
\end{markdown}
\markdownInput{materials.md}
\end{document}
The hybrid option lets you mix LaTeX commands inside the Markdown, which is how the $...$ math above still works. fencedCode enables triple-backtick blocks. The package parses Markdown with Lua, so LuaLaTeX runs it natively; the package manual says pdfLaTeX and XeLaTeX need shell escape enabled to call the Lua interpreter. Overleaf has a short guide to the markdown package if you compile there.
Pick this route when the document is fundamentally LaTeX and you want easier prose. Pick pandoc when the document is fundamentally Markdown and LaTeX is just the output.
Common Conversion Mistakes and Fixes
Forgetting -s and then wondering why the file won't compile. The fragment has no \documentclass or \begin{document}. Either add -s or \input{} the fragment from a wrapper file that loads longtable, booktabs, and graphicx.
Using raw HTML in the Markdown. Pandoc drops HTML tags when writing LaTeX, so <br> and <center> vanish silently. Write the LaTeX command instead; pandoc passes raw LaTeX through unchanged when the raw_tex extension is on, which it is by default for the markdown reader.
Expecting cross-references to work. Markdown has no \ref. Pandoc labels every heading (\label{introduction}), so you can write \ref{introduction} as raw LaTeX in the Markdown. Tables and figures get no labels unless you add them by hand or use a filter such as pandoc-crossref.
The acknowledged limit of the whole approach: a converted .tex file is a starting point, not a finished paper. Column widths, figure placement, and equation numbering still need a human pass, and that pass is easier if you convert once at the end instead of round-tripping.
Markdown to LaTeX FAQ
Converting markdown to LaTeX with pandoc gives you a compilable .tex file from one command, and the mapping is stable enough to trust for headings, lists, tables, footnotes, and math. Draft and check the Markdown in the editor, keep your build flags in a script, and reserve hand edits for figure placement and column widths. The Markdown vs LaTeX guide helps if you're still deciding which format the project should live in.