Skip to content

Instantly share code, notes, and snippets.

@ekzhang
Last active February 26, 2023 05:27
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ekzhang/33b5de25b843788951d02f2adbe1d038 to your computer and use it in GitHub Desktop.
Save ekzhang/33b5de25b843788951d02f2adbe1d038 to your computer and use it in GitHub Desktop.
LaTeX template and useful macros
\ProvidesPackage{ekzhang}
\usepackage{amsmath,amsfonts,amssymb,amsthm}
\usepackage{asymptote}
\usepackage{cancel}
\usepackage{color}
\usepackage[dvipsnames]{xcolor}
\usepackage{enumerate}
\usepackage[margin=1in]{geometry}
\usepackage{graphicx}
\usepackage{mathtools}
\usepackage{minted}
\usepackage{tikz,tikz-cd}
\usepackage{verbatim}
\usepackage[colorlinks=true]{hyperref}
\hypersetup{
linkcolor=NavyBlue,
citecolor=ForestGreen,
filecolor=Plum,
urlcolor=RubineRed,
}
\usepackage[capitalise,nameinlink]{cleveref}
\newtheorem{theorem}{Theorem}[section]
\newtheorem{corollary}{Corollary}[theorem]
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{proposition}[theorem]{Proposition}
\theoremstyle{definition}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{example}[theorem]{Example}
\newtheorem{exercise}{Exercise}[section]
\newtheorem*{note}{Note}
\newtheorem*{claim}{Claim}
\newcommand{\C}{\mathbb{C}}
\newcommand{\F}{\mathbb{F}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\CO}{\mathcal{O}}
\newcommand{\CC}{\mathcal{C}}
\newcommand{\CU}{\mathcal{U}}
\DeclarePairedDelimiter{\pa}{\lparen}{\rparen}
\DeclarePairedDelimiter{\br}{[}{]}
\DeclarePairedDelimiter{\cbr}{\{}{\}}
\DeclarePairedDelimiter{\ang}{\langle}{\rangle}
\DeclarePairedDelimiter{\abs}{\lvert}{\rvert}
\DeclarePairedDelimiter{\dabs}{\|}{\|}
\DeclarePairedDelimiter{\eval}{.}{|}
\DeclarePairedDelimiter{\floor}{\lfloor}{\rfloor}
\DeclarePairedDelimiter{\ceil}{\lceil}{\rceil}
\newcommand{\p}{\partial}
\newcommand{\dd}{\mathop{}\!\mathrm{d}}
\newcommand{\dv}[3][]{\frac{\dd^{#1} #2}{\dd #3^{#1}}}
\newcommand{\pdv}[3][]{\frac{\p^{#1} #2}{\p #3^{#1}}}
\newcommand{\del}{\nabla}
\newcommand{\mat}[1]{\begin{matrix} #1 \end{matrix}}
\newcommand{\smat}[1]{\begin{smallmatrix} #1 \end{smallmatrix}}
\newcommand{\bmat}[1]{\begin{bmatrix} #1 \end{bmatrix}}
\newcommand{\bsmat}[1]{\begin{bsmallmatrix} #1 \end{bsmallmatrix}}
\newcommand{\pmat}[1]{\begin{pmatrix} #1 \end{pmatrix}}
\newcommand{\psmat}[1]{\begin{psmallmatrix} #1 \end{psmallmatrix}}
\newcommand{\E}[1]{\operatorname{\mathbf{E}}\left[#1\right]}
\newcommand{\Var}[1]{\operatorname{\mathbf{Var}}\left[#1\right]}
\newcommand{\lcm}{\operatorname*{lcm}}
\newcommand{\argmax}{\operatorname*{arg max}}
\newcommand{\argmin}{\operatorname*{arg min}}
\newcommand{\into}{\hookrightarrow}
\newcommand{\onto}{\twoheadrightarrow}
\newcommand{\im}{\operatorname{im}}
\newcommand{\rank}{\operatorname{rank}}
\newcommand{\ch}{\operatorname{char}}
\newcommand{\id}{\operatorname{id}}
\newcommand{\ord}{\operatorname{ord}}
\newcommand{\sgn}{\operatorname{sgn}}
\newcommand{\Hom}{\operatorname{Hom}}
\newcommand{\End}{\operatorname{End}}
\newcommand{\Aut}{\operatorname{Aut}}
\newcommand{\Ann}{\operatorname{Ann}}
\newcommand{\Sym}{\operatorname{Sym}}
\newcommand{\GL}{\operatorname{GL}}
\newcommand{\SL}{\operatorname{SL}}
\newcommand{\Ob}{\mathrm{Ob}}
\newcommand{\Mor}{\mathrm{Mor}}
\newcommand{\extp}{\@ifnextchar^\@extp{\@extp^{\,}}}
\def\@extp^#1{\mathop{\bigwedge\nolimits^{\!#1}}}
\newcommand{\ds}{\displaystyle}
\newcommand{\on}{\operatorname}
\newcommand{\nc}{\newcommand}
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{ekzhang}
\title{\LaTeX{} Template}
\author{Eric K. Zhang\\
\href{mailto:ekzhang@college.harvard.edu}{ekzhang@college.harvard.edu}}
\date{\today}
\begin{document}
\maketitle
\section{Introduction}
\label{sec:intro}
This is a simple template for personal use. Macros are defined in the \href{run:./ekz.sty}{\texttt{ekzhang.sty}} package.
\section{Math Macros}
Several math macros have been added to facilitate the typesetting of common expressions. The \verb|\dd| macro produces the differential symbol with proper spacing. The \verb|\dv| and \verb|\pdv| macros can be used to quickly typeset derivatives. Macros have been added for expectation \verb|\E| and variance \verb|\Var|, which automatically surround their argument with brackets.
For convenience, matrix environments have macro forms. For example, \verb|\mat{x}| is equivalent to \verb|\begin{matrix} x \end{matrix}|. Other bracketed and small variations, including \verb|\bmat|, \verb|\pmat|, \verb|\smat|, \verb|\bsmat|, and \verb|\psmat|, are available.
Several paired delimiters (from \verb|mathtools|) are included. Each delimiter can be dynamically scaled by adding an asterisk \verb|*| to the corresponding macro. For example, \verb|\pa*{x^2}| is equivalent to \verb|\left(x^2\right)|. Other macros include \verb|\br| (brackets), \verb|\cbr| (braces), \verb|\ang| (angle brackets), \verb|\abs| (absolute value), \verb|\floor|, \verb|\ceil|, and \verb|\dabs| (magnitude).
\[ \pdv ut = k\pa*{\pdv[2] ux + \pdv[2] uy + \pdv[2] uz}. \]
\[ \pdv{L}{x} - \dv{}{t}\br*{\pdv{L}{\dot x}} = 0. \]
\[ \int_0^2 x^2 \dd x = \eval*{\frac 13 x^3}_0^2. \]
\[ \oint_{\partial\Sigma} \vec F \cdot \dd \vec\ell = \iint_{\Sigma} \del \times \vec F \cdot \dd \vec S. \]
\[ \det \bmat{2 & 3 \\ 5 & 6} = \abs*{\mat{2 & 3 \\ 5 & 6}}. \]
\[ \pi(s) = \argmax_a \cbr*{\sum_{s'} P(s' \mid s, a) (R(s' \mid s, a) + \gamma V(s'))}. \]
\[ \Var{X} = \E{X^2} - \E{X}^2. \]
\[ \Var{\sum_{i=1}^n X_i} = \sum_{i=1}^n \Var{X_i} + 2\sum_{1 \leq i < j \leq n} \on{Cov}(X_i, X_j). \]
\section{Symbols}
Some common symbols and operators have been provided, mostly for abstract and linear algebra.
\[ \C, \F, \Q, \Z, \R, \N, \CO, \CC, \CU, \]
\[ \into, \onto, \im, \rank, \ch, \id, \ord, \sgn, \]
\[ \lcm, \argmax, \argmin \]
\[ \Hom, \End, \Aut, \Ann, \Sym, \GL, \SL, \Ob, \Mor, \del, \extp^n. \]
\section{Theorems and References}
This is an example of a reference to \cref{sec:intro}.
\begin{definition}[Self-adjoint]
\label{def:self-adjoint}
We call a linear operator $T : V \to V$ \textit{self-adjoint} if $T = T^*$. In other words, for all $v, w \in V$, \[
\ang{v, Tw} = \ang{Tv, w}.
\]
\end{definition}
\begin{theorem}[Spectral theorem]
Any self-adjoint operator (see \cref{def:self-adjoint}) has an orthonormal basis of eigenvectors, with all real eigenvalues.
\end{theorem}
\begin{corollary}[Principal axis theorem]
If $T$ is self-adjoint, then there exists an orthogonal matrix $Q$ of eigenvectors and diagonal matrix $\Lambda$ of real eigenvalues such that \[
T = Q\Lambda Q^*.
\]
\end{corollary}
\begin{note}
The full list of available environments is: theorem, corollary, lemma, proposition, definition, example, exercise, note, claim.
\end{note}
\section{Asymptote Diagram}
We can use the \verb|asy| environment to draw diagrams, as in \cref{fig:asy-diagram}.
\begin{figure}[h]
\centering
\begin{asy}
size(6cm);
import markers;
import geometry;
draw(unitcircle);
draw(scale(0.75)*unitcircle);
pair O = (0, 0);
pair T = (0, -1);
pair P = dir(-90 + 41.4) * 0.75;
dot(P);
draw(O--T, L=Label("$R$", align=W, position=MidPoint));
draw(O--P, L=Label("$r$", align=dir(21.4), position=MidPoint));
draw(T--P);
pair X = 3*P - 2*T;
pair Y = P + (1.3, 0);
draw(P--X, dashed);
draw(P--Y, dotted);
markangle("$\theta$", radius=13, Y, P, X);
markangle("$\theta$", radius=13, T, O, P);
perpendicular(P, NE, O-P, size=2.5mm);
\end{asy}
\caption{A diagram drawn with Asymptote.}
\label{fig:asy-diagram}
\end{figure}
\pagebreak
\section{Code Listing}
We can produce code listings with syntax highlighting using the \verb|minted| package.
\begin{listing}[h]
\begin{minted}[frame=single,linenos]{python}
def preorders(n):
stack, order = [], []
def dfs():
if not stack:
yield tuple(order)
return
s, e = stack.pop()
for k in range(s, e + 1):
order.append(k)
if k < e: stack.append((k + 1, e))
if k > s: stack.append((s, k - 1))
yield from dfs()
if k > s: stack.pop()
if k < e: stack.pop()
order.pop()
stack.append((s, e))
stack.append((1, n))
return dfs()
\end{minted}
\caption{Enumeration of rooted binary trees.}
\end{listing}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment