commit 439290778e595eb2ee0e06fd76ff59d685ead7ca
parent 9c6f43ca3f29c821aff2f0963915d5d603319cd0
Author: Alex Balgavy <a.balgavy@gmail.com>
Date: Thu, 4 Apr 2019 19:18:16 +0200
Latex snippets
Former-commit-id: 11006656a678da1f9af1048040e01aea1affbfba
Diffstat:
1 file changed, 124 insertions(+), 5 deletions(-)
diff --git a/vim/ultisnips/tex.snippets b/vim/ultisnips/tex.snippets
@@ -1,9 +1,70 @@
+# vim: foldmethod=marker foldlevel=0 expandtab softtabstop=2 shiftwidth=2:
+# Define math context {{{
+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
+# }}}
+
+# Skeletons {{{
+snippet skelmath "Math skeleton" bA
+\documentclass[12pt,a4paper,oneside,fleqn]{article}
+\usepackage{amsmath}
+\usepackage{amssymb}
+\begin{document}
+ $1
+\end{document}
+endsnippet
+
+snippet front "Front matter" b
+\title{$1}
+\author{$2}
+endsnippet
+# }}}
+
+# Environments {{{
+snippet ul "unordered list" b
+\begin{itemize}
+ \item $1
+ $2
+\end{itemize}
+endsnippet
+
+snippet - "item in list" b
+\item $1
+ $2
+endsnippet
+
snippet beg "begin{} / end{}" bA
\begin{$1}
- $0
+ $0
\end{$1}
endsnippet
+snippet sec "section{}" b
+\section{$1}
+$0
+endsnippet
+
snippet mk "Inline math" wA
$${1}$`!p
if t[2] and t[2][0] not in [',', '.', '?', '-', ' ']:
@@ -18,49 +79,107 @@ snippet dm "Displayed math" wA
$1
.\] $0
endsnippet
+# }}}
+
+# Word processing {{{
+snippet i "italic text" w
+\textit{$1}
+endsnippet
+snippet b "bold text" w
+\textbf{$1}
+endsnippet
+# }}}
+
+# Math symbols {{{
+context "math()"
snippet '([A-Za-z])(\d)' "auto subscript" wrA
`!p snip.rv = match.group(1)`_`!p snip.rv = match.group(2)`
endsnippet
-snippet '([A-Za-z])_(\d\d)' "auto subscript" wrA
+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 // "Fraction" iA
\\frac{$1}{$2}$0
endsnippet
+context "math()"
snippet '((\d+)|(\d*)(\\)?([A-Za-z]+)((\^|_)(\{\d+\}|\d))*)/' "Fraction" wrA
\\frac{`!p snip.rv = match.group(1)`}{$1}$0
endsnippet
-snippet title "Title and author"
-\title{$1}
-\author{$2}
+context "math()"
+priority 1000
+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
+context "math()"
snippet / "Fraction" iA
\\frac{${VISUAL}}{$1}$0
endsnippet
+context "math()"
priority 10
snippet "bar" "bar" riA
\overline{$1}$0
endsnippet
+context "math()"
priority 100
snippet "([a-zA-Z])bar" "bar" riA
\overline{`!p snip.rv=match.group(1)`}
endsnippet
+context "math()"
priority 10
snippet "hat" "hat" riA
\hat{$1}$0
endsnippet
+context "math()"
priority 100
snippet "([a-zA-Z])hat" "hat" riA
\hat{`!p snip.rv=match.group(1)`}
endsnippet
+context "math()"
+snippet _{ "underbrace"
+\underbrace{$1}_\text{$2}
+endsnippet
+
+context "math()"
+snippet ^{ "overbrace"
+\overbrace{$1}^\text{$2}
+endsnippet
+# }}}
+
+# Custom commands {{{
+snippet cmd "\given{A}{B} - (A|B)" b
+% \given{A}{B} ("A given B") %
+\makeatletter
+\newcommand{\@givenstar}[2]{\left(#1\;\middle|\;#2\right)}
+\newcommand{\@givennostar}[3][]{#1(#2\;#1|\;#3#1)}
+\newcommand{\given}{\@ifstar\@givenstar\@givennostar}
+\makeatother
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+endsnippet
+
+snippet cmd "another one" b
+this doesn't actually do anything
+endsnippet
+# }}}