local ls = require 'luasnip' local fmta = require('luasnip.extras.fmt').fmta local function in_math() local node = vim.treesitter.get_node() while node do if node:type() == 'inline_formula' or node:type() == 'math_environment' then return true end node = node:parent() end return false end local snippets = {} local function create_snippet(trig, snip, desc, math) local snip = snip or '\\' .. trig snip = snip .. '<>' local desc = desc or trig local _, icount = string.gsub(snip, '<>', '') local is = {} local condition = {} if math == nil then condition = { condition = in_math } end for i = 1, icount - 1 do table.insert(is, ls.insert_node(i)) end table.insert(is, ls.insert_node(0)) table.insert(snippets, ls.snippet({ trig = trig, snippetType = 'autosnippet', desc = desc }, fmta(snip, is), condition)) end local add = create_snippet add('bold', '**<>**', 'bold', false) for i = 1, 9 do add('x' .. i, 'x_' .. i) add('xb' .. i, 'x_{' .. i .. '<>}') end add('xn', 'x_n') add('xbn', 'x_{n<>}') add('xb', 'x_{<>}') add('dots', '\\dots', 'dots') add 'langle' add 'rangle' add 'land' add 'lor' add 'iff' add 'cdot' add('mathbb', '\\mathbb{<>}') add 'rightarrow' add 'leftarrow' add('^', '^{<>}', '^') add('_', '_{<>}', '_') add('text', '\\text{<>}', 'text') add('exists', '\\exists') add('forall', '\\forall') add 'in' add('mathcal', '\\mathcal{<>}') add 'setminus' add('textit', '\\textit{<>}') add('frac', '\\frac{<>}{<>}', 'frac') add('sqrt', '\\sqrt{<>}', 'sqrt') add('times', '\\times', 'times') add('imp', '\\implies', 'implies') add('sum', '\\sum_{<>}^{<>}', 'sum') add('inf', '\\infinity', 'infinity') add('cases', '\\begin{cases} <> & \\text{if } <> \\text{then} <> \\\\ <> & {else } <> \\end{cases}', 'cases') add('int', '\\int_{<>}^{<>} <> \\, d', 'int') add('vec', '\\vec{<>}', 'vector') add 'emptyset' -- set operations add('subset', '\\subset', 'subset') add('subseteq', '\\subseteq', 'subset equals') add('cup', '\\cup', 'cup') add('cap', '\\cap', 'cap') -- bool add('leq', '\\leq', 'less equal') add('geq', '\\geq', 'greater equals') local greek_letters = { 'alpha', 'beta', 'gamma', 'delta', 'epsilon', 'zeta', 'eta', 'theta', 'iota', 'kappa', 'lambda', 'mu', 'nu', 'xi', 'omicron', 'pi', 'rho', 'sigma', 'tau', 'upsilon', 'phi', 'chi', 'psi', 'omega', } for _, letter in pairs(greek_letters) do add(letter, '\\' .. letter, letter) local upper_letter = letter:sub(1, 1):upper() .. letter:sub(2) add(upper_letter, '\\' .. upper_letter, upper_letter) end table.insert( snippets, ls.snippet( { trig = 'im', snippetType = 'snippet', desc = 'Inline Math' }, fmta('$<>$<>', { ls.i(1), ls.i(0), }) ) ) return snippets -- return { -- -- 1. Trigger inline math mode anywhere by typing 'mk' -- s( -- { trig = 'mk', snippetType = 'autosnippet', desc = 'Inline Math' }, -- fmta('$<>$<>', { -- i(1), -- i(0), -- }) -- ), -- -- -- 2. LaTeX Fraction: Only triggers if you are already inside $ ... $ -- s( -- { trig = 'ff', snippetType = 'autosnippet', desc = 'LaTeX Fraction' }, -- fmta('\\frac{<>}{<>}<>', { -- i(1), -- i(2), -- i(0), -- }), -- { -- condition = in_math, -- This magic line prevents 'ff' from triggering in normal text -- } -- ), -- -- -- 3. Superscript shortcut: Triggers inside math mode -- s({ trig = 'sr', snippetType = 'autosnippet', desc = 'Squared' }, { t '^2' }, { condition = in_math }), -- }