Source Code, Algorithms & Theorems
Typeset programming source code with syntax highlighting, algorithm pseudocode procedures, and mathematical theorem/proof environments.
Code Listings (Listings & Minted)
Comprehensive syntax, arguments, options, and copyable snippets for code listings (listings & minted).
\begin{lstlisting}...\end{lstlisting}
Typesets a block of source code with syntax highlighting (listings package)
\begin{lstlisting}[language=Python]
def add(a, b):
return a + b
\end{lstlisting}
\lstinputlisting{file}
Includes and typesets an external source code file (listings package)
\lstinputlisting[language=Python]{goscat.py}
\lstset{options}
Sets global default options for the listings package
\lstset{basicstyle=\ttfamily\small, numbers=left}
\lstinline|code|
Inline syntax-highlighted code (listings package)
\lstinline|import numpy as np|
\begin{minted}{lang}...\end{minted}
Typesets syntax-highlighted code using Pygments (minted package)
\begin{minted}{python}
import numpy as np
\end{minted}
\mintinline{lang}{code}
Inline syntax-highlighted code (minted package)
\mintinline{python}{x = np.array([1,2,3])}
\inputminted{lang}{file}
Includes and highlights an external code file (minted package)
\inputminted{python}{goscat.py}
Algorithms & Pseudocode
Comprehensive syntax, arguments, options, and copyable snippets for algorithms & pseudocode.
\begin{algorithm}...\end{algorithm}
Floating wrapper for a numbered algorithm (algorithm package)
\begin{algorithm}
\caption{Bisection method}
\begin{algorithmic}
\State ...
\end{algorithmic}
\end{algorithm}
\begin{algorithmic}...\end{algorithmic}
Typesets pseudocode with control-flow keywords (algorithmicx package)
\begin{algorithmic}
\State $x \gets 0$
\While{$x < 10$}
\State $x \gets x+1$
\EndWhile
\end{algorithmic}
\State
A single pseudocode statement line (algorithmicx)
\State $x \gets x + 1$
\If{cond} ... \EndIf
Conditional pseudocode block (algorithmicx)
\If{$x > 0$}
\State positive
\EndIf
\For{cond} ... \EndFor
For-loop pseudocode block (algorithmicx)
\For{$i = 1$ to $n$}
\State process($i$)
\EndFor
\While{cond} ... \EndWhile
While-loop pseudocode block (algorithmicx)
\While{$x < 10$}
\State $x \gets x+1$
\EndWhile
\Require
Marks the preconditions of an algorithm (algorithmicx)
\Require $n > 0$
\Ensure
Marks the postconditions/output of an algorithm (algorithmicx)
\Ensure sorted array
\Return
Marks a return statement in pseudocode (algorithmicx)
\Return $x$
\Comment{text}
Adds an inline comment in pseudocode (algorithmicx)
\State $x \gets 0$ \Comment{initialize}
Theorems & Proofs (amsthm)
Comprehensive syntax, arguments, options, and copyable snippets for theorems & proofs (amsthm).
\newtheorem{name}{Text}
Defines a new numbered theorem-like environment (amsthm/base)
\newtheorem{theorem}{Theorem}
\newtheorem{name}{Text}[within]
Defines a new theorem-like environment numbered within a sectioning unit
\newtheorem{corollary}{Corollary}[section]
\theoremstyle{style}
Sets the display style (plain, definition, remark) for subsequent theorem environments (amsthm)
\theoremstyle{definition}
\qed
Inserts the end-of-proof symbol (amsthm)
\qed