Algorithms & Theorems

Source Code, Algorithms & Theorems

Typeset programming source code with syntax highlighting, algorithm pseudocode procedures, and mathematical theorem/proof environments.

Code Listings (Listings & Minted)

Code Listings (Listings & Minted)

Comprehensive syntax, arguments, options, and copyable snippets for code listings (listings & minted).

Environment Mandatory { }

\begin{lstlisting}...\end{lstlisting}

Typesets a block of source code with syntax highlighting (listings package)

Example Code
\begin{lstlisting}[language=Python]
def add(a, b):
    return a + b
\end{lstlisting}
Command Mandatory { }

\lstinputlisting{file}

Includes and typesets an external source code file (listings package)

Example Code
\lstinputlisting[language=Python]{goscat.py}
Command Mandatory { }

\lstset{options}

Sets global default options for the listings package

Example Code
\lstset{basicstyle=\ttfamily\small, numbers=left}
Command

\lstinline|code|

Inline syntax-highlighted code (listings package)

Example Code
\lstinline|import numpy as np|
Environment Mandatory { }

\begin{minted}{lang}...\end{minted}

Typesets syntax-highlighted code using Pygments (minted package)

Example Code
\begin{minted}{python}
import numpy as np
\end{minted}
Command Mandatory { }

\mintinline{lang}{code}

Inline syntax-highlighted code (minted package)

Example Code
\mintinline{python}{x = np.array([1,2,3])}
Command Mandatory { }

\inputminted{lang}{file}

Includes and highlights an external code file (minted package)

Example Code
\inputminted{python}{goscat.py}
Algorithms & Pseudocode

Algorithms & Pseudocode

Comprehensive syntax, arguments, options, and copyable snippets for algorithms & pseudocode.

Environment Mandatory { }

\begin{algorithm}...\end{algorithm}

Floating wrapper for a numbered algorithm (algorithm package)

Example Code
\begin{algorithm}
\caption{Bisection method}
\begin{algorithmic}
\State ...
\end{algorithmic}
\end{algorithm}
Environment Mandatory { }

\begin{algorithmic}...\end{algorithmic}

Typesets pseudocode with control-flow keywords (algorithmicx package)

Example Code
\begin{algorithmic}
\State $x \gets 0$
\While{$x < 10$}
  \State $x \gets x+1$
\EndWhile
\end{algorithmic}
Command

\State

A single pseudocode statement line (algorithmicx)

Example Code
\State $x \gets x + 1$
Command Mandatory { }

\If{cond} ... \EndIf

Conditional pseudocode block (algorithmicx)

Example Code
\If{$x > 0$}
  \State positive
\EndIf
Command Mandatory { }

\For{cond} ... \EndFor

For-loop pseudocode block (algorithmicx)

Example Code
\For{$i = 1$ to $n$}
  \State process($i$)
\EndFor
Command Mandatory { }

\While{cond} ... \EndWhile

While-loop pseudocode block (algorithmicx)

Example Code
\While{$x < 10$}
  \State $x \gets x+1$
\EndWhile
Command

\Require

Marks the preconditions of an algorithm (algorithmicx)

Example Code
\Require $n > 0$
Command

\Ensure

Marks the postconditions/output of an algorithm (algorithmicx)

Example Code
\Ensure sorted array
Command

\Return

Marks a return statement in pseudocode (algorithmicx)

Example Code
\Return $x$
Command Mandatory { }

\Comment{text}

Adds an inline comment in pseudocode (algorithmicx)

Example Code
\State $x \gets 0$ \Comment{initialize}
Theorems & Proofs (amsthm)

Theorems & Proofs (amsthm)

Comprehensive syntax, arguments, options, and copyable snippets for theorems & proofs (amsthm).

Command Mandatory { }

\newtheorem{name}{Text}

Defines a new numbered theorem-like environment (amsthm/base)

Example Code
\newtheorem{theorem}{Theorem}
Command Mandatory { } Optional [ ]

\newtheorem{name}{Text}[within]

Defines a new theorem-like environment numbered within a sectioning unit

Example Code
\newtheorem{corollary}{Corollary}[section]
Command Mandatory { }

\theoremstyle{style}

Sets the display style (plain, definition, remark) for subsequent theorem environments (amsthm)

Example Code
\theoremstyle{definition}
Command

\qed

Inserts the end-of-proof symbol (amsthm)

Example Code
\qed