global
// JavaScript code
function gen_matrix(nrow, ncol) {
	let results = "";
	let order = 1;
	for (var i=0; i < nrow; i++){
		results += ' ';
		for(var j = 0; j < ncol - 1; j++){
			results += "$" + (order).toString() + " & ";
			order ++;
		}
		results += "$" + (order).toString() + " \\\\" + "\\";
		order ++;
	}
	return results;
}

function gen_matrix_transposed(nrow, ncol) {
	let results = "";
	for (var i = 0; i < nrow; i++){
		results += ' ';
        var j = 0;
		for(; j < ncol - 1;j++){
			results += "$" + (i + j * ncol + 1).toString() + " & ";
		}
		results += "$" + (i + j * ncol + 1).toString() + " \\\\" + "\\";
	}
	return results;
}

function tes_matrix(nrow, ncol,t) {
	let results = "";
	let order = 1;
	for (var i=0; i<nrow; i++){
		results += '	';
		for(var j = 0;j <ncol-1;j++){
			if (order > 1 ){
				results += "${" +(order ).toString() + ":" + t[order-2] + "}\t & ";
			}
			else{
				results += "$" +(order ).toString()  + " & ";
			}
			order ++;
		}
		results += "$"+(order).toString() +" \\\\" + "\\ ";
		order ++;
	}
	return results;
}

// 输出一个表格
function createTable(rows, cols) {
    let ret = "";
    let order = 1;
    for (let i = 0; i < parseInt(rows) + 2; i++) {
        for (let j = 0; j < parseInt(cols); j++) {
            if (i === 1) {
                ret += "|---";
            } else {
                ret += "| $" + (order).toString() + " ";
                order++;
            }
        }
        ret += "|\n"
    }
    return ret;
}

endglobal

snippet example "Example" i
\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}

\begin{document}
\title{${1:title}}
\author{Peikun Yang}
\date{\today}
\maketitle
$0
\end{document}
endsnippet

# == Fraction Match ==


snippet // "Fraction" iAm
\\frac{${1:${VISUAL}}}{$2}$0
endsnippet

snippet `((\d+)|(\d*)(\\)?([A-Za-z!]+)((\^|_)(\{\d+\}|\d))*)/` "Fraction no ()" iAm
\frac{``rv = m[1]``}{$1}$0
endsnippet

priority 200
snippet `(?<=\s)(\\mu|\\alpha|\\sigma|\\rho|\\beta|\\gamma|\\delta|\\zeta|\\eta|\\varepsilon|\\theta|\\iota|\\kappa|\\vartheta|\\lambda|\\nu|\\pi|\\rho|\\tau|\\upsilon|\\phi|\\chi|\\psi|\\omega|\\Gamma|\\Delta|\\Theta|\\Lambda|\\Xi|\\Pi|\\Sigma|\\Upsilon|\\Phi|\\Psi|\\Omega|[A-Za-z]{1,2})?(_[A-Za-z0-9]|_\{[^}]+\})?(\^[A-Za-z0-9]|\^\{[^}]+\})?(_[A-Za-z0-9]|_\{[^}]+\})?(\([^)]+\))(_[A-Za-z0-9]|_\{[^}]+\})?(\^[A-Za-z0-9]|\^\{[^}]+\})?(_[A-Za-z0-9]|_\{[^}]+\})?\/` "Fraction with ()" iAm
``rv = "\\frac{" + m.slice(1, m.length).join('') + "}{$1}$2"``
endsnippet

# == Hat Operation ==

# ==== Auto Capture Hat Operation ====
snippet `(\\?[a-zA-Z]\w*({?\w*})?)(hbar|BAR)` "Bar" iAm
\overline{``rv = m[1] + m[2] + m[3]``}
endsnippet

snippet `(\\?[a-zA-Z]\w*({?\w*})?)(htd|TD)` "tilde" iAm
\tilde{``rv = m[1]``}
endsnippet

