Skip to content

Instantly share code, notes, and snippets.

@lucasgruwez
Last active April 13, 2024 05:43
Show Gist options
  • Save lucasgruwez/ac0ee45c2b5da289fa1b11f594525578 to your computer and use it in GitHub Desktop.
Save lucasgruwez/ac0ee45c2b5da289fa1b11f594525578 to your computer and use it in GitHub Desktop.
Snippets I use with vim for super-productive latex editing
################################################################################
# Set math environment, to allow some snippets to run only when in inline or
# disp math. i.e. sr ==> disregard != di^2egard.
################################################################################
global !p
texMathZones = ['texMathZone'+x for x in ['A', 'AS', 'B', 'BS', 'C', 'CS', 'D', 'DS', 'E', 'ES', 'F', 'FS', 'G', 'GS', 'H', 'HS', 'I', 'IS', 'J', 'JS', 'K', 'KS', 'L', 'LS', 'DS', 'V', 'W', 'X', 'Y', 'Z']]
texIgnoreMathZones = ['texMathText']
texMathZoneIds = vim.eval('map('+str(texMathZones)+", 'hlID(v:val)')")
texIgnoreMathZoneIds = vim.eval('map('+str(texIgnoreMathZones)+", 'hlID(v:val)')")
ignore = texIgnoreMathZoneIds[0]
def math():
synstackids = vim.eval("synstack(line('.'), col('.') - (col('.')>=2 ? 1 : 0))")
try:
first = next(i for i in reversed(synstackids) if i in texIgnoreMathZoneIds or i in texMathZoneIds)
return first != ignore
except StopIteration:
return False
endglobal
################################################################################
# Base template all files are based on
################################################################################
snippet template "Template"
\title{$1}
\author{Lucas Gruwez, IB M20}
\date{\today}
\documentclass[a4paper,12pt]{article}
\usepackage{amsmath,amsfonts,amssymb} % Math packages
\usepackage{amsthm} % Theorems
\usepackage{array} % Better tables
\usepackage{chemfig} % chemical figures
\usepackage{commath} % derivatives and partials
\usepackage{enumitem} % List manipulation
\usepackage{float} % Better positioning [H]
\usepackage{framed} % Framed boxes
\usepackage{geometry} % Required for adjusting page dimensions and margins
\usepackage{graphicx} % Include images
\usepackage{mhchem} % chem equations
\usepackage{multirow} % multicolumn tables
\usepackage{pgfplots} % Create plots in latex
\usepackage{siunitx} % SI unit system
\renewcommand{\arraystretch}{1.6}
\theoremstyle{definition}
\newtheorem*{definition}{Definition}
\newenvironment{define}[1]
{\begin{framed}\begin{definition}{#1\\\\[1ex]}}
{\end{definition}\end{framed}}
\geometry{
paper=a4paper, % Paper size, change to letterpaper for US letter size
top=2.5cm, % Top margin
bottom=2.5cm, % Bottom margin
left=2.5cm, % Left margin
right=2.5cm, % Right margin
headheight=14pt, % Header height
footskip=1.5cm, % Space from the bottom margin to the baseline of the footer
headsep=1.2cm, % Space from the top margin to the baseline of the header
}
\let\tss\textsuperscript % superscript macro
\let\oldtextbf\textbf
\renewcommand{\textbf}[1]{\oldtextbf{\boldmath #1}}
\newcommand{\reaction}[1]{\begin{equation}\ce{#1}\end{equation}}
\begin{document}
\maketitle
$0
\end{document}
endsnippet
################################################################################
# Start snippets for regular latex commands, i.e. paragraph, begin end, math
# environment, figures, etc.
################################################################################
snippet beg "begin{} / end{}" bA
\begin{$1}
$0
\end{$1}
endsnippet
snippet abs "abstract environment" b
\begin{abstract}
$0
\end{abstract}
endsnippet
snippet it "Individual item" bA
\item $0
endsnippet
snippet par "paragraph{}" bA
\paragraph{}
$0
endsnippet
snippet *sec "section" bA
\section*{$1}
$0
endsnippet
snippet sec "section" bA
\section{$1}
$0
endsnippet
snippet *sub "subection" bA
\subsection*{$1}
$0
endsnippet
snippet sub "subsection" bA
\subsection{$1}
$0
endsnippet
snippet *ssub "subsubsection" bA
\subsubsection*{$1}
$0
endsnippet
snippet ssub "subsubsection" bA
\subsubsection{$1}
$0
endsnippet
snippet fig "figure{}" bA
\begin{figure}[${1:htpb}]
\centering
\includegraphics[width=0.8\textwidth]{$3}
\caption{$4}
\label{fig:$5}
\end{figure}
endsnippet
snippet graph "Tikz pgfplot" b
\begin{figure}[${1:htpb}]
\centering
\begin{tikzpicture}\begin{axis}[
axis lines=middle,
width=240pt,
height=166pt,
samples=128,
no marks,
xlabel=$x$, ylabel=$y$,
xtick={0}, xticklabels={\$0\$},
ytick={0}, yticklabels={\$0\$},
xmin=-3, xmax=3, ymin=-3, ymax=3
]
\addplot[color=black] {$0};
\end{axis}\end{tikzpicture}
\caption{$2}
\label{plot:$3}
\end{figure}
endsnippet
snippet plot "Tikz single \addplot command" bA
\addplot[color=black${1:,domain=0:1,dashed}] $0;
endsnippet
snippet coord "Tikz coordinates" bA
cordinates{($1) ($0)};
endsnippet
snippet table "table{}" b
\begin{table}[${1:htpb}]
\centering
\begin{tabular}{${5:c}}
$0
\end{tabular}
\caption{${2:caption}}
\label{tab:${3:label}}
\end{table}
endsnippet
snippet colex "Column explanation (IA)" b
\begin{tabular}{l c p{12cm}}
\textbf{Column 1} & - & $0
\end{tabular}
endsnippet
snippet col "Column" b
\textbf{Column $1} & - & $0
endsnippet
snippet list "enumerate{}" bA
\begin{enumerate}
\item $0
\end{enumerate}
endsnippet
snippet ulist "enumerate{}[-]" bA
\begin{enumerate}[label=-]
\item $0
\end{enumerate}
endsnippet
snippet def "definition{}" bA
\begin{define}{$1}
$0
\end{define}
endsnippet
snippet desc "Description" b
\begin{description}
\item[$1] $0
\end{description}
endsnippet
snippet pkg "Package" b
\usepackage[${1:options}]{${2:package}}$0
endsnippet
################################################################################
# Inline and disp math, as well as equation snippets
################################################################################
snippet ilm "Math" wA
$${1}$`!p
if t[2] and t[2][0] not in [',', '.', '?', '-', ' ']:
snip.rv = ' '
else:
snip.rv = ''
`$2
endsnippet
snippet dpm "Math" wA
$$
$1
.$$ $0
endsnippet
snippet equ "equation{}" bA
\begin{equation}
$0
\label{eq:$1}
\end{equation}
endsnippet
snippet *equ "equation{}" bA
\begin{equation*}
$0
\end{equation*}
endsnippet
snippet align "align{}" bA
\begin{align}
$0
\label{eq:$1}
\end{align}
endsnippet
snippet *align "align{}" bA
\begin{align*}
$0
\end{align*}
endsnippet
################################################################################
# Snippets for fast math typing, from castel.dev
# ASCII math like snippets => =< <= :. ... ** xx |->
################################################################################
context "math()"
snippet txt "\text{}" iA
\text{$1}$0
endsnippet
context "math()"
snippet => "implies" Ai
\implies
endsnippet
context "math()"
snippet =< "implied by" Ai
\impliedby
endsnippet
context "math()"
snippet |-> "mapsto" Ai
\mapsto
endsnippet
context "math()"
snippet +- "plus minus" Ai
\pm
endsnippet
context "math()"
snippet -+ "minus plus" Ai
\mp
endsnippet
context "math()"
snippet ** "dot" iA
\cdot
endsnippet
context "math()"
snippet xx "cross" iA
\times
endsnippet
context "math()"
snippet -:- "divide" iA
\div
endsnippet
snippet ... "ellipse" Ai
\ldots
endsnippet
snippet :. "therefore" Ai
\therefore
endsnippet
################################################################################
# Equality snippets
################################################################################
context "math()"
snippet << "ll" Ai
\ll
endsnippet
context "math()"
snippet >> "gg" Ai
\gg
endsnippet
context "math()"
snippet <= "le" Ai
\le
endsnippet
context "math()"
snippet >= "ge" Ai
\ge
endsnippet
context "math()"
snippet != "neq" Ai
\ne
endsnippet
context "math()"
snippet ~= "approx" Ai
\approx
endsnippet
context "math()"
snippet === "equivalent" Ai
\equiv
endsnippet
context "math()"
snippet def= "defeq" Ai
\overset{\underset{\mathrm{def}}{}}{=}
endsnippet
################################################################################
# Other math symbols
################################################################################
context "math()"
snippet oo "infty" Ai
\infty
endsnippet
context "math()"
snippet oc "proportional" Ai
\propto
endsnippet
################################################################################
# Sub and superscript. Use only in math env
################################################################################
context "math()"
snippet '([A-Za-z])(\d)' "auto subscript" wrA
`!p snip.rv = match.group(1)`_`!p snip.rv = match.group(2)`
endsnippet
context "math()"
snippet '([A-Za-z])_(\d\d)' "auto subscript2" wrA
`!p snip.rv = match.group(1)`_{`!p snip.rv = match.group(2)`}
endsnippet
context "math()"
snippet sq "^2" iA
^2
endsnippet
context "math()"
snippet cb "^3" iA
^3
endsnippet
context "math()"
snippet compl "complement" iA
^{c}
endsnippet
context "math()"
snippet td "superscript" iA
^{$1}$0
endsnippet
context "math()"
snippet sr "sqrt{}" iA
\sqrt{$1}$0
endsnippet
context "math()"
snippet nr "sqrt{}" iA
\sqrt[$1]{$2}$0
endsnippet
################################################################################
# Fractions using regex and //
################################################################################
context "math()"
snippet // "Fraction" iA
\\frac{$1}{$2}$0
endsnippet
priority 100
context "math()"
snippet '((\d+)|(\d*)(\\)?([A-Za-z]+)((\^|_)(\{\d+\}|\d))*)/' "Fraction" wrA
\\frac{`!p snip.rv = match.group(1)`}{$1}$0
endsnippet
priority 1000
context "math()"
snippet '^.*\)/' "() Fraction" wrA
`!p
stripped = match.string[:-1]
depth = 0
i = len(stripped) - 1
while True:
if stripped[i] == ')': depth += 1
if stripped[i] == '(': depth -= 1
if depth == 0: break;
i -= 1
snip.rv = stripped[0:i] + "\\frac{" + stripped[i+1:-1] + "}"
`{$1}$0
endsnippet
################################################################################
# Over text type snippets, i.e. v., => \vec{v}, xdot => \dot{x}, vddot =>
# \ddot{v}
################################################################################
priority 10
context "math()"
snippet "bar" "bar" ri
\overline{$1}$0
endsnippet
priority 100
context "math()"
snippet "([a-zA-Z])bar" "bar" riA
\overline{`!p snip.rv=match.group(1)`}
endsnippet
priority 10
context "math()"
snippet "hat" "hat" ri
\hat{$1}$0
endsnippet
priority 100
context "math()"
snippet "([a-zA-Z])hat" "hat" riA
\hat{`!p snip.rv=match.group(1)`}
endsnippet
priority 100
context "math()"
snippet "([a-ce-zA-Z])dot" "dot" riA
\dot{`!p snip.rv=match.group(1)`}
endsnippet
context "math()"
snippet "(\\?\w+)(,\.|\.,)" "Vector postfix" riA
\vec{`!p snip.rv=match.group(1)`}
endsnippet
################################################################################
# Sums products etc
################################################################################
context "math()"
snippet sum "sum" wA
\sum_{$1}^{$2} $0
endsnippet
context "math()"
snippet prod "product" wA
\prod_{$1}^{$2} $0
endsnippet
context "math()"
snippet taylor "taylor series" wA
\sum_{n=0}^{\infty} \frac{${1:f^{(n)}(0)} \cdot ${2:x^n}}{${3:n!}}$0
endsnippet
################################################################################
# Calculus
################################################################################
context "math()"
snippet int "indefinite integral" wA
\int $0 d${1:x}
endsnippet
context "math()"
snippet dint "definite integral" wA
\int_{$1}^{$2} $0 d${3:x}
endsnippet
context "math()"
snippet lim "limit"
\lim_{$1 \to $2} $0
endsnippet
context "math()"
snippet odx "d/dx" wA
\od{$1}{x} $0
endsnippet
context "math()"
snippet ody "d/dx" wA
\od{$1}{y} $0
endsnippet
context "math()"
snippet odt "d/dx" wA
\od{$1}{t} $0
endsnippet
context "math()"
snippet od "d/dx" wA
\od[$1]{$2}{$3} $0
endsnippet
context "math()"
snippet pd "d/dx" wA
\pd[$1]{$2}{$3} $0
endsnippet
context "math()"
snippet md "d/dx" wA
\md{$1}{$2}$0
endsnippet
context "math()"
snippet eval "eval int" wA
\eval{$3}_{$1}^{$2}
endsnippet
context "math()"
snippet nab "nabla" wA
\nabla
endsnippet
context "math()"
snippet grad "nabla" wA
\nabla
endsnippet
################################################################################
# Matrices and vectors
################################################################################
context "math()"
snippet mat "Matrix"
\begin{bmatrix}
$0
\end{bmatrix}
endsnippet
context "math()"
snippet det "Determinant matrix"
\begin{vmatrix}
$0
\end{vmatrix}
endsnippet
context "math()"
snippet vec "Vector"
\begin{pmatrix}
$0
\end{pmatrix}
endsnippet
context "math()"
snippet matil "Inline matrix"
\left[ \begin{smallmatrix}
$0
\end{smallmatrix} \right]
endsnippet
context "math()"
snippet detil "Inline determinant"
\left| \begin{smallmatrix}
$0
\end{smallmatrix} \right|
endsnippet
context "math()"
snippet vecil "Inline vector"
\left( \begin{smallmatrix}
$0
\end{smallmatrix} \right)
endsnippet
context "math()"
snippet choose "n choose p"
{$1 \choose $2} $0
endsnippet
################################################################################
# (), [], {}, and their bigger counterparts
################################################################################
context "math()"
snippet ( "left(" i
\left(
endsnippet
context "math()"
snippet ) "right)" i
\right)
endsnippet
context "math()"
snippet [ "left[" i
\left[
endsnippet
context "math()"
snippet ] "right]" i
\right]
endsnippet
context "math()"
snippet { "left{" i
\left{
endsnippet
context "math()"
snippet } "right}" i
\right}
endsnippet
################################################################################
# QED QEA
################################################################################
snippet qed "Q.E.D." wA
Q.E.D.
endsnippet
snippet qea "Q.E.A." wA
Q.E.A.
endsnippet
################################################################################
# Run code straight from the latex file, such as sympy, maybe eval or matplotlib
# or whatevs later
################################################################################
# sympy blocks of code
snippet sympy "sympy block " w
sympy $1 sympy$0
endsnippet
priority 10000
snippet 'sympy(.*)sympy' "evaluate sympy" wr
`!p
from sympy import *
x, y, z, t = symbols('x y z t')
k, m, n, p = symbols('k m n p', integer=True)
f, g, h, u, v = symbols('f g h u v', cls=Function)
init_printing()
snip.rv = eval('latex(' + match.group(1).replace('\\', '') \
.replace('^', '**') \
.replace('{', '(') \
.replace('}', ')') + ')')
`
endsnippet
################################################################################
# Chemistry
################################################################################
snippet react "reaction" bA
\reaction{$1}
endsnippet
snippet *react "reaction no num" bA
\reactionstar{$1}
endsnippet
snippet ce "ce" w
\ce{$1} $0
endsnippet
snippet CH2 "C with H on top and bottom" w
C([2]-H)([6]-H)
endsnippet
snippet COOH "carboxyl" w
C([1]=O)([7]-OH)
endsnippet
snippet CH=O "aldehyde" w
C([1]=O)([7]-H)
endsnippet
snippet CHO "carbonyl" w
C([2]=O)
endsnippet
snippet CH-OH "hydroxide" w
C([2]-OH)([6]-H)
endsnippet
snippet CONH2 "carboxyamide" w
C([1]=O)([7]-N([1]-H)([7]-H))
endsnippet
snippet COOC "ester" w
C([1]=O)[7]-O-C
endsnippet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment