├── README.md ├── hsnips └── latex.hsnips └── vscode ├── keybindings.json └── latex.json /README.md: -------------------------------------------------------------------------------- 1 | # Latex Snippets for Visual Studio Code 2 | 3 | Common snippets to write faster LaTeX 4 | 5 | ## Installing 6 | 7 | * Install the [HyperSnips](https://github.com/draivin/hsnips) extension. 8 | * Open the HyperSnips directory (Ctrl + Shift + P, "HyperSnips: Open Snippets Directory") and copy there the file `hsnips/latex.hsnips` 9 | * From the VSCode menu (Ctrl + Shift + P) go to "Preferences: Configure User Snippets" and select "latex". Copy there the content of `vscode/latex.json` 10 | * Set keybindings with Ctrl + Shift + P, and "Preferences: Open Keyboard Shortcuts (JSON)". Copy there the content of `vscode/keybindings.json` 11 | * You may also install the `LaTeX Workshop` extension, but some snippets may conflict. You can disable the snippets from that extension thanks to another extension: `Control Snippets`. 12 | 13 | ### Usage 14 | 15 | Simply open a `.tex` file and start typing. All "static" snippets are contained in `latex.json`, and are expanded by typing their "prefix" and pressing `Tab` (or `Enter`, if they appear in the IntelliSense menu). You can navigate between fixed placeholders with `Alt + LeftArrow` and `Alt + RightArrow`. 16 | 17 | There are also "dynamic" snippets, that automatically expands after typing a certain trigger. For example: 18 | * `mk` expands in `$ $ `. Note that the final space is automatically removed if you type certain characters (e.g. `-?:,`) after the last `$`. 19 | * An alphabetical character repeated two times is automatically converted to the corresponding greek letter: e.g. `aa` becomes `\alpha`. This does not conflict with words with repeated letters (e.g. `letters`) or with other commands (e.g. `\ddot`). 20 | * Type `M3x3` and a space to obtain a 0-filled 3x3 matrix. Dimensions can be changed dynamically: `M2x6` works too. If you type a `d` instead of the final space, the resulting matrix will have navigable placeholders only on the diagonal. 21 | * Type `dfdx` and a space to write a partial derivative. Use a capital `D` at the start if you want the usual derivative. 22 | * Vectors can be set by typing a character (or word) followed by `.,` or `,.` (just press the two keys at the same time, as their order does not matter). Similar commands automatically convert strings like `p-hat`, `pbar`, `Pcal` (to `\mathcal{P}`). For `\mathbb{P}` use `P#`. 23 | * Many more! Look at `latex.hsnips` for all the implementations. -------------------------------------------------------------------------------- /hsnips/latex.hsnips: -------------------------------------------------------------------------------- 1 | snippet dategreeting "Gives you the current date!" 2 | Hello from your hsnip at ``rv = new Date().toDateString()``! 3 | endsnippet 4 | 5 | snippet box "Box" A 6 | ``rv = '┌' + '─'.repeat(t[0].length + 2) + '┐'`` 7 | │ $1 │ 8 | ``rv = '└' + '─'.repeat(t[0].length + 2) + '┘'`` 9 | endsnippet 10 | 11 | snippet // "Fraction simple" A 12 | \frac{$1}{$2}$0 13 | endsnippet 14 | 15 | snippet `((\d+)|(\d*)(\\)?([A-Za-z]+)((\^|_)(\{\d+\}|\d))*)/` "Fraction no ()" A 16 | \frac{``rv = m[1]``}{$1}$0 17 | endsnippet 18 | 19 | snippet `^.*\)/` "Fraction with ()" A 20 | `` 21 | let str = m[0]; 22 | str = str.slice(0, -1); 23 | let lastIndex = str.length - 1; 24 | 25 | let depth = 0; 26 | let i = str.length - 1; 27 | 28 | while (true) { 29 | if (str[i] == ')') depth += 1; 30 | if (str[i] == '(') depth -= 1; 31 | if (depth == 0) break; 32 | i -= 1; 33 | } 34 | 35 | let results = str.slice(0, i) + "\\frac{" + str.slice(i+1, -1) + "}"; 36 | results += "{$1}$0"; 37 | rv = results; 38 | `` 39 | endsnippet 40 | 41 | snippet `M([1-9])x([1-9])([ d])` "matrix" A 42 | ``rv = '\\left(\\begin{array}{' + 'c'.repeat(m[2]) + '}'`` 43 | ``count = 1; 44 | msg = ''; 45 | diag = (m[3] == 'd'); 46 | 47 | for (i=0; i < parseInt(m[1],10); i++) { 48 | diag && (i!=0) ? msg += '0' : msg += '${' + count + ':0}'; 49 | count++; 50 | for (j=1; j < parseInt(m[2],10); j++) { 51 | diag && (i!=j) ? msg += ' & 0' : msg += ' & ${' + count + ':0}'; 52 | count++; 53 | } 54 | if (i != parseInt(m[1],10)-1) { 55 | msg += ' \\\\\\ \n'; 56 | } 57 | } 58 | rv = msg;`` 59 | \\end{array}\\right) 60 | endsnippet 61 | 62 | snippet `vec([1-9])` "column vector" A 63 | ``rv = '\\begin{pmatrix}'`` 64 | ``count=2; 65 | msg = '${1:0}'; 66 | for (i = 1; i < parseInt(m[1],10); i++) { 67 | msg += ' \\\\\\ \n${' + count + ':0}'; 68 | count++; 69 | } 70 | rv = msg; 71 | `` 72 | \\end{pmatrix} 73 | endsnippet 74 | 75 | snippet `([a-zA-Z])([0-9]+) ` "underset" A 76 | ``if ( m[2].length == 1 ) { a = '_'; b = ' '; } 77 | else { a = '_{'; b = '} '; } 78 | rv = m[1] + a + m[2] + b`` 79 | endsnippet 80 | 81 | snippet `(\\?\w+)(,\.|\.,)` "vector" A 82 | ``rv = '\\vec{' + m[1] + '}'`` 83 | endsnippet 84 | 85 | snippet `\b([a-zA-Z])bar\b` "bar" A 86 | ``rv = '\\bar{' + m[1] + '}'`` 87 | endsnippet 88 | 89 | snippet `\b([a-zA-Z])-hat\b` "hat" A 90 | ``rv = '\\hat{' + m[1] + '}'`` 91 | endsnippet 92 | 93 | snippet `\b([a-zA-Z])cal\b` "cal" A 94 | ``rv = '\\mathcal{' + m[1] + '}'`` 95 | endsnippet 96 | 97 | snippet `([A-Z])#` "bb" A 98 | ``rv = '\\mathbb{' + m[1] + '}'`` 99 | endsnippet 100 | 101 | snippet mk "inline math" A 102 | \$$1\$``if (t[1] && ",-.?: )]".indexOf(t[1][0]) >= 0) { rv = '' } else { rv = ' ' }``$2 103 | endsnippet 104 | 105 | snippet `(? "to" A 111 | ``rv = '\\to'`` 112 | endsnippet 113 | 114 | snippet !> "mapsto" A 115 | ``rv = '\\mapsto'`` 116 | endsnippet 117 | 118 | snippet @@ "infinity" A 119 | ``rv = '\\infty'`` 120 | endsnippet 121 | 122 | snippet `([a-zA-Z0-9])td` "exponentiation" A 123 | ``rv = m[1] + '^{$1}'`` 124 | endsnippet 125 | 126 | snippet `\b(d|D)(\\?\w+)[dD](\w{1}|\\\w+) ` "derivatives" A 127 | `` 128 | if (m[1] == 'd') 129 | rv = '\\frac{\\partial ' + m[2] + '}{\\partial ' + m[3] + '}'; 130 | else 131 | rv = '\\frac{\\mathrm{d' + m[2] + '}}{\\mathrm{d' + m[3] + '}}'; 132 | `` 133 | endsnippet 134 | 135 | snippet `=>` "implies" A 136 | `` rv = '\\Rightarrow' `` 137 | endsnippet 138 | 139 | snippet `=<` "implied by" A 140 | ``rv = '\\Leftarrow'`` 141 | endsnippet 142 | 143 | snippet `==` "equals" A 144 | ``rv = '&= $1 \\\\\\'`` 145 | endsnippet 146 | 147 | snippet `<=` "leq" A 148 | ``rv = '\\leq'`` 149 | endsnippet 150 | 151 | snippet `>=` "geq" A 152 | ``rv = '\\geq'`` 153 | endsnippet 154 | 155 | snippet xnn "x_n" 156 | ``rv = 'x_n' `` 157 | endsnippet 158 | 159 | snippet `([\\?a-zA-Z]+)->([\\?a-zA-Z]+) ` A 160 | ``rv = '$1 \\xrightarrow[' + m[1] + ' \\to ' + m[2].replace('inf', '\\infty') + ']{} $2'`` 161 | endsnippet 162 | 163 | snippet `lim_([\\?a-zA-Z]+)_([\\?a-zA-Z]+) ` A 164 | ``rv = '\\lim_{' + m[1] + ' \\to ' + m[2].replace('inf', '\\infty') + '} '`` 165 | endsnippet 166 | -------------------------------------------------------------------------------- /vscode/keybindings.json: -------------------------------------------------------------------------------- 1 | // Place your key bindings in this file to override the defaultsauto[] 2 | [ 3 | { 4 | "key": "alt+j", 5 | "command": "editor.action.insertSnippet", 6 | "when": "editorTextFocus", 7 | "args": { 8 | "langId": "latex", 9 | "name": "fraction" 10 | } 11 | }, 12 | { 13 | "key": "ctrl+b", 14 | "command": "editor.action.insertSnippet", 15 | "when": "editorTextFocus", 16 | "args": { 17 | "langId": "latex", 18 | "name": "bold" 19 | } 20 | }, 21 | { 22 | "key": "ctrl+i", 23 | "command": "editor.action.insertSnippet", 24 | "when": "editorTextFocus", 25 | "args": { 26 | "langId": "latex", 27 | "name": "italic" 28 | } 29 | }, 30 | { 31 | "key": "ctrl+shift+i", 32 | "command": "editor.action.insertSnippet", 33 | "when": "editorTextFocus", 34 | "args": { 35 | "langId": "latex", 36 | "name": "itemize" 37 | } 38 | }, 39 | { 40 | "key": "ctrl+shift+e", 41 | "command": "editor.action.insertSnippet", 42 | "when": "editorTextFocus", 43 | "args": { 44 | "langId": "latex", 45 | "name": "enumerate" 46 | } 47 | }, 48 | { 49 | "key": "ctrl+shift+u", 50 | "command": "editor.action.insertSnippet", 51 | "when": "editorTextFocus", 52 | "args": { 53 | "langId": "latex", 54 | "name": "underbrace" 55 | } 56 | }, 57 | { 58 | "key": "ctrl+shift+o", 59 | "command": "editor.action.insertSnippet", 60 | "when": "editorTextFocus", 61 | "args": { 62 | "langId": "latex", 63 | "name": "overbrace" 64 | } 65 | }, 66 | { 67 | "key": "ctrl+alt+u", 68 | "command": "editor.action.insertSnippet", 69 | "when": "editorTextFocus", 70 | "args": { 71 | "langId": "latex", 72 | "name": "underset" 73 | } 74 | }, 75 | { 76 | "key": "ctrl+alt+o", 77 | "command": "editor.action.insertSnippet", 78 | "when": "editorTextFocus", 79 | "args": { 80 | "langId": "latex", 81 | "name": "overset" 82 | } 83 | }, 84 | { 85 | "key": "alt+right", 86 | "command": "-workbench.action.terminal.focusNextPane", 87 | "when": "terminalFocus" 88 | }, 89 | { 90 | "key": "alt+right", 91 | "command": "jumpToNextSnippetPlaceholder", 92 | "when": "editorTextFocus && hasNextTabstop && inSnippetMode" 93 | }, 94 | { 95 | "key": "tab", 96 | "command": "-jumpToNextSnippetPlaceholder", 97 | "when": "editorTextFocus && hasNextTabstop && inSnippetMode" 98 | }, 99 | { 100 | "key": "alt+left", 101 | "command": "jumpToPrevSnippetPlaceholder", 102 | "when": "editorTextFocus && hasPrevTabstop && inSnippetMode" 103 | }, 104 | { 105 | "key": "shift+tab", 106 | "command": "-jumpToPrevSnippetPlaceholder", 107 | "when": "editorTextFocus && hasPrevTabstop && inSnippetMode" 108 | }, 109 | { 110 | "key": "alt+left", 111 | "command": "-workbench.action.terminal.focusPreviousPane", 112 | "when": "terminalFocus" 113 | }, 114 | { 115 | "key": "alt+right", 116 | "command": "hsnips.nextPlaceholder", 117 | "when": "editorTextFocus && hasNextTabstop && inSnippetMode" 118 | }, 119 | { 120 | "key": "tab", 121 | "command": "-hsnips.nextPlaceholder", 122 | "when": "editorTextFocus && hasNextTabstop && inSnippetMode" 123 | }, 124 | { 125 | "key": "alt+left", 126 | "command": "hsnips.prevPlaceholder", 127 | "when": "editorTextFocus && hasPrevTabstop && inSnippetMode" 128 | }, 129 | { 130 | "key": "alt+b", 131 | "command": "workbench.action.toggleSidebarVisibility" 132 | }, 133 | { 134 | "key": "ctrl+b", 135 | "command": "-workbench.action.toggleSidebarVisibility" 136 | }, 137 | { 138 | "key": "ctrl+shift+e", 139 | "command": "-workbench.view.explorer" 140 | }, 141 | { 142 | "key": "ctrl+shift+u", 143 | "command": "-workbench.action.output.toggleOutput" 144 | }, 145 | { 146 | "key": "ctrl+shift+o", 147 | "command": "-workbench.action.gotoSymbol" 148 | } 149 | ] 150 | 151 | -------------------------------------------------------------------------------- /vscode/latex.json: -------------------------------------------------------------------------------- 1 | { 2 | // Place your snippets for latex here. Each snippet is defined under a snippet name and has a prefix, body and 3 | // description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are: 4 | // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 5 | // same ids are connected. 6 | // Example: 7 | // "Print to console": { 8 | // "prefix": "log", 9 | // "body": [ 10 | // "console.log('$1');", 11 | // "$2" 12 | // ], 13 | // "description": "Log output to console" 14 | // } 15 | "Align*": { 16 | "prefix": "al", 17 | "body": [ 18 | "\\begin{align*}", 19 | "\t$0", 20 | "\\end{align*}" 21 | ] 22 | }, 23 | "fraction": { 24 | "prefix": "f", 25 | "body": [ 26 | "\\frac{$1}{$2} $0" 27 | ] 28 | }, 29 | "bold": { 30 | "prefix": "bold", 31 | "body": [ 32 | "\\textbf{${1:${TM_SELECTED_TEXT}}} $0" 33 | ] 34 | }, 35 | "italic": { 36 | "prefix": "italic", 37 | "body": [ 38 | "\\textit{${1:${TM_SELECTED_TEXT}}} $0" 39 | ] 40 | }, 41 | "underbrace": { 42 | "prefix": "underbrace", 43 | "body": [ 44 | "\\underbrace{${1:${TM_SELECTED_TEXT}}}_{$2} $0" 45 | ] 46 | }, 47 | "overbrace": { 48 | "prefix": "overbrace", 49 | "body": [ 50 | "\\overbrace{${1:${TM_SELECTED_TEXT}}}_{$2} $0" 51 | ] 52 | }, 53 | "underset": { 54 | "prefix": "underset", 55 | "body": [ 56 | "\\underset{$1}{${2:${TM_SELECTED_TEXT}}} $0" 57 | ] 58 | }, 59 | "overset": { 60 | "prefix": "overset", 61 | "body": [ 62 | "\\overset{$1}{${2:${TM_SELECTED_TEXT}}} $0" 63 | ] 64 | }, 65 | "thm": { 66 | "prefix": "thm", 67 | "body": [ 68 | "\\begin{thm} $1 \\end{thm} $0" 69 | ] 70 | }, 71 | "itemize": { 72 | "prefix": "itemize", 73 | "body": [ 74 | "\\begin{itemize}", 75 | "\t\\item $1", 76 | "\\end{itemize}", 77 | "$0" 78 | ] 79 | }, 80 | "enumerate": { 81 | "prefix": "enum", 82 | "body": [ 83 | "\\begin{enumerate}", 84 | "\t\\item $1", 85 | "\\end{enumerate}", 86 | "$0" 87 | ] 88 | }, 89 | "Environment": { 90 | "prefix": "beg", 91 | "body": [ 92 | "\\begin{${1:name}}", 93 | "\t$0", 94 | "\\end{${1:name}}" 95 | ] 96 | }, 97 | "Text": { 98 | "prefix": "txt", 99 | "body": [ 100 | "\\text{$0}" 101 | ] 102 | }, 103 | "Image": { 104 | "prefix": "img", 105 | "body": [ 106 | "\\begin{figure}[H]", 107 | "\t\\centering", 108 | "\t\\includegraphics[width=0.5\\textwidth]{${1:${TM_SELECTED_TEXT}}}", 109 | "\t\\caption{${2:caption}\\label{fig:${3:sample}}}", 110 | "\\end{figure}", 111 | "$0" 112 | ] 113 | }, 114 | "Table": { 115 | "prefix": "tab", 116 | "body": [ 117 | "\\begin{table}[H]", 118 | "\t\\centering", 119 | "\t\\begin{tabular}{${1:ccc}}", 120 | "\t\t\\toprule", 121 | "\t\ta & b & c \\\\\\ \\midrule", 122 | "\t\t1 & 2 & 3 \\\\\\", 123 | "\t\t\\bottomrule", 124 | "\t\\end{tabular}", 125 | "\t\\caption{${2:caption}\\label{tab:${3:sample}}}", 126 | "\\end{table}", 127 | "$0" 128 | ] 129 | }, 130 | "pmatrix": { 131 | "prefix": "pm", 132 | "body": [ 133 | "\\begin{pmatrix}", 134 | "$1", 135 | "\\end{pmatrix}", 136 | "$0" 137 | ] 138 | }, 139 | "Integral": { 140 | "prefix": "int", 141 | "body": [ 142 | "\\int_{${1:0}}^{${2:\\infty}} $0" 143 | ] 144 | }, 145 | "Sum Full": { 146 | "prefix": "sumi", 147 | "body": [ 148 | "\\sum_{${1:i}=${2:0}}^{${3:\\infty}} $0" 149 | ] 150 | }, 151 | "undersetinline": { 152 | "prefix": "_", 153 | "body": [ 154 | "_{$1}$0" 155 | ] 156 | }, 157 | "parenthesis1": { 158 | "prefix": "(", 159 | "body": [ 160 | "\\left($1\\right" 161 | ] 162 | }, 163 | "parenthesis2": { 164 | "prefix": "[", 165 | "body": [ 166 | "\\left[$1\\right" 167 | ] 168 | }, 169 | "parenthesis3": { 170 | "prefix": "{", 171 | "body": [ 172 | "\\left{$1\\right" 173 | ] 174 | }, 175 | "average": { 176 | "prefix": "avg", 177 | "body": [ 178 | "\\langle $1 \\rangle$0" 179 | ] 180 | }, 181 | "Sum": { 182 | "prefix": "sum", 183 | "body": [ 184 | "\\sum_{${1:i=0}}^{${2:\\infty}} $0" 185 | ] 186 | }, 187 | "hbar": { 188 | "prefix": "hb", 189 | "body": [ 190 | "\\hbar" 191 | ] 192 | }, 193 | "section": { 194 | "prefix": "sec", 195 | "body": [ 196 | "\\section{$1}", 197 | "$0" 198 | ] 199 | }, 200 | "subsection": { 201 | "prefix": "ssec", 202 | "body": [ 203 | "\\subsection{$1}", 204 | "$0" 205 | ] 206 | }, 207 | "operatorname": { 208 | "prefix": "op", 209 | "body": [ 210 | "\\operatorname{$1} $0", 211 | ] 212 | }, 213 | "mathrm": { 214 | "prefix": "rm", 215 | "body": [ 216 | "\\mathrm{$1} $0" 217 | ] 218 | } 219 | } --------------------------------------------------------------------------------