How to Write Pseudocode in Markdown and Export It to PDF
At a glance
- 1. Choose an approach
Use a fenced code block for simple pseudocode, or a LaTeX algorithm package for a formatted algorithm in PDF.
- 2. Add LaTeX packages
For structured output, include algorithm and algpseudocode in the Markdown document’s header.
- 3. Insert the algorithm
Place the algorithm environment in a raw LaTeX block so Pandoc passes it through to PDF.
- 4. Convert and check
Run Pandoc with a working LaTeX engine, then inspect the PDF for placement and formatting.
Markdown does not define a standard algorithm or pseudocode block. A fenced code block is the simplest portable choice, but it will usually appear as monospaced text rather than a numbered, captioned algorithm. If you are producing a PDF with Pandoc and want that structured layout, use LaTeX’s algorithm and algpseudocode packages inside a raw LaTeX block.
Option 1: Use a fenced code block
For notes, drafts, or content that needs to display in several Markdown renderers, write pseudocode as ordinary text inside a fence:
BinarySearch(A, x):
low = 0
high = length(A) - 1
while low <= high:
mid = floor((low + high) / 2)
if A[mid] = x:
return mid
if A[mid] < x:
low = mid + 1
else:
high = mid - 1
return -1
The text label avoids asking Pandoc to apply the syntax rules of a programming language. The fence preserves indentation and makes the pseudocode easy to read, but does not add an algorithm number, caption, or automatically formatted line numbers. Markdown renderers vary in their support for syntax highlighting, so do not rely on highlighting for meaning.
Option 2: Use a LaTeX algorithm environment in Pandoc
For a PDF with a caption and line numbers, save a file such as pseudocode.md with a YAML header, then put the algorithm in a raw LaTeX block. This complete example uses zero-based indexing and assumes A is sorted in ascending order.
---
title: Binary search pseudocode
header-includes:
- |
\usepackage{algorithm}
\usepackage{algpseudocode}
---
```{=latex}
\begin{algorithm}
\caption{Binary search}
\begin{algorithmic}[1]
\Procedure{BinarySearch}{$A, x$}
\State $low \gets 0$
\State $high \gets |A| - 1$
\While{$low \le high$}
\State $mid \gets \lfloor (low + high)/2 \rfloor$
\If{$A[mid] = x$}
\State \Return $mid$
\ElsIf{$A[mid] < x$}
\State $low \gets mid + 1$
\Else
\State $high \gets mid - 1$
\EndIf
\EndWhile
\State \Return $-1$
\EndProcedure
\end{algorithmic}
\end{algorithm}
```
Convert it to PDF with Pandoc and a LaTeX engine installed:
pandoc pseudocode.md --pdf-engine=xelatex -o pseudocode.pdf
algorithm provides the floating algorithm environment and caption; algpseudocode provides commands such as \Procedure, \State, \While, and \If. The optional [1] on algorithmic requests line numbers. The {=latex} fence tells Pandoc to pass the enclosed LaTeX through rather than treat it as ordinary code. The packages must be installed in the LaTeX distribution used by the chosen engine.
The search returns the index of x if it is present, and -1 otherwise. Each loop iteration halves the remaining search interval, so the running time is for an array of length ; the extra space is .
Choosing between the two formats
| Need | Suitable format |
|---|---|
| A readable, portable snippet | Fenced Markdown code block |
| Captions, algorithm numbering, or line numbers in PDF | LaTeX algorithm environment |
| The same source rendered to HTML and PDF | Fenced code, or separate output-specific formatting |
Raw LaTeX is specific to LaTeX-based output. It can work well for Pandoc-to-PDF, but a renderer targeting HTML may omit it or show it as text. If you need both outputs, test each target and consider keeping a plain pseudocode version for HTML.
Common mistakes
- Expecting Markdown to format an algorithm automatically. A code fence preserves text formatting; it does not create a captioned algorithm float.
- Leaving out the LaTeX packages. Commands such as
\Procedureare not built into Pandoc. Add the package that defines them and make sure it is installed. - Using a regular code fence for LaTeX. A fence such as
```latexdisplays the source as code. Use the{=latex}raw block when the goal is to execute LaTeX during PDF generation. - Assuming a float stays exactly where it appears. LaTeX may move an algorithm to improve page layout. If its position matters, look up the float-placement options supported by your chosen packages rather than assuming Markdown controls it.
- Forgetting the PDF engine. Pandoc needs a working LaTeX installation to create PDFs this way. If conversion fails, check that the engine and required packages are installed before changing the pseudocode.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.