Skip to content

Commit e4d782a

Browse files
committed
feat: add more math snippets, bullet points, format a bit
1 parent fdff372 commit e4d782a

File tree

4 files changed

+243
-22
lines changed

4 files changed

+243
-22
lines changed

lua/luasnip-latex-snippets/luasnippets/tex/commands.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ M = {
4848
\label{<>:<>}
4949
]],
5050
{ i(1), i(0) })),
51+
5152
autosnippet({ trig='([acCer])ref', name='(acC|eq)?ref', dscr='add a reference (with autoref, cref, eqref)', regTrig = true, hidden = true},
5253
fmta([[
5354
\<>ref{<>:<>}

lua/luasnip-latex-snippets/luasnippets/tex/environments.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ local autosnippet = ls.extend_decorator.apply(s, { snippetType = "autosnippet" }
3333
-- personal imports
3434
--]
3535
local tex = require("luasnip-latex-snippets.luasnippets.tex.utils.conditions")
36+
local make_condition = require("luasnip.extras.conditions").make_condition
37+
local in_bullets_cond = make_condition(tex.in_bullets)
38+
local line_begin = require("luasnip.extras.conditions.expand").line_begin
3639

3740
M = {
3841
s({ trig='beg', name='begin/end', dscr='begin/end environment (generic)'},
@@ -79,6 +82,18 @@ M = {
7982
}
8083
),
8184
{ condition = tex.in_text, show_condition = tex.in_text }),
85+
86+
-- generate new bullet points
87+
autosnippet({ trig = "--", hidden = true }, { t("\\item") },
88+
{ condition = in_bullets_cond * line_begin, show_condition = in_bullets_cond * line_begin }
89+
),
90+
autosnippet({ trig = "!-", name = "bullet point", dscr = "bullet point with custom text" },
91+
fmta([[
92+
\item [<>]<>
93+
]],
94+
{ i(1), i(0) }),
95+
{ condition = in_bullets_cond * line_begin, show_condition = in_bullets_cond * line_begin }
96+
),
8297
}
8398

8499
return M

lua/luasnip-latex-snippets/luasnippets/tex/math-commands.lua

Lines changed: 209 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,38 +35,83 @@ local autosnippet = ls.extend_decorator.apply(s, { snippetType = "autosnippet" }
3535
local tex = require("luasnip-latex-snippets.luasnippets.tex.utils.conditions")
3636
local auto_backslash_snippet = require("luasnip-latex-snippets.luasnippets.tex.utils.scaffolding").auto_backslash_snippet
3737
local symbol_snippet = require("luasnip-latex-snippets.luasnippets.tex.utils.scaffolding").symbol_snippet
38+
local single_command_snippet = require("luasnip-latex-snippets.luasnippets.tex.utils.scaffolding").single_command_snippet
3839

3940
M = {
41+
-- superscripts
42+
autosnippet({ trig = "sr", wordTrig = false },
43+
{ t("^2") },
44+
{ condition = tex.in_math, show_condition = tex.in_math }),
45+
autosnippet({ trig = "cb", wordTrig = false },
46+
{ t("^3") },
47+
{ condition = tex.in_math, show_condition = tex.in_math }),
48+
autosnippet({ trig = "compl", wordTrig = false },
49+
{ t("^{c}") },
50+
{ condition = tex.in_math, show_condition = tex.in_math }),
51+
autosnippet({ trig = "vtr", wordTrig = false },
52+
{ t("^{T}") },
53+
{ condition = tex.in_math, show_condition = tex.in_math }),
54+
autosnippet({ trig = "inv", wordTrig = false },
55+
{ t("^{-1}") },
56+
{ condition = tex.in_math, show_condition = tex.in_math }),
57+
4058
autosnippet({ trig = "lim", name = "lim(sup|inf)", dscr = "lim(sup|inf)" },
4159
fmta([[
4260
\lim<><><>
4361
]],
4462
{c(1, { t(""), t("sup"), t("inf") }),
4563
c(2, { t(""), fmta([[_{<> \to <>}]], { i(1, "n"), i(2, "\\infty") }) }),
46-
i(0)}
47-
),
64+
i(0)}),
4865
{ condition = tex.in_math, show_condition = tex.in_math }),
66+
4967
autosnippet({ trig = "sum", name = "summation", dscr = "summation" },
5068
fmta([[
5169
\sum<> <>
5270
]],
5371
{ c(1, {fmta([[_{<>}^{<>}]], {i(1, "i = 0"), i(2, "\\infty")}), t("")}), i(0) }),
54-
{ condition = tex.in_math, show_condition = tex.in_math }
55-
),
72+
{ condition = tex.in_math, show_condition = tex.in_math }),
73+
5674
autosnippet({ trig = "prod", name = "product", dscr = "product" },
5775
fmta([[
5876
\prod<> <>
5977
]],
6078
{ c(1, {fmta([[_{<>}^{<>}]], {i(1, "i = 0"), i(2, "\\infty")}), t("")}), i(0) }),
61-
{ condition = tex.in_math, show_condition = tex.in_math }
62-
),
79+
{ condition = tex.in_math, show_condition = tex.in_math }),
80+
6381
autosnippet({ trig = "cprod", name = "coproduct", dscr = "coproduct" },
6482
fmta([[
6583
\coprod<> <>
6684
]],
6785
{ c(1, {fmta([[_{<>}^{<>}]], {i(1, "i = 0"), i(2, "\\infty")}), t("")}), i(0) }),
68-
{ condition = tex.in_math, show_condition = tex.in_math }
69-
),
86+
{ condition = tex.in_math, show_condition = tex.in_math }),
87+
88+
autosnippet({ trig = "set", name = "set", dscr = "set" }, -- overload with set builder notation
89+
fmta([[
90+
\{<>\}<>
91+
]],
92+
{ c(1, { r(1, ""), sn(nil, { r(1, ""), t(" \\mid "), i(2) }) }), i(0) }),
93+
{ condition = tex.in_math, show_condition = tex.in_math }),
94+
95+
autosnippet({ trig = "nnn", name = "bigcap", dscr = "bigcap" },
96+
fmta([[
97+
\bigcap<> <>
98+
]],
99+
{ c(1, {fmta([[_{<>}^{<>}]], {i(1, "i = 0"), i(2, "\\infty")}), t("")}), i(0) }),
100+
{ condition = tex.in_math, show_condition = tex.in_math }),
101+
102+
autosnippet({ trig = "uuu", name = "bigcup", dscr = "bigcup" },
103+
fmta([[
104+
\bigcup<> <>
105+
]],
106+
{ c(1, {fmta([[_{<>}^{<>}]], {i(1, "i = 0"), i(2, "\\infty")}), t("")}), i(0) }),
107+
{ condition = tex.in_math, show_condition = tex.in_math }),
108+
109+
autosnippet({ trig = "bnc", name = "binomial", dscr = "binomial (nCR)" },
110+
fmta([[
111+
\binom{<>}{<>}<>
112+
]],
113+
{ i(1), i(2), i(0) }),
114+
{ condition = tex.in_math, show_condition = tex.in_math }),
70115
}
71116