snippet `(\\?[a-zA-Z]\w*({?\w*})?)bar` "bar" iAm
\bar{``rv = m[1]``}
endsnippet

snippet `(\\?[a-zA-Z]\w*({?\w*})?)(hat|HAT)` "hat" iAm
\hat{``rv = m[1]``}
endsnippet

snippet `(\\?[a-zA-Z]\w*({?\w*})?)(hvec)` "Vector postfix" iAm
\vec{``rv = m[1]``}
endsnippet

snippet `(\\?[a-zA-Z]\w*({?\w*})?)(rta)` "Vector postfix" iAm
\overrightarrow{``rv = m[1]``}
endsnippet

snippet `(\\?[a-zA-Z]\w*({?\w*})?)(dot)` "dot" iAm
\dot{``rv = m[1]``}
endsnippet

priority 1000
snippet `(\\?[a-zA-Z]\w*({?\w*})?)(hdd)` "ddot" iAm
\ddot{``rv = m[1]``}
endsnippet

# ===== Static Hat Operation ====

snippet hbar "bar" iAm
\overline{$1}$0
endsnippet

snippet hat "hat" iAm
\hat{$1}$0
endsnippet

snippet sq "\sqrt{}" iAm
\sqrt{${1}}$0
endsnippet

priority 10000
snippet psq "\sqrt[]{}" iAm
\sqrt[${2}]{${1}}
endsnippet

# == Superscript Operation ==

snippet invs "inverse" iAm
^{-1}
endsnippet

snippet dig "digree" iAm
^{\circ}
endsnippet

priority 10000
snippet TR "inverse" iAm
^{\mathsf{T}}
endsnippet

snippet CL "complement" iAm
^{c}
endsnippet

snippet R+ "R0+" iAm
R_0^+
endsnippet

snippet pow "power" iAm
^{${1:n}}
endsnippet

snippet tp "to the ... power" iAm
^{${1:2}}$0
endsnippet

snippet sr "square" iAm
^{2}$0
endsnippet

snippet cb "cube" iAm
^{3}$0
endsnippet

# == Subscript Operation ==

snippet td "subscript" iAm
_{${1}}$0
endsnippet

snippet sb "subscript" iAm
_{${0}}
endsnippet

snippet `(})(\d)\2` "auto subscript" iAm
`` rv = m[1] + "_" + m[2]``
endsnippet

snippet `([A-Za-z])(\d)` "auto subscript" iAm
`` rv = m[1] + "_" + m[2]``
endsnippet

priority 100
snippet `([A-Za-z])_(\d{2})` "auto subscript" iAm
`` rv = m[1] + "_{" + m[2] + "}$0" ``
endsnippet

priority 100
snippet `([A-Za-z])S(\d)` "auto subscript" iAm
`` rv = m[1] + "_{" + m[2] + "$1}$2"``
endsnippet

snippet `\b(?<!\\)([A-Za-z}])([a-z])\2` "auto subscript 2" iAm
`` rv = m[1] + "_" + m[2].substring(0, 1) ``
endsnippet

snippet `\b(?<!\\)([A-Za-z}])S([a-z])\2` "auto subscript 2" iAm
`` rv = m[1] + "_{" + m[2].substring(0, 1) + "$1}$2"``
endsnippet

# Custom: Add more greek letters
snippet `(\\mu|\\alpha|\\sigma|\\rho|\\beta|\\gamma|\\delta|\\zeta|\\eta|\\varepsilon|\\epsilon|\\theta|\\iota|\\kappa|\\vartheta|\\lambda|\\nu|\\pi|\\rho|\\tau|\\upsilon|\\phi|\\chi|\\psi|\\omega|\\Gamma|\\Delta|\\Theta|\\Lambda|\\Xi|\\Pi|\\Sigma|\\Upsilon|\\Phi|\\Psi|\\Omega)([a-z])\2` "auto subscript for greek letter" iAm
`` rv = m[1] + "_" + m[2].substring(0, 1) ``
endsnippet

snippet `(\\mu|\\alpha|\\sigma|\\rho|\\beta|\\gamma|\\delta|\\zeta|\\eta|\\varepsilon|\\epsilon|\\theta|\\iota|\\kappa|\\vartheta|\\lambda|\\nu|\\pi|\\rho|\\tau|\\upsilon|\\phi|\\chi|\\psi|\\omega|\\Gamma|\\Delta|\\Theta|\\Lambda|\\Xi|\\Pi|\\Sigma|\\Upsilon|\\Phi|\\Psi|\\Omega)S([a-z])\2` "auto subscript for greek letter" iAm
`` rv = m[1] + "_{${1:" + m[2].substring(0, 1) + "}}$2"``
endsnippet


# == Font Operation ==

# ==== Static Operation ====

snippet txt "text" iAm
\text{$1}$0
endsnippet

snippet tit "text it" iAm
\textit{$1}$0
endsnippet

snippet mcal "mathcal" im
\mathcal{$1}$0
endsnippet

snippet mbb "mathbb" iAm
\mathbb{$1}$0
endsnippet

snippet mbf "mathbf" iAm
\mathbf{$1}$0
endsnippet

snippet mbm "mathbm" iAm
\mathbm{$1}$0
endsnippet

snippet KK "^K" iAm
^{\mathrm{K}}
endsnippet

snippet TT "^T" iAm
^{\mathrm{T}}
endsnippet

snippet HH "^H" iAm
^{\mathrm{H}}
endsnippet

snippet RR "R" iAm
\mathbb{R}
endsnippet

snippet NN "N" iAm
\mathbb{N}
endsnippet

snippet ZZ "Z" iAm
\mathbb{Z}
endsnippet

snippet ZZ "Z" iAm
\mathbb{Z}
endsnippet

snippet QQ "Q" iAm
\mathbb{Q}
endsnippet

snippet CC "C" iAm
\mathbb{C}
endsnippet

# ==== Dynamic Operation ====

priority 100
snippet `(\\?[a-zA-Z]\w*({?\w*})?)(bf|BF)` "mathbf" iAm
\mathbf{``rv = m[1]``}
endsnippet

priority 100
snippet `(\\?[a-zA-Z]\w*({?\w*})?)(bm|BM)` "mathbm" iAm
\bm{``rv = m[1]``}
endsnippet

snippet 0bm "0bm" iAm
\bm{0}
endsnippet

snippet 1bm "1bm" iAm
\bm{1}
endsnippet

priority 100
snippet `(\\?[a-zA-Z]\w*({?\w*})?)(bs)` "boldsymbol" iAm
\boldsymbol{``rv = m[1]``}
endsnippet

priority 100
snippet `(\\?[a-zA-Z]\w*({?\w*})?)(sf)` "mathsf" iAm
\mathsf{``rv = m[1]``}
endsnippet

priority 100
snippet `(\\?[a-zA-Z]\w*({?\w*})?)(frak)` "mathfrak" iAm
\mathfrak{``rv = m[1]``}
endsnippet

priority 100
snippet `(\\?[a-zA-Z]\w*({?\w*})?)cal` "mathcal" iAm
\mathcal{``rv = m[1].toUpperCase()``}$0
endsnippet

priority 100
snippet `(?<!\\)\b([a-zA-Z]+)rm` "mathrm" iAm
\mathrm{``rv = m[1]``}
endsnippet

priority 100
snippet `(?<!\\)\b([a-zA-Z]+)opn` "operatorname" iAm
\operatorname{``rv = m[1]``}
endsnippet

priority 100
snippet `(\\?[a-zA-Z]\w*({?\w*})?)bb` iAm
\mathbb{``rv = m[1]``}$0
endsnippet


# == Auto Symbol ==

snippet oo "\infty" iAmm
\infty
endsnippet

