Documentation/Specialized Notation/How to Write Pseudocode and Code Listings in LaTeX
Specialized Notation

How to Write Pseudocode and Code Listings in LaTeX

Computer science papers frequently include algorithms (as pseudocode) and source code (as formatted listings). LaTeX offers several approaches: algorithmicx and algorithm2e for pseudocode, and listings and minted for syntax-highlighted code. This guide covers all four, helping you choose the right tool. Bibby AI supports minted with Pygments out of the box — no shell-escape configuration needed, unlike Overleaf where you must enable it manually.

Write Pseudocode with algorithmicx

Use the algorithm and algpseudocode packages for IEEE/ACM-style pseudocode with line numbers:

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}

\begin{algorithm}
\caption{Binary Search}\label{alg:bsearch}
\begin{algorithmic}[1]  % [1] enables line numbers
\Require Sorted array $A[1..n]$, target value $t$
\Ensure Index $i$ such that $A[i] = t$, or $-1$
\State $\textit{lo} \gets 1$
\State $\textit{hi} \gets n$
\While{$\textit{lo} \leq \textit{hi}$}
    \State $\textit{mid} \gets \lfloor (\textit{lo} + \textit{hi}) / 2 \rfloor$
    \If{$A[\textit{mid}] = t$}
        \State \Return $\textit{mid}$
    \ElsIf{$A[\textit{mid}] < t$}
        \State $\textit{lo} \gets \textit{mid} + 1$
    \Else
        \State $\textit{hi} \gets \textit{mid} - 1$
    \EndIf
\EndWhile
\State \Return $-1$
\end{algorithmic}
\end{algorithm}

See Algorithm~\ref{alg:bsearch} for the binary search procedure.

\end{document}

Format Source Code with the listings Package

Use listings for syntax-highlighted code in many programming languages without external dependencies:

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}

\lstset{
    basicstyle=\ttfamily\small,
    keywordstyle=\color{blue}\bfseries,
    commentstyle=\color{green!60!black}\itshape,
    stringstyle=\color{red!70!black},
    numberstyle=\tiny\color{gray},
    numbers=left,
    numbersep=8pt,
    frame=single,
    breaklines=true,
    captionpos=b,
    tabsize=4,
    showstringspaces=false
}

\begin{document}

\begin{lstlisting}[language=Python, caption={Quicksort in Python}]
def quicksort(arr):
    """Sort array using quicksort algorithm."""
    if len(arr) <= 1:
        return arr
    pivot = arr[len(arr) // 2]
    left = [x for x in arr if x < pivot]
    middle = [x for x in arr if x == pivot]
    right = [x for x in arr if x > pivot]
    return quicksort(left) + middle + quicksort(right)
\end{lstlisting}

\end{document}

Use algorithm2e for Alternative Pseudocode Style

The algorithm2e package offers a different pseudocode style popular in European CS papers:

\documentclass{article}
\usepackage[ruled, vlined, linesnumbered]{algorithm2e}

\begin{document}

\begin{algorithm}[H]
\caption{Dijkstra's Shortest Path}\label{alg:dijkstra}
\KwIn{Graph $G = (V, E)$, source vertex $s$}
\KwOut{Shortest distances $d[v]$ for all $v \in V$}

\ForEach{$v \in V$}{
    $d[v] \gets \infty$\;
    $\textit{visited}[v] \gets \textit{false}$\;
}
$d[s] \gets 0$\;
$Q \gets$ priority queue with all vertices\;

\While{$Q \neq \emptyset$}{
    $u \gets Q.\text{extractMin}()$\;
    $\textit{visited}[u] \gets \textit{true}$\;
    \ForEach{neighbor $v$ of $u$}{
        \If{$\neg\textit{visited}[v]$ \textbf{and} $d[u] + w(u,v) < d[v]$}{
            $d[v] \gets d[u] + w(u,v)$\;
            $Q.\text{decreaseKey}(v, d[v])$\;
        }
    }
}
\Return $d$\;
\end{algorithm}

\end{document}

💡 Tips

  • Use algorithmicx (algpseudocode) for ACM/IEEE-style pseudocode and algorithm2e for European-style — don't load both.
  • The listings package works everywhere but has limited highlighting. Use minted (requires Pygments) for publication-quality syntax coloring.
  • Always add \usepackage{algorithm} alongside algpseudocode — the first creates the float, the second provides the pseudocode commands.
  • For inline code snippets, use \lstinline|code here| (listings) or \mintinline{python}{code} (minted).

Try This in Bibby AI

Write LaTeX faster with AI auto-complete and instant compilation.

Start Writing Free

Related Tutorials

How to Write Pseudocode and Code Listings in LaTeX | Bibby AI