72117
-- Auto backslashes
@@ -137,5 +182,161 @@ for k, v in pairs(greek_specs) do
137182
end
138183
vim.list_extend(M, greek_snippets)
139184

185+
local symbol_specs = {
186+
-- operators
187+
["!="] = { context = { name = "!=" }, command = [[\neq]] },
188+
["<="] = { context = { name = "" }, command = [[\leq]] },
189+
[">="] = { context = { name = "" }, command = [[\geq]] },
190+
["<<"] = { context = { name = "<<" }, command = [[\ll]] },
191+
[">>"] = { context = { name = ">>" }, command = [[\gg]] },
192+
["~~"] = { context = { name = "~" }, command = [[\sim]] },
193+
["~="] = { context = { name = "" }, command = [[\approx]] },
194+
["~-"] = { context = { name = "" }, command = [[\simeq]] },
195+
["-~"] = { context = { name = "" }, command = [[\backsimeq]] },
196+
["-="] = { context = { name = "" }, command = [[\equiv]] },
197+
["=~"] = { context = { name = "" }, command = [[\cong]] },
198+
[":="] = { context = { name = "" }, command = [[\definedas]] },
199+
["**"] = { context = { name = "·", priority = 100 }, command = [[\cdot]] },
200+
xx = { context = { name = "×" }, command = [[\times]] },
201+
["!+"] = { context = { name = "" }, command = [[\oplus]] },
202+
["!*"] = { context = { name = "" }, command = [[\otimes]] },
203+
-- sets
204+
NN = { context = { name = "" }, command = [[\mathbb{N}]] },
205+
ZZ = { context = { name = "" }, command = [[\mathbb{Z}]] },
206+
QQ = { context = { name = "" }, command = [[\mathbb{Q}]] },
207+
RR = { context = { name = "" }, command = [[\mathbb{R}]] },
208+
CC = { context = { name = "" }, command = [[\mathbb{C}]] },
209+
OO = { context = { name = "" }, command = [[\emptyset]] },
210+
pwr = { context = { name = "P" }, command = [[\powerset]] },
211+
cc = { context = { name = "" }, command = [[\subset]] },
212+
cq = { context = { name = "" }, command = [[\subseteq]] },
213+
qq = { context = { name = "" }, command = [[\supset]] },
214+
qc = { context = { name = "" }, command = [[\supseteq]] },
215+
["\\\\\\"] = { context = { name = "" }, command = [[\setminus]] },
216+
Nn = { context = { name = "" }, command = [[\cap]] },
217+
UU = { context = { name = "" }, command = [[\cup]] },
218+
["::"] = { context = { name = ":" }, command = [[\colon]] },
219+
-- quantifiers and logic stuffs
220+
AA = { context = { name = "" }, command = [[\forall]] },
221+
EE = { context = { name = "" }, command = [[\exists]] },
222+
inn = { context = { name = "" }, command = [[\in]] },
223+
notin = { context = { name = "" }, command = [[\not\in]] },
224+
["!-"] = { context = { name = "¬" }, command = [[\lnot]] },
225+
VV = { context = { name = "" }, command = [[\lor]] },
226+
WW = { context = { name = "" }, command = [[\land]] },
227+
["!W"] = { context = { name = "" }, command = [[\bigwedge]] },
228+
["=>"] = { context = { name = "" }, command = [[\implies]] },
229+
["=<"] = { context = { name = "" }, command = [[\impliedby]] },
230+
iff = { context = { name = "" }, command = [[\iff]] },
231+
["->"] = { context = { name = "", priority = 250 }, command = [[\to]] },
232+
["!>"] = { context = { name = "" }, command = [[\mapsto]] },
233+
["<-"] = { context = { name = "", priority = 250}, command = [[\gets]] },
234+
-- differentials
235+
dp = { context = { name = "" }, command = [[\partial]] },
236+
-- arrows
237+
["-->"] = { context = { name = "", priority = 500 }, command = [[\longrightarrow]] },
238+
["<->"] = { context = { name = "", priority = 500 }, command = [[\leftrightarrow]] },
239+
["2>"] = { context = { name = "", priority = 400 }, command = [[\rightrightarrows]] },
240+
upar = { context = { name = "" }, command = [[\uparrow]] },
241+
dnar = { context = { name = "" }, command = [[\downarrow]] },
242+
-- etc
243+
ooo = { context = { name = "" }, command = [[\infty]] },
244+
lll = { context = { name = "" }, command = [[\ell]] },
245+
dag = { context = { name = "" }, command = [[\dagger]] },
246+
["+-"] = { context = { name = "" }, command = [[\pm]] },
247+
["-+"] = { context = { name = "" }, command = [[\mp]] },
248+
}
249+
250+
local symbol_snippets = {}
251+
for k, v in pairs(symbol_specs) do
252+
table.insert(
253+
symbol_snippets,
254+
symbol_snippet(vim.tbl_deep_extend("keep", { trig = k }, v.context), v.command, { condition = tex.in_math })
255+
)
256+
end
257+
vim.list_extend(M, symbol_snippets)
258+
259+
local single_command_math_specs = {
260+
tt = {
261+
context = {
262+
name = "text (math)",
263+
dscr = "text in math mode",
264+
},
265+
command = [[\text]],
266+
},
267+
sbf = {
268+
context = {
269+
name = "symbf",
270+
dscr = "bold math text",
271+
},
272+
command = [[\symbf]],
273+
},
274+
syi = {
275+
context = {
276+
name = "symit",
277+
dscr = "italic math text",
278+
},
279+
command = [[\symit]],
280+
},
281+
udd = {
282+
context = {
283+
name = "underline (math)",
284+
dscr = "underlined text in math mode",
285+
},
286+
command = [[\underline]],
287+
},
288+
conj = {
289+
context = {
290+
name = "conjugate",
291+
dscr = "conjugate (overline)",
292+
},
293+
command = [[\overline]],
294+
},
295+
["__"] = {
296+
context = {
297+
name = "subscript",
298+
dscr = "auto subscript 3",
299+
wordTrig = false,
300+
},
301+
command = [[_]],
302+
},
303+
td = {
304+
context = {
305+
name = "superscript",
306+
dscr = "auto superscript alt",
307+
wordTrig = false,
308+
},
309+
command = [[^]],
310+
},
311+
sbt = {
312+
context = {
313+
name = "substack",
314+
dscr = "substack for sums/products",
315+
},
316+
command = [[\substack]],
317+
},
318+
sq = {
319+
context = {
320+
name = "sqrt",
321+
dscr = "sqrt",
322+
},
323+
command = [[\sqrt]],
324+
ext = { choice = true },
325+
},
326+
}
327+
328+
local single_command_math_snippets = {}
329+
for k, v in pairs(single_command_math_specs) do
330+
table.insert(
331+
single_command_math_snippets,
332+
single_command_snippet(
333+
vim.tbl_deep_extend("keep", { trig = k, snippetType = "autosnippet" }, v.context),
334+
v.command,
335+
{ condition = tex.in_math },
336+
v.ext or {}
337+
)
338+
)
339+
end
340+
vim.list_extend(M, single_command_math_snippets)
140341