snippet ... "cdots" iAm
\cdots
endsnippet

snippet `(\d\d+)\.\.` "0, 1, 2, ..., n" iAm
``rv = m[1].split('').map((d) => "${1:_}" + d + "${2:, }").join('');``\cdots${3:${2:, }${1:_}n}
endsnippet

snippet `(\d\d+),` "0, 1, 2" iAm
``rv = m[1].split('').map((d) => "${1:_}" + d).join("${2:, }");``
endsnippet

snippet <> "hokje" iA
\diamond 
endsnippet

# +... -> , \cdots
# -  ... -> , \cdots
# add a space if there already is one.
priority 101
snippet `(?<=[-+])\s*\.\.\.` "smart cdots" imA
 \cdots 
endsnippet

# It seems that \ldots is only used when , ..., 
# ,... -> , \ldots
# ,  ... -> , \ldots
priority 101
snippet `(?<=,)(\s*)\.\.\.` "smart ldots" imA
 \ldots 
endsnippet

snippet ** "dot multiply" iAm
\cdot 
endsnippet

snippet +- "pm" iAm
\pm 
endsnippet

snippet -+ "mp" iAm
\mp 
endsnippet

snippet odot "odot" iAm
\odot 
endsnippet

priority 101
snippet xx "cross" iAm
\times 
endsnippet

snippet eps "epsilon" iAm
\epsilon
endsnippet

priority 100
snippet veps "varepsilon" iAm
\varepsilon
endsnippet

priority 100
snippet ell "ell" iAm
\ell
endsnippet

priority 100
snippet log "log" iAm
\log
endsnippet

snippet bin "binom" iAm
\binom{$1}{$2}
endsnippet

snippet oth "otherwise" iAm
\text{otherwise}
endsnippet

snippet star "star" iAm
^{*}
endsnippet

snippet `(?<!\\)(oint|iiint|iint|int)` "integrate" iAm
\\``rv = m[1]``
endsnippet

snippet `(?<!\\)(sum|min|max|argmin|argmax|sup|inf)` "sum|min|max|argmin|argmax|sup|inf" iAm
\\``rv = m[1]``
endsnippet

snippet `(?<!\\)(sin|cos|tan|arccot|cot|csc|ln|exp|det|perp|arcsin|arccos|arctan|arccot|arccsc|arcsec|ell|nabla|notin|not)` "function" iAm
\\``rv = m[1]``
endsnippet

snippet `(?<!\\)(mu|alpha|sigma|rho|beta|Beta|gamma|delta|pi|zeta|eta|varepsilon|theta|iota|kappa|vartheta|lambda|nu|pi|rho|tau|upsilon|varphi|phi|chi|psi|omega|Gamma|Delta|Theta|Lambda|Xi|Pi|Sigma|Upsilon|Phi|Psi|Omega)` "greek" iAm
\\``rv = m[1]``
endsnippet

# ==== Space Symbol ====
snippet `(?<!\\)(quad)` "ln" iAm
\\``rv = m[1]``
endsnippet

# ==== Logic Symbol ====


snippet empty "empty" iAm
\empty
endsnippet

priority 200
snippet inn "in" iAm
\in 
endsnippet

priority 200
snippet `\\ln ?ot` "in" iAm
\lnot 
endsnippet

snippet sse "subseteq" iAm
\subseteq 
endsnippet

snippet sqs "sqsubset" iAm
\sqsubseteq 
endsnippet

snippet tto "to" iAm
\to 
endsnippet

priority 200
snippet ott "leftarrow" iAm
\leftarrow 
endsnippet

priority 200
snippet `(?<!\\)(cap|cup|land|lor|lnot|oplus|ominus|otimes|sqcap|sqcup|vdash|models)` "logic operator" iAm
\\``rv = m[1]`` 
endsnippet

priority 200
snippet `(?<=\b|\d+)(?<!\\)(bot|top|dagger)` "logic symbols" iAm
\\``rv = m[1]``
endsnippet

