All guides
Advanced

Algorithms, code and theorems

Pseudocode with algpseudocode, source code with listings, and theorem environments with amsthm.

Pseudocode

The algorithm and algpseudocode packages give numbered algorithms with keywords (\State, \For, \If, \Return).

\begin{algorithm}
\caption{Gradient descent}
\begin{algorithmic}[1]
  \State $w \gets w_0$
  \For{$t = 1$ to $T$}
    \State $w \gets w - \eta \nabla L(w)$
  \EndFor
  \State \Return $w$
\end{algorithmic}
\end{algorithm}

Source code

The listings package prints code with highlighting; choose the language and a style.

\begin{lstlisting}[language=Python]
def mean(xs):
    return sum(xs) / len(xs)
\end{lstlisting}

Theorems and proofs

With amsthm, declare the kinds you need once in the preamble, then use them like environments. proof ends with a square automatically.

\newtheorem{theorem}{Theorem}
\newtheorem{lemma}[theorem]{Lemma}
…
\begin{theorem}\label{thm:main}
Every bounded monotone sequence converges.
\end{theorem}
\begin{proof}
…
\end{proof}
Try it: the full example compiles as it is. Open as a project