141342
return M

lua/luasnip-latex-snippets/luasnippets/tex/math.lua

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,44 +83,50 @@ M = {
8383
fmta([[
8484
$<>$<>
8585
]],
86-
{ i(1), i(0) })
87-
),
86+
{ i(1), i(0) })),
87+
8888
autosnippet({ trig = "dm", name = "\\[...\\]", dscr = "display math" },
8989
fmta([[
9090
\[
9191
<>
9292
.\]
9393
<>]],
9494
{ i(1), i(0) }),
95-
{ condition = line_begin, show_condition = line_begin }
96-
),
95+
{ condition = line_begin, show_condition = line_begin }),
96+
9797
autosnippet({ trig = "ali", name = "align(|*|ed)", dscr = "align math" },
9898
fmta([[
9999
\begin{align<>}
100100
<>
101101
.\end{align<>}
102102
]],
103103
{ c(1, {t("*"), t(""), t("ed")}), i(2), rep(1) }), -- in order of least-most used
104-
{ condition = line_begin, show_condition = line_begin }
105-
),
104+
{ condition = line_begin, show_condition = line_begin }),
105+
106+
autosnippet({ trig='==', name='&= align', dscr='&= align'},
107+
fmta([[
108+
&<> <> \\
109+
]],
110+
{ c(1, {t("="), t("\\leq"), i(1)}), i(2) }
111+
), { condition = tex.in_align, show_condition = tex.in_align }),
112+
106113
autosnippet({ trig = "gat", name = "gather(|*|ed)", dscr = "gather math" },
107114
fmta([[
108115
\begin{gather<>}
109116
<>
110117
.\end{gather<>}
111118
]],
112119
{ c(1, {t("*"), t(""), t("ed")}), i(2), rep(1) }),
113-
{ condition = line_begin, show_condition = line_begin }
114-
),
120+
{ condition = line_begin, show_condition = line_begin }),
121+
115122
autosnippet({ trig = "eqn", name = "equation(|*)", dscr = "equation math" },
116123
fmta([[
117124
\begin{equation<>}
118125
<>
119126
.\end{equation<>}
120127
]],
121128
{ c(1, {t("*"), t("")}), i(2), rep(1) }),
122-
{ condition = line_begin, show_condition = line_begin }
123-
),
129+
{ condition = line_begin, show_condition = line_begin }),
124130

125131
-- Matrices and Cases
126132
s({trig = "([bBpvV])mat(%d+)x(%d+)([ar])", name = "[bBpvV]matrix", dscr = "matrices", regTrig = true, hidden = true},
@@ -143,8 +149,7 @@ M = {
143149
return snip.captures[1] .. "matrix"
144150
end)
145151
}),
146-
{ condition = tex.in_math, show_condition = tex.in_math }
147-
),
152+
{ condition = tex.in_math, show_condition = tex.in_math }),
148153

149154
autosnippet({ trig = "(%d?)cases", name = "cases", dscr = "cases", regTrig = true, hidden = true },
150155
fmta([[
@@ -153,8 +158,7 @@ M = {
153158
.\end{cases}
154159
]],
155160
{ d(1, generate_cases) }),
156-
{ condition = tex.in_math, show_condition = tex.in_math }
157-
),
161+
{ condition = tex.in_math, show_condition = tex.in_math }),
158162
}
159163

160164
return M

0 commit comments

Comments
 (0)