snippet -> "to" iAm
\to 
endsnippet

snippet !> "mapsto" iAm
\mapsto 
endsnippet

snippet => "implies" iAm
\implies
endsnippet

snippet =< "implied by" iAm
\impliedby
endsnippet

priority 200
snippet iff "if and only if" iAm
\iff
endsnippet

snippet EE "exist" iAm
\exists 
endsnippet

snippet AA "forall" iAm
\forall 
endsnippet

snippet bec "because" iAm
\because 
endsnippet

snippet thr "therefore" iAm
\therefore 
endsnippet


# ==== Possibility Symbol ====

snippet Pr "Pr" iAm
\operatorname{Pr}
endsnippet

snippet Var "Var" iAm
\operatorname{Var}
endsnippet

snippet Cov "Cov" iAm
\operatorname{Cov}
endsnippet

snippet Exp "Expected" iAm
\mathbb{E}
endsnippet


# ==== Compare Symbol ====

snippet -- "setminus" iAm
\setminus
endsnippet

snippet >= "greater than" iAm
\ge $0
endsnippet

snippet dis "displaystyle" iAm
\displaystyle 
endsnippet

snippet <= "less than" iAm
\le $0
endsnippet

snippet != "no equals" iAm
\neq 
endsnippet

snippet == " constan equals" iAm
\equiv 
endsnippet

snippet sim "sim" iAm
\sim 
endsnippet

snippet `\\sim eq` "simeq" iAm
\simeq 
endsnippet

snippet ~~ " Amppro equals" iAm
\approx 
endsnippet

snippet ~= " Amppro equals2" iAm
\cong
endsnippet

snippet >> ">>" iAm
\gg
endsnippet


snippet << "<<" iAm
\ll
endsnippet


# == Auto Environment ==

# ==== Auto Math Mode ====

snippet mk "inline Math" wA
\(${1}\)$0
endsnippet

snippet dm "display Math" w
\[
${1}
\]
endsnippet

snippet dmi "display Math" wA
\[$1\]$0
endsnippet

snippet eqt "equation" wA
\begin{equation}
  ${1}
\end{equation}
endsnippet

snippet eqs "equation*" wA
\begin{equation*}
  ${1}
\end{equation*}
endsnippet

# ==== Common Environment ====

# snippet case "cases" iAm
# \begin{cases} $1, & $2 \\\\ $3, & $4 \end{cases}
# endsnippet

snippet ali "aligned" iAm
\begin{aligned}
$1 \\\\
\end{aligned}
endsnippet

# == Auto Adaptive Close ==

snippet ceil "ceil" iAm
\left\lceil $1 \right\rceil $0
endsnippet

snippet floor "floor" iAm
\left\lfloor $1 \right\rfloor$0
endsnippet

priority 100
snippet @) "left( right)" Aim
\left( ${1} \right) $0
endsnippet

priority 100
snippet @| "left| right|" Aim
\left| ${1} \right| $0
endsnippet

priority 100
snippet @\ "left. right|" Aim
\left. ${1} \right|_{${2}}$0
endsnippet

priority 100
snippet @} "left\{ right\}" Aim
\left\\{ ${1} \right\\} $0
endsnippet

priority 100
snippet set "\{ \}" Aim
\\{ ${1} \\}$0
endsnippet

priority 100
snippet @] "left[ right]" Aim
\left[ ${1} \right] $0
endsnippet

priority 100
snippet @> "leftangle rightangle" iAm
\left<${1} \right>$0
endsnippet

priority 200

# == Snippet ==

# ==== General Snippet ====

# ====== Lite Snippet ======

snippet tag "tag" iAm
\tag{$1}
endsnippet

snippet xyb "Auto (x, y)" iAm
(x, y)
endsnippet

snippet xyzb "Auto (x, y ,z)" iAm
(x, y, z)
endsnippet

priority 100
snippet `\b([a-zA-Z])n(\d)` "x[n+1]" iAm
``rv = m[1]``_{${1:n}+``rv = m[2]``}$0
endsnippet

# Unkown
snippet rij "mrij" iAm
(${1:x}_${2:n})_{${3:$2} \\in ${4:N}}$0
endsnippet

priority 200
snippet abs "absolute value" iAm
\left\vert ${1} \right\vert $0
endsnippet

snippet beg "begin{} / end{}" bA
\\begin{$1}
	$0
\\end{$1}
endsnippet

# ======== N Series ========

priority 100
snippet comma "comma" iAm
${1:\\alpha}_1,${1:\\alpha}_2,\\cdots,${1:\\alpha}_${2:n}
endsnippet

priority 100
snippet plus "plus" iAm
${1:k}_1${2:\\alpha}_1+${1:k}_2${2:\\alpha}_2+\\cdots+${1:k}_${3:n}${2:\\alpha}_${3:n}
endsnippet

snippet `\b([ijk])=n` "i=1,2,\cdots,n" iAm
``rv = m[1]``=1,2,\cdots,n
endsnippet

# ======== Common Operator Snippet ========

snippet taylor "taylor" iAm
\sum_{${1:k}=${2:0}}^{${3:\infty}} ${4:c_$1} (x-a)^$1 $0
endsnippet

snippet `(?<!\\)lim` "limit" iAm
\lim_{${1:n} \to ${2:\infty}}
endsnippet

snippet `(?<!\\)prod` "product" iAm
\prod_{${1:n=${2:1}}}^{${3:\infty}} ${4:${VISUAL}}$0
endsnippet

snippet `(?<!\\)part` "d/dx" iAm
\frac{\partial ${1:V}}{\partial ${2:x}}$0
endsnippet

priority 300
snippet `(?<!\\)diff` "d/dx" iAm
\frac{\mathrm{d}${1:y}}{\mathrm{d}${2:x}}$0
endsnippet

priority 400
snippet `(?<!\\)2diff` "d/dx" iAm
\frac{\mathrm{d}^2${1:y}}{\mathrm{d}${2:x}^2}$0
endsnippet

priority 400
snippet `(?<!\\)3diff` "d/dx" iAm
\frac{\mathrm{d}^3${1:y}}{\mathrm{d}${2:x}^3}$0
endsnippet

priority 300
snippet `dd` "dd" iAm
\mathrm{d}
endsnippet

snippet buu "bigcup" iAm
\bigcup_{${1:i \in ${2: I}}} $0
endsnippet

snippet bnn "bigcap" iAm
\bigcap_{${1:i \in ${2: I}}} $0
endsnippet

priority 100
snippet dint "integral" iAm
\int_{${1:-\infty}}^{${2:\infty}} ${3} \\mathrm{d}${4:x}$0
endsnippet

priority 200
snippet `c(o|n)?(l|n)?(b|c)?int` "s 	egral" iAm
``
let final = "\\"; // init
let isO = m[1] == "o";
(isO) ? final += "o" : "" // o option
let b = 1;
let isL = m[2] == "l";
(m[3] == 'b') ? b = 2 : (m[3] == 'c') ? b = 3 : 1;
for (let i = 0; i < b - 1; i++) {
final += "i";
}
final += "int";
final += ((b >= 2) || (b != 1 && !isO && isL)) ? "\\limits" : "";
let r = (b == 3) ? "E" : (b == 1 && (isL || isO)) ? "C" : "R";
final += ((b >= 2) || isO || (b == 1 && isL)) ? "_{${1:" + r + "}}" : "_{${1:-\\infty}}^{${2:\\infty}}";
let x = (b == 2) ? "A" : (b == 3) ? "V" : (b == 1 && isL) ? "s" : "x";
final += " ${3} \\mathrm{d}${4:" + x + "}$0";
rv = final;
``
endsnippet

# Custom: Can add more defined operator
priority 100
snippet `(?<![\a-zA-Z])(rank|trace|svd|eye|ones|orth|rows|cols|zeros|diag|rref|hstack|vstack|nullspace|eigen|dim|lcm|gcd|atan2|softmax|eig|sign|const)` "math function" iAm
\\operatorname{``rv = m[1]``}
endsnippet

# ====== Big Snippet ======

snippet bigdef "Big function" iAm
\begin{equation$6}
    \begin{aligned}
        $1\colon $2 &\longrightarrow $3 \\\\
                 $4 &\longmapsto $1($4) = $5
    \end{aligned}
\end{equation$6}$0
endsnippet

priority 300
snippet bigmin "Optimization problem" iAm
\begin{equation$4}
	\begin{aligned}
		\min &\quad ${1:f(x)}\\\\
		\text{s.t.} &\quad ${2:g(x)} \leq 0\\\\
					&\quad ${3:h(x)} = 0\\\\
	\end{aligned}
\end{equation$4}$0
endsnippet

priority 300
snippet bigmax "Optimization problem" iAm
\begin{equation$4}
	\begin{aligned}
		\max &\quad ${1:f(x)}\\\\
		\text{s.t.} &\quad ${2:g(x)} \leq 0\\\\
					&\quad ${3:h(x)} = 0\\\\
	\end{aligned}
\end{equation$4}$0
endsnippet


priority 300
snippet Argmin "Optimization problem" iAm
\begin{aligned}
\argmin_{${1:\beta}}
&\quad ${2:f(x)}  \\\\
\text{s.t.}
&\quad ${3:g(x) > 0}  \\\\
\end{aligned}
endsnippet

priority 300
snippet Argmax "Optimization problem" iAm
\begin{aligned}
\argmax_{${1:\beta}}
&\quad ${2:f(x)}  \\\\
\text{s.t.}
&\quad ${3:g(x) > 0}  \\\\
\end{aligned}
endsnippet

snippet deff "Definition of function" iAm
$1\colon ${2:\\mathbb{R\}} \to ${3:\\mathbb{R\}}, ${4:x} \mapsto $0
endsnippet


snippet iid "independent and identical distribution" iAm
\overset{\text{i.i.d.}}{\sim}
endsnippet

snippet defe "define equal" iAm
\overset{\underset{\mathrm{def}}{}}{=}
endsnippet

snippet deft "define triangleq" iAm
\triangleq 
endsnippet


# == Matrix ==

# ==== Static Matrix ====

snippet pmat "pmat" wm
\begin{pmatrix} 
    ${1: } 
\end{pmatrix} $0
endsnippet

snippet bmat "pmat" wm
\begin{bmatrix} 
    $1 
\end{bmatrix} $0
endsnippet

snippet vecC "column vector" iAm
\begin{bmatrix} ${1:x}_1 \\\\ ${1:x}_2 \\\\ \vdots \\\\ ${1:x}_${2:n} \end{bmatrix}
endsnippet

snippet vecR "row vector" iAm
\begin{bmatrix} ${1:x}_1, ${1:x}_2, \cdots, ${1:x}_${2:n} \end{bmatrix}$0
endsnippet

priority 300
snippet omis "omission" iAm
\\begin{bmatrix}${1:1}&${2:1}&\\cdots&${4:1}\\\\${5:1}&${6:1}&\\cdots&${8:1}\\\\\\vdots&\\vdots&\\ddots&\\vdots\\\\${13:1}&${14:1}&\\cdots&${16:1}\\end{bmatrix}
endsnippet

priority 300
snippet submat "omission" iAm
\\begin{bmatrix}
    ${1:a}_{11} & ${1:a}_{12} & \\cdots & ${1:a}_{1n} \\\\
    ${1:a}_{21} & ${1:a}_{22} & \\cdots & ${1:a}_{2n} \\\\
    \\vdots & \\vdots & \\ddots & \\vdots \\\\
    ${1:a}_{n1} & ${1:a}_{n2} & \\cdots & ${1:a}_{nn}
\\end{bmatrix}
endsnippet

priority 300
snippet subplusmat "omission" iAm
\\begin{bmatrix}
    ${1:a}_{11}+${2:b}_{11} & ${1:a}_{12}+${2:b}_{12} & \\cdots & ${1:a}_{1n}+${2:b}_{1n} \\\\
    ${1:a}_{21}+${2:b}_{21} & ${1:a}_{22}+${2:b}_{22} & \\cdots & ${1:a}_{2n}+${2:b}_{2n} \\\\
    \\vdots & \\vdots & \\ddots & \\vdots \\\\
    ${1:a}_{n1}+${2:b}_{n1} & ${1:a}_{n2}+${2:b}_{n2} & \\cdots & ${1:a}_{nn}+${2:b}_{nn}
\\end{bmatrix}
endsnippet

snippet jacobi "jacobi" iAm
\\begin{bmatrix}\\frac{\\partial ${1:f}_1}{\\partial ${2:x}_1}&\\frac{\\partial ${1:f}_1}{\\partial ${2:x}_2}&\\cdots&\\frac{\\partial ${1:f}_1}{\\partial ${2:x}_${3:n}}\\\\\\frac{\\partial ${1:f}_2}{\\partial ${2:x}_1}&\\frac{\\partial ${1:f}_2}{\\partial ${2:x}_2}&\\cdots&\\frac{\\partial ${1:f}_2}{\\partial ${2:x}_${3:n}}\\\\\\vdots&\\vdots&\\ddots&\\vdots\\\\\\frac{\\partial ${1:f}_${3:m}}{\\partial ${2:x}_1}&\\frac{\\partial ${1:f}_${3:m}}{\\partial ${2:x}_2}&\\cdots&\\frac{\\partial ${1:f}_${3:m}}{\\partial ${2:x}_${3:n}}\\end{bmatrix}
endsnippet

# ==== Dynamic Matrix ====

priority 300
snippet `(b|p|v)mata([1-9])` "bmatrix" iiAm
\\begin{``rv = m[1]``matrix}``
	let len = m[2];
	let results = "";
	for (var i=0; i<len; i++){
		results += "$1 &".repeat(len-1) + " $1 \\\\\\\\";
	}
	rv = results;
``\\end{``rv = m[1]``matrix}$0
endsnippet

priority 300
snippet `(b|p|v)mat([1-9])` "bmatrix" iiAm
\\begin{``rv = m[1]``matrix}``
	rv = gen_matrix(m[2],m[2]);
``\\end{``rv = m[1]``matrix}$0
endsnippet

priority 2000
snippet `(b|p|v)matr([1-9]{1})` "bmatrix" iiAm
\\begin{``rv = m[1]``matrix}``
	rv = gen_matrix_transposed(m[2],m[2]);
``\\end{``rv = m[1]``matrix}$0
endsnippet

priority 300
snippet `vec([1-9])` "col vector" iiAm
\\begin{bmatrix}``
    rv = gen_matrix(m[1], 1);
``\\end{bmatrix}$0
endsnippet

priority 300
snippet `vecr([1-9])` "row vector" iiAm
\\begin{bmatrix}``
    rv = gen_matrix(1, m[1]);
``\\end{bmatrix}$0
endsnippet


# == General ==

snippet \box "Box" 
``rv = '┌' + '─'.repeat(t[0].length + 2) + '┐'``
│ $1 │
``rv = '└' + '─'.repeat(t[0].length + 2) + '┘'``
endsnippet


priority 300
snippet `table(\d)(\d)` "create table with rows and columns" wA
``
rv = createTable(m[1], m[2]);
``
endsnippet

snippet ee "exp" iAm
e^{$1}
endsnippet

snippet H "highlight" iA
#highlight[${VISUAL}]
endsnippet