8 | A way to integrate LaTeX, VS Code, and Inkscape in macOS.
9 |
10 |
11 | ## Table of Content
12 |
13 | - [Abstract](#abstract)
14 | - [Disclaimer](#disclaimer)
15 | - [Setup For Typing Blasting Fast](#setup-for-typing-blasting-fast)
16 | - [Tex Conceal](#tex-conceal)
17 | - [HyperSnips](#hypersnips)
18 | - [Sympy and Mathematica](#sympy-and-mathematica)
19 | - [Correcting Spelling Mistakes on the Fly](#correcting-spelling-mistakes-on-the-fly)
20 | - [Drawing Like a Pro - With Inkscape](#drawing-like-a-pro---with-inkscape)
21 | - [Inkscape Figure Manager](#inkscape-figure-manager)
22 | - [Inkscape Shortcut Manager](#inkscape-shortcut-manager)
23 | - [Reference Card for Key Chords](#reference-card-for-key-chords)
24 | - [Summary](#summary)
25 | - [Updates](#updates)
26 | - [~~About Inkscape Shortcut Manager (09.27.21)~~](#about-inkscape-shortcut-manager-092721)
27 | - [Quiver - For commutative diagram (01.24.22)](#quiver---for-commutative-diagram-012422)
28 | - [Migrate to HyperSnips (02.18.22)](#migrate-to-hypersnips-021822)
29 | - [Documenting Inkscape Shortcut Manager (07.30.22)](#documenting-inkscape-shortcut-manager-073022)
30 | - [Credits](#credits)
31 | - [Related Project](#related-project)
32 | - [Star History](#star-history)
33 |
34 | ## Abstract
35 |
36 | I use $\LaTeX$ heavily for both academic work and professional work, and I think I'm quite proficient in terms of typing things out in $\LaTeX$. But when I see the mind-blowing blog posts from **Gilles Castel (RIP)**-[How I'm able to take notes in mathematics lectures using LaTeX and Vim](https://castel.dev/post/lecture-notes-1/) and also [How I draw figures for my mathematical lecture notes using Inkscape](https://castel.dev/post/lecture-notes-2/), I realize that I'm still far from *fast*, so I decided to adapt the whole setup from Linux-Vim to macOS-VS Code.
37 |
38 | > This setup is universal for VS Code users indeed. The only part that'll be macOS-specific is the Inkscape part ([Inkscape-figures](#inkscape-figure-manager) and [Inkscape-shortcut-manager](#inkscape-shortcut-manager)). While the first part can be replaced by [super-figure](https://github.com/Joao-Peterson/super-figure) (while I still prefer my setup, you can still try it out even if you're in macOS), and you can certainly achieve a similar result in Windows as in my [Notes](./Notes), the drawing speed will be slower without the shortcut manager. Just keep that in mind.
39 |
40 | If you still don't know what to expect, please check out my [Notes](https://github.com/sleepymalc/Notes) taken in this setup. Also, due to the VS Code recent update (1.76.1), we have the [profile](https://code.visualstudio.com/docs/editor/profiles) functionality available. Specifically, this is [my current minimal profile](https://vscode.dev/profile/github/70b175ba903a4f1cc5dcd271ce8fcb51) for $\LaTeX$ I'm currently using, but since some configurations are not included in the [profile](https://code.visualstudio.com/docs/editor/profiles), you should still read through everything.
41 |
42 | > Available: [My website](https://pbb.wtf/posts/VSCode-LaTeX-Inkscape)
43 |
44 | ## Disclaimer
45 |
46 | Please look through the two blog posts above by Gilles Castel! They are incredible and worth spending your time to understand how all things work, and what's the motivation behind all these. I'm only mimicking his workflow, with a little patience to set up the whole thing in my environment. Show respect to the original author!
47 |
48 | Before we start anything serious, just copy the [`keybindings.json`](./VSCode-setting/keybindings.json) and [`settings.json`](./VSCode-setting/settings.json) into your own `keybindings.json` and `settings.json`. Don't worry, I'll explain what they do later.
49 |
50 |
51 |
52 |
53 |
54 | Also, create a snippet file for $\LaTeX$ in the following steps:
55 |
56 | 1. Press `shift`+`cmd`+`p` to open the VS Code command.
57 | 2. Type `snippets`, and choose `Snippets: Configure Snippets`.
58 | 3. Choose `New Global Snippets file...`.
59 | 4. Enter `latex` to create a new file.
60 | 5. Paste the [`latex.json`](./VSCode-setting/Snippets/latex.json) into that file.
61 |
62 |
63 |
64 | ## Setup For Typing Blasting Fast
65 |
66 | First thing first, please set up your [VS Code](https://code.visualstudio.com/) with $\LaTeX$ properly with [LaTeX Workshop](https://marketplace.visualstudio.com/items?itemName=James-Yu.latex-workshop), there are lots of tutorials online, just check them out and set them up properly. Basically, it can be done in the following steps:
67 |
68 | 1. Download [MacTex](https://www.tug.org/mactex/). This can be replaced by something more lightweight, but in my opinion, this doesn't really help much in terms of speed or wasting your disk. But if you want something like this, check out [TeXLive](https://www.tug.org/texlive/).
69 | 2. Download [LaTeX Workshop](https://marketplace.visualstudio.com/items?itemName=James-Yu.latex-workshop)
70 | 3. Copy-pasting the following configuration file into your `settings.json`
71 |
72 | ```JSON
73 | "latex-workshop.latex.autoBuild.run": "onSave"
74 | ```
75 |
76 | > This will save your time by compiling your $\LaTeX$ project whenever you save your file by `cmd`+`s`.
77 |
78 | Now, we go through things one by one following Gilles Castel's blog post.
79 |
80 | ### Tex Conceal
81 |
82 | To achieve a similar result as in Gilles Castel's setup, there is an extension called [vsc-conceal](https://github.com/Pancaek/vsc-conceal) for VS Code. All the setup is in the `setting.json`, and since this setup is quite straightforward, I'll just give a snapshot to show how it looks in practice.
83 |
84 |
85 |
86 |
87 |
88 | Note that I set the `"conceal.revealOn"` to `"active-line"`, which is why you will see the source code in line 51. There are other options you can choose, see the original repo for details.
89 |
90 | ### HyperSnips
91 |
92 | If you look around in the VS Code extension marketplace to find UltiSnips' equivalence, you probably will find [Vsnips](https://marketplace.visualstudio.com/items?itemName=corvofeng.Vsnips). But I'm not sure why this is the case, I can't figure out how to set it up properly. Hence, I find another alternative, which is [HyperSnips](https://marketplace.visualstudio.com/items?itemName=draivin.hsnips). Please first download [HyperSnips](https://marketplace.visualstudio.com/items?itemName=draivin.hsnips) and just follow the instructions, copy [`latex.hsnips`](./VSCode-setting/Snippets/latex.hsnips) into `$HOME/Library/Application Support/Code/User/globalStorage/draivin.hsnips/hsnips/`, and you're good to go!
93 |
94 | To modify this file, you can either go to this file in your finder or use the VS Code built-in command function. For commands function,
95 |
96 | 1. Press `shift+cmd+space` to type in some commands to VS Code.
97 | 2. Type `>HyperSnips: Open Snippet File`
98 | 3. Choose `latex.hsnips`
99 |
100 | After doing this, you're all set. But a big question is, what exactly is a snippet?
101 |
102 | #### Snippets
103 |
104 | A snippet is a short reusable piece of text that can be triggered by some other text. For example, when I type `dm` (stands for display math), the word `dm` will be expanded to a display math environment:
105 |
106 |
107 |
108 |
109 |
110 | If you are a math guy, you may need to type some inline math like `\(\)`, which is kind of painful. But with snippets, you can have
111 |
112 |
113 |
114 |
115 |
116 | See? You just type `fm` (not the best choice here, but since `im` is a common prefix, so can't really use that as our snippet 🥲), and then your snippet not only automatically types `\(\)` for you, but it also sends your cursor between `\(\)`! With this, you can type something **really** fast:
117 |
118 |
119 |
120 |
121 |
122 | Note that in the above demo, I use a very common snippet, `qs` for `^{2}`.
123 |
124 | As you can imagine, this can be quite complex. For example, you can even have something like this:
125 |
126 |
127 |
128 |
129 |
130 | or this:
131 |
132 |
133 |
134 |
135 |
136 | For the first snippet, I type `table2 5`, and then it generates a table with 2 rows and 5 columns. For the second one, I type `pmat` for matrix, and then type `2 5` to indicate that I want a 2 by 5 matrix, then boom! My snippets do that for me in an instant!
137 |
138 | My snippet file includes commonly used snippets as suggested in the original posts, you can look into it to better understand how it works. And maybe you can create your snippets also! Here are some useful snippets for you.
139 |
140 |
141 |
142 |
143 |
144 | #### Math Environment
145 |
146 | In the recent update of [HyperSnips](https://marketplace.visualstudio.com/items?itemName=draivin.hsnips), the *context* functionality is implemented, which is very useful, and you should understand how it works. If you look at the top of the snippet file, you will see
147 |
148 | ```javascript
149 | global
150 | function math(context) {
151 | return context.scopes.findLastIndex(s => s.startsWith("meta.math")) > context.scopes.findLastIndex(s => s.startsWith("comment") || s.startsWith("meta.text.normal.tex"));
152 | }
153 | endglobal
154 | ```
155 |
156 | And for some snippets, you will see `context math(context)` in front of which, e.g., the *greater or equal* snippet:
157 |
158 | ```javascript
159 | context math(context)
160 | snippet `>=|(? s.startsWith("meta.math")) && !context.scopes.some(s => s.startsWith("comment") || s.startsWith("meta.text.normal.tex"));
181 | }
182 | endglobal
183 | ```
184 |
185 | However, this leads to some problems. For example, sometimes, in the equation, I want to write
186 |
187 | ```latex
188 | \[
189 | x_n = x \text{ for some \(n\) large enough},
190 | \]
191 | ```
192 |
193 | Such a case is fine since the math I want to write in the text scope is simple, just `\(n\)`. However, in the current (and perhaps most popular) scope function, it happens that `\(\text{ \( not in the math mode \) }\)`.
194 |
195 | To overcome this, I write the following more generic version:
196 |
197 | ```javascript
198 | global
199 | function math(context) {
200 | return context.scopes.findLastIndex(s => s.startsWith("meta.math")) > context.scopes.findLastIndex(s => s.startsWith("comment") || s.startsWith("meta.text.normal.tex"));
201 | }
202 | endglobal
203 | ```
204 |
205 | So, since the nested environment is ordered in the scope, this will always return the correct mode. Even better, there will be no undefined behavior since if the `.findLastIndex` can't find either, it will return `-1` instead of something undefined, so everything is handled.
206 |
207 | ### Sympy and Mathematica
208 |
209 | Unlike Gilles Castel's approach, there is an available extension out there for you to simplify your math calculation already! Please go check out [Latex SYMPY Calculator](https://marketplace.visualstudio.com/items?itemName=OrangeX4.latex-sympy-calculator). It works like follows:
210 |
211 |
212 |
213 |
214 |
215 | Magic right? Let's set it up! First, please look at the installation document provided by [Latex Sympy Calculator](https://marketplace.visualstudio.com/items?itemName=OrangeX4.latex-sympy-calculator). After your installation, you can set up the keybinding for calculating the math expression. I use `shift`+`e`, where `e` stands for evaluating, to calculate so that it will append an equal sign and the answer right after your formula, just like above. If you want to avoid showing the intermediate steps of your calculation, you can use `shift`+`r`, where `r` stands for replacing, to directly replace the whole formula and give me the answer only. See the demo below:
216 |
217 |
218 |
219 |
220 |
221 | > This plugin is indeed more powerful than just this, see the documentation for detail.
222 |
223 | Let's go to the last thing covered in Gilles Castel's post, correcting spelling mistakes.
224 |
225 | ### Correcting Spelling Mistakes on the Fly
226 |
227 | Although my typing speed is quite high, I have typos all the time. So this is a must for me. And surprisingly, this is the hardest thing until now for me to set it upright. Let's see how we can configure this functionality in VS Code! There are three plugins we need:
228 |
229 | 1. [multi-command](https://marketplace.visualstudio.com/items?itemName=ryuta46.multi-command): This is a very powerful extension, which allows you to do a sequence of actions in one shortcut. We will use this later on also, and that's the place it shines.
230 |
231 | 2. [Code Spell Checker](https://marketplace.visualstudio.com/items?itemName=streetsidesoftware.code-spell-checker): This is a popular spelling checker out there that meets our needs.
232 |
233 | 3. [LTeX](https://marketplace.visualstudio.com/items?itemName=valentjn.VSCode-ltex): If you are bad at grammar like me, you definitely want to install to check some simple grammar mistakes for you. Although it's not as powerful as [Grammarly](https://www.grammarly.com/), not even comparable, it's still a good reference for you to keep your eyes on some simple mistakes you may overlook.
234 |
235 | > There is an unofficial API for Grammarly, and the plugin can be found [here](https://marketplace.visualstudio.com/items?itemName=znck.grammarly). Though it's quite slow...
236 |
237 | Here is a quick demo of how it works when typing:
238 |
239 |
240 |
241 |
242 |
243 | Additionally, if you also want to correct your grammar error, I use the shortcut `cmd`+`k` to trigger a quick-fix for a general error.
244 |
245 |
246 |
Detail Explanation
247 |
248 | > You can skip this part if you don't want to know the working mechanism. But if you're interested, please follow! The following code snippet in `settings.json` is responsible for correcting your spelling mistakes by just clicking `cmd`+`l`.
249 | >
250 | > ```json
251 | > {
252 | > "key": "cmd+l",
253 | > "command": "extension.multiCommand.execute",
254 | > "args": {
255 | > "sequence": [
256 | > "cSpell.goToPreviousSpellingIssue",
257 | > {
258 | > "command": "editor.action.codeAction",
259 | > "args": {
260 | > "kind": "quickfix",
261 | > "apply": "first"
262 | > }
263 | > },
264 | > "cursorUndo",
265 | > ]
266 | > }
267 | > },
268 | > ```
269 | >
270 | > > Make sure that the curly braces above have a trailing comma, otherwise, VS Code will complain about it.
271 | >
272 | > The working mechanism is as follows. When you press `cmd`+`l`, the [multi-command](https://marketplace.visualstudio.com/items?itemName=ryuta46.multi-command) will do the following:
273 | >
274 | > 1. Use one of the default function from cSpell's: `goToPreviousSpellingIssue`, which jump your cursor on that spelling error word
275 | > 2. Triggered a default editor action, with the argument being `quickfix` to open a quick fix drop-down list, and choose the `first` suggestion
276 | > 3. Move your cursor back by `cursorUndo`
277 | >
278 | > And likewise, the following code snippet is responsible for correcting grammar mistakes.
279 | >
280 | > ```json
281 | > {
282 | > "key": "cmd+k",
283 | > "command": "extension.multiCommand.execute",
284 | > "args": {
285 | > "sequence": [
286 | > "editor.action.marker.prev",
287 | > {
288 | > "command": "editor.action.codeAction",
289 | > "args": {
290 | > "kind": "quickfix",
291 | > "apply": "first"
292 | > }
293 | > },
294 | > "cursorUndo",
295 | > ]
296 | > }
297 | > },
298 | > ```
299 |
300 |
301 |
302 | Now, the first part is over. Let's go to the next truly beautiful, elegant, and exciting world, drawing with [Inkscape](https://inkscape.org/zh-hant/).
303 |
304 | ## Drawing Like a Pro - With Inkscape
305 |
306 |
307 |
308 |
309 |
310 | For more examples, check out the original blog. Or for more figures I draw, you can check out [Note](https://github.com/sleepymalc/Notes).
311 |
312 | One last thing is that I'll assume you have already installed [VS Code Vim](https://marketplace.visualstudio.com/items?itemName=vscodevim.vim). While this is not required, if you don't want to use it, then you'll need to assign different keybinding. Anyway, you'll see what I mean until then!
313 |
314 | ### Inkscape
315 |
316 | A big question is, why Inkscape? In the original blog, he had already explained it. One reason is that although $\texttt{TikZ}$ can do the job of drawing vector figures in $\LaTeX$ with original support, it's too slow to set all diagrams right. This is so true since my experience with $\texttt{TikZ}$ is *nice looking* and *intuitive* but also *slow* and *bulky*. Also, the $\texttt{TikZ}$ code tends to be **long**. A large file will take [*latexindent*](https://ctan.org/pkg/latexindent) and *pdfLaTeX* **a minute** to compile for one save. That's not efficient at all, especially when you want some instant feedback for some small changes.
317 |
318 | #### Download Inkscape
319 |
320 | You need to install [Inkscape](https://inkscape.org/zh-hant/) first. I recommend you install this in a terminal. I assume that you have your [`homebrew`](https://brew.sh/) installed. Then, just type the following into your terminal:
321 |
322 | ```sh
323 | > brew install --cask inkscape
324 | ```
325 |
326 | #### Set up the Environment in LaTeX
327 |
328 | First thing first, include the following in your preamble
329 |
330 | ```latex
331 | \usepackage{import}
332 | \usepackage{xifthen}
333 | \usepackage{pdfpages}
334 | \usepackage{transparent}
335 |
336 | \newcommand{\incfig}[1]{%
337 | \def\svgwidth{\columnwidth}
338 | \import{./Figures/}{#1.pdf_tex}
339 | }
340 | ```
341 |
342 | And to use it in your code, it's like the following:
343 |
344 | ```latex
345 | \begin{figure}[H]
346 | \centering
347 | \incfig{figure's name}
348 | \caption{Your caption}
349 | \label{fig:label}
350 | \end{figure}
351 | ```
352 |
353 | And then you're done! Also, the compilation time for this is shorter than you can ever expect. Let's get started then!
354 |
355 | This assumes that your $\LaTeX$ project's home directory looks like this:
356 |
357 | ```sh
358 | LaTeX_project
359 | ├── main.tex
360 | ├── main.pdf
361 | ├── Figures
362 | │ ├── fig.pdf
363 | │ ├── fig.pdf_tex
364 | │ ├── fig.svg
365 | │ .
366 | .
367 | ```
368 |
369 | Now, let's get into the fun part, i.e., setting up the shortcut for this.
370 |
371 | ### Inkscape Figure Manager
372 |
373 | This is a figure manager developed by Gilles Castel, and here is the [repo](https://github.com/gillescastel/inkscape-figures). I recommend you follow the installation instructions there. Here are just some guidelines for you.
374 |
375 | 1. Install [choose](https://github.com/chipsenkbeil/choose) (specifically for macOS, [rofi](https://github.com/davatorium/rofi) for Linux instead):
376 |
377 | ```sh
378 | > brew install choose-gui
379 | ```
380 |
381 | 2. Install [fswatch](https://github.com/emcrisostomo/fswatch):
382 |
383 | ```sh
384 | > brew install fswatch
385 | ```
386 |
387 | 3. Install the Inkscape figure manager:
388 |
389 | ```sh
390 | > pip3 install inkscape-figures
391 | ```
392 |
393 | > After installing it, type `inkscape-figures` in your terminal to make sure you have corrected install it.
394 |
395 | If you're using Linux and Vim, then you are done already. But since you're using macOS and VS Code, please follow me, there are some more things for you to configure.
396 |
397 | > If you're using Windows, then check out [super-figure](https://github.com/Joao-Peterson/super-figure). It implements similar functionalities but in a more chunky way. Even if you're using macOS, you can try it too, although I prefer my setup.
398 |
399 | #### Set up Inkscape Figure Manager
400 |
401 | Firstly, install the [Command Runner](https://marketplace.visualstudio.com/items?itemName=edonet.vscode-command-runner). This will allow you to send commands into a terminal with the shortcut. The configuration is in [`settings.json`](./VSCode-setting/settings.json), and we'll see how it works later. Now, this is a tricky part: you need to find the source code of the inkscape-figures manager. In my case, it's in `/Users/pbb/opt/anaconda3/lib/python3.8/site-packages/inkscapefigures`.
402 |
403 | > Using global finding may be helpful...
404 |
405 | Open this directory by VS Code, there is something for you to modify. Ok, I know you probably don't have that much patience now, so I have a modified version available [here](./Inkscape-setting/Inkscape-figure-manager/). Just replace the whole directory with mine, and you're good to go.
406 |
407 | > Notice that the directory in this repo is named `Inkscape-figure-manager`, while in your system, it should be `inkscapefigures`.
408 |
409 |
410 |
Detail Explanation
411 |
412 | > In Gilles Castel's approach, he uses the shortcut `ctrl`+`f` to trigger this script, which will copy the whole line's content depending on the cursor's position, and the script will send the snippets by the function
413 | >
414 | > ```python
415 | > def latex_template(name, title):
416 | > return '\n'.join((r"\begin{figure}[ht]",
417 | > r" This is a custom LaTeX template!",
418 | > r" \centering",
419 | > rf" \incfig[1]{{{name}}}",
420 | > rf" \caption{{{title}}}",
421 | > rf" \label{{fig:{name}}}",
422 | > r"\end{figure}"))
423 | > ```
424 | >
425 | > to `stdout`, and then create a figure by the `name`, which is the content of the line.
426 | >
427 | > But this in VS Code is impossible, hence we don't need this, we'll use command line. And if we leave this function as it was, then it will send all these snippets into our terminal, which is quite annoying. So the modified version just removes this snippet completely.
428 | >
429 | > But let me explain it to you, in case you want to modify it to meet your need later on. First thing first, we see that in the given code in [`keybindings.json`](./VSCode-setting/keybindings.json) and [`settings.json`](./VSCode-setting/settings.json), we're using [Command Runner](https://marketplace.visualstudio.com/items?itemName=edonet.vscode-command-runner), so let me tell you how to set this up first.
430 |
431 |
432 |
433 | We're now prepared to see a detailed explanation of commands provided in [Inkscape figure manager](https://github.com/gillescastel/inkscape-figures). There are three different commands in the [Inkscape figure manager](https://github.com/gillescastel/inkscape-figures). We break it down one by one.
434 |
435 | #### Watch
436 |
437 | Since Inkscape by default does not save the file in `pdf+latex`, we need [Inkscape figure manager](https://github.com/gillescastel/inkscape-figures) to help us. We need to first open the file watcher to *watch* the file for any changes. If there is any, then the file watcher will tell Inkscape to save the file in `pdf+latex` format.
438 |
439 | To open the file watcher, you can type `inkscape-figures watch` in the terminal. But remember the [Command Runner](https://marketplace.visualstudio.com/items?itemName=edonet.vscode-command-runner) we just installed? We can assign this command with a keybinding! In my case, since I don't want to introduce more than one keybinding for Inkscape-figures manager, I use `mode` provided by `vim` to help us. In `VISUAL` mode (enter by `v` in `NORMAL` mode), press `ctrl`+`f`.
440 |
441 | > You should trigger this at the beginning. i.e., use this after you open your project folder. To check whether `watch` is triggered correctly, you can simply open the terminal and see what's the output when you press `ctrl`+`f`: If it's already triggered, then it'll show
442 | >
443 | > ```sh
444 | > > inkscape-figures watch
445 | > Unable to lock on the pidfile.
446 | > ```
447 | >
448 | > Otherwise it'll simply show nothing. (Remember to select the terminal corresponds to `runCommand`!)
449 |
450 |
451 |
Detail Explanation
452 |
453 | > In [`keybindings.json`](./VSCode-setting/keybindings.json), we have
454 | >
455 | > ```json
456 | > {
457 | > "key": "ctrl+f",
458 | > "command": "command-runner.run",
459 | > "args": {
460 | > "command": "inkscapeStart",
461 | > "terminal": {
462 | > "name": "runCommand",
463 | > "shellArgs": [],
464 | > "autoClear": true,
465 | > "autoFocus": false
466 | > }
467 | > },
468 | > "when": "editorTextFocus && vim.active && vim.use && !inDebugRepl && vim.mode == 'Visual'"
469 | > }
470 | > ```
471 | >
472 | > for starting the [Inkscape figure manager](https://github.com/gillescastel/inkscape-figures). And the command is defined in [`settings.json`](./VSCode-setting/settings.json):
473 | >
474 | > ```json
475 | > "command-runner.commands": {
476 | > "inkscapeStart": "inkscape-figures watch"
477 | > }
478 | > ```
479 | >
480 | > In detail, we just use [Command Runner](https://marketplace.visualstudio.com/items?itemName=edonet.vscode-command-runner) to run the command we defined in [`settings.json`](./VSCode-setting/settings.json), in this case, I explicitly tell the keybinding `ctrl`+`f` will trigger `inkscapeStart` when I'm in `VISUAL` mode in Vim, which is just `inkscape-figures watcher` as defined above.
481 | >
482 | > Notice that we set the `autoFocus=false` for the terminal [Command Runner](https://marketplace.visualstudio.com/items?itemName=edonet.vscode-command-runner) uses since we don't want a pop-up terminal to distract us. If you want to see whether the command is triggered correctly every time, you can set it to `true`.
483 |
484 |
485 |
486 | #### Create
487 |
488 | Same as above, we also use `ctrl`+`f` to trigger `inkscape-figures create` command. But in this case, we use `INSERT` for creating a new Inkscape figure. Specifically, we first type out the image's name we want our image to be called, then, in this case, we're already in `INSERT` mode, we just press `ctrl`+`f` to create this image after naming.
489 |
490 |
491 |
Detail Explanation
492 |
493 | > We set up our [`keybindings.json`](./VSCode-setting/keybindings.json) as
494 | >
495 | > ```json
496 | > {
497 | > "key": "ctrl+f",
498 | > "command": "extension.multiCommand.execute",
499 | > "args": {
500 | > "sequence": [
501 | > "editor.action.clipboardCopyAction",
502 | > "editor.action.insertLineAfter",
503 | > "cursorUp",
504 | > "editor.action.deleteLines",
505 | > {
506 | > "command": "editor.action.insertSnippet",
507 | > "args": {
508 | > "name": "incfig"
509 | > }
510 | > },
511 | > {
512 | > "command": "command-runner.run",
513 | > "args": {
514 | > "command": "inkscapeCreate",
515 | > },
516 | > "terminal": {
517 | > "name": "runCommand",
518 | > "shellArgs": [],
519 | > "autoClear": true,
520 | > "autoFocus": false
521 | > }
522 | > },
523 | > ]
524 | > },
525 | > "when": "editorTextFocus && vim.active && vim.use && !inDebugRepl && vim.mode == 'Insert'"
526 | > },
527 | > ```
528 | >
529 | > and also in [`settings.json`](./VSCode-setting/settings.json):
530 | >
531 | > ```json
532 | > "command-runner.commands": {
533 | > "inkscapeCreate": "inkscape-figures create ${selectedText} ${fileDirname}/Figures/"
534 | > }
535 | > ```
536 | >
537 | > We break down what `ctrl`+`f` do in `INSERT` mode exactly step by step. We see that when we press `ctrl`+`f` in `INSERT` mode, we trigger `multiCommand.execute` to execute a sequence of instructions, which are
538 | >
539 | > 1. Copy the content into your clipboard of the line your cursor at
540 | > 2. Insert a blank line after since we need to insert a snippet, and that will delete an additional line. You can try to delete this and the next instruction, and see what happens.
541 | > 3. Move back our cursor after inserting that new line.
542 | > 4. Delete that copied content by removing this line.
543 | > 5. Insert a snippet defined in [`latex.json`](./VSCode-setting/Snippets/latex.json). **Notice that this is the default snippet functionality built-in VS Code, not [HyperSnips](https://marketplace.visualstudio.com/items?itemName=draivin.hsnips) we have used before**. I'll explain where to copy this file in a minute.
544 | > 6. Lastly, we send a command in a terminal by [Command Runner](https://marketplace.visualstudio.com/items?itemName=edonet.vscode-command-runner), with the command `inkscapeCreate` we defined in [`settings.json`](./VSCode-setting/settings.json).
545 | >
546 | > In the fifth instruction, the snippet we used is
547 | >
548 | >
549 | >
550 | > which is just the snippet we remove from [Inkscape figure manager](https://github.com/gillescastel/inkscape-figures)'s source code! It's back again, in a different approach.
551 |
552 |
553 |
554 |
555 |
556 |
557 |
558 | Let me break it down for you. Firstly, I changed into `INSERT` mode in VS Code Vim and typed my new figure's name `figure-test`. Then, I press `ctrl`+`f` to trigger the keybinding, which will automatically create an Inkscape figure named `figure-test` for me and open it.
559 |
560 | > The three files will be created along the way: `figure-test.pdf`, `figure-test.pdf_tex` and `figure-test.svg`. Unfortunately, to rename a file, you'll need to manually rename three of them.
561 |
562 | #### Edit
563 |
564 | Again, we also use `ctrl`+`f` to trigger `inkscape-figures edit` command, but this time in `NOMAL` mode. Here, [choose](https://github.com/chipsenkbeil/choose) comes into play. After you select the image you want to edit in Inkscape, you simply press `enter` and it'll open that image for you to edit.
565 |
566 | > You can modify the styling of [choose](https://github.com/chipsenkbeil/choose). For example, in [`picker.py`](./Inkscape-setting/Inkscape-figure-manager/picker.py), we have the following:
567 | >
568 | > ```python
569 | > def get_picker_cmd(picker_args=None, fuzzy=True):
570 | > """
571 | > Create the shell command that will be run to start the picker.
572 | > """
573 | > if SYSTEM_NAME == "Darwin":
574 | > args = ["choose"]
575 | > # args = ["choose", "-u", "-n", "15", "-c", "BB33B7", "-b", "BF44C8"]
576 | > ```
577 | >
578 | > We see that we don't have any additional argument for `choose`, but if you want, you can replace this line by the next line, which modify the style of `choose`. For detail information, type `choose -h` to see all the options.
579 |
580 |
581 |
Detail Explanation
582 |
583 | > The corresponding keybinding in ['keybindings.json'](./VSCode-setting/keybindings.json) is:
584 | >
585 | > ```json
586 | > {
587 | > "key": "ctrl+f",
588 | > "command": "command-runner.run",
589 | > "args": {
590 | > "command": "inkscapeEdit",
591 | > "terminal": {
592 | > "name": "runCommand",
593 | > "shellArgs": [],
594 | > "autoClear": true,
595 | > "autoFocus": false
596 | > }
597 | > },
598 | > "when": "editorTextFocus && vim.active && vim.use && !inDebugRepl && vim.mode == 'Normal'"
599 | > },
600 | > ```
601 | >
602 | > and also in [`settings.json`](./VSCode-setting/settings.json):
603 | >
604 | > ```json
605 | > "command-runner.commands": {
606 | > "inkscapeEdit": "inkscape-figures edit ${fileDirname}/Figures/"
607 | > }
608 | > ```
609 | >
610 | > I think now it's clear enough how all these work together to trigger the corresponding command. When you press `ctrl`+`f` in `NORMAL` mode, you'll trigger the `inkscape-figures edit` command, and it'll look into your `Figures/` subfolder to see what figures you have and pop out a window for you to choose, which is the functionality provided by [choose](https://github.com/chipsenkbeil/choose).
611 |
612 |
613 |
614 | In the following demo, I create another figure named `figure-test2`, then modify it a little, and compile it again.
615 |
616 |
617 |
618 |
619 |
620 | ### Inkscape Shortcut Manager
621 |
622 | In this section, we'll set up a very efficient shortcut manager to help you draw any mathematical figures faster than you can ever imagine! Notice that this setup is quite complicated, but the result is quite good. It depends on
623 |
624 | - [Hammerspoon](https://www.hammerspoon.org/): For windows focus.
625 | - [Karabiner Elements](https://karabiner-elements.pqrs.org/): For capturing the overlapping key chords.
626 |
627 | Please download the above two apps.
628 |
629 | > This section is contributed **purely** by [@kiryph](https://github.com/kiryph) in [#1](https://github.com/sleepymalc/VSCode-LaTeX-Inkscape/issues/1).
630 |
631 | #### Karabiner Elements
632 |
633 | We'll need [Karabiner Elements](https://karabiner-elements.pqrs.org)' [Complex Modifications](https://karabiner-elements.pqrs.org/docs/json/root-data-structure/#custom-json-file-in-configkarabinerassetscomplex_modifications) to help us. The steps are the following (adapted from [️⌨ How to type?](https://pbb.wtf/posts/How2TypeFast#import-settings)).
634 |
635 | 1. Open [Karabiner-Elements](https://karabiner-elements.pqrs.org/), go to *Misc* and click on *Export & Import*.
636 |
637 |
638 |
639 | 2. Copy [`Inkscape.json`](https://github.com/sleepymalc/VSCode-LaTeX-Inkscape/blob/main/Inkscape-setting/Inkscape-shortcut-manager/Inkscape.json) into `.config/karabiner/assets/complex_modifications`.
640 |
641 |
642 |
643 | 3. Again open [Karabiner-Elements](https://karabiner-elements.pqrs.org/), go to *Complex Modifications* and click on *Add rule*.
644 |
645 |
646 |
647 | 4. Enable it.
648 |
649 |
650 |
651 |
652 | If you're interested in how [`Inkscape.json`](https://github.com/sleepymalc/VSCode-LaTeX-Inkscape/blob/main/Inkscape-setting/Inkscape-shortcut-manager/Inkscape.json) is created, see the following.
653 |
654 |
655 |
656 | Detail Explanation
657 |
658 | The [`Inkscape.json`](https://github.com/sleepymalc/VSCode-LaTeX-Inkscape/blob/main/Inkscape-setting/Inkscape-shortcut-manager/Inkscape.json) is created by using a [`jsonnet`](https://jsonnet.org) file. The file can be found [here](https://github.com/sleepymalc/VSCode-LaTeX-Inkscape/blob/main/Inkscape-setting/Inkscape-shortcut-manager/karabiner-Inkscape.jsonnet),
659 |
660 |
661 |
662 | and the `jsonnet` tool can be installed via `> brew install jsonnet`.
663 |
664 | Converting the `jsonnet` file into the `json` file for [Karabiner Elements](https://karabiner-elements.pqrs.org/) can be done as follows
665 |
666 | ```sh
667 | > jsonnet karabiner-Inkscape.jsonnet > ~/.config/karabiner/assets/complex_modifications/karabiner-Inkscape.json
668 | ```
669 |
670 |
671 |
672 | #### Hammerspoon
673 |
674 | Firstly, open the [Hammerspoon](https://www.hammerspoon.org/) console and run `hs.ipc.cliInstall()` to install the cli command `hs`. Then, just add the following code to your [`~/.hammerspoon/init.lua`](./Inkscape-setting/Inkscape-shortcut-manager/init.lua).
675 |
676 |
677 |
678 | #### Reference Card for Key Chords
679 |
680 | As a reference for the key chords, I added the original picture from [the original blog](https://castel.dev/post/lecture-notes-2/) but with the key chords included in the picture.
681 |
682 |
683 |
684 |
685 |
686 | #### Missing Key Chords
687 |
688 | I did not add the *ergonomic* rebinding `x`, `w`, `f`, and `shift`+`z`. This should be possible in Inkscape itself. This setup also misses the bindings `t`, `shift`+`t`, `a`, `shift`+`a`, `s`, and `shift`+`s`. Since I encountered issues I did not pursue these.
689 |
690 | ### Summary
691 |
692 | This is the whole setup I have, and let's wrap this up since I know this may be quite overwhelming.
693 |
694 | 1. Before you start your project, enter the `VISUAL` mode by pressing `v` in `NORMAL` mode. And then press `ctrl`+`f`. This will set up the file watcher.
695 | 2. When you want to create a new figure, go into a new line, type the name of your figure in `INSERT` mode, then press `ctrl`+`f`. This will create a new figure with the name you typed, and open it in Inkscape for you.
696 | 3. When you have drawn your figure, as long as you press `cmd`+`s` in Inkscape, it will automatically save the figure in `pdf+latex` for you, then you can close Inkscape.
697 | 4. When you want to edit one of your figures, you press `ctrl`+`f` in `NORMAL` mode, it will pop out a window for you to choose the figure you want to edit. And the rest is the same as 3.
698 |
699 | ## Updates
700 |
701 | ### ~~About Inkscape Shortcut Manager (09.27.21)~~
702 |
703 | ~~After some research, although there is a way to let the original script in [inkscape-shortcut-manager](https://github.com/gillescastel/inkscape-shortcut-manager) run correctly since it depends on `xlib`, which is no longer used by macOS for almost every application(including Inkscape, as expected), hence the only thing I can do now is to give up. In a perceivable future, if I have time to find an alternative way to interrupt the window activity in macOS, I'll try to configure it for macOS.~~
704 |
705 | > Now the Inkscape Shortcut Manager is fully functional, see [here](#inkscape-shortcut-manager).
706 |
707 | ### Quiver - For commutative diagram (01.24.22)
708 |
709 | I have been working on Category Theory for a while, and I found out that [quiver](https://q.uiver.app/) is quite appealing, hence I integrate it into my workflow. You can also pull it to your local environment, configure the VS Code Task, and combine it with a hotkey to use it **locally**. Specifically, I added the following code to my [`keybindings.json`](./VSCode-setting/keybindings.json):
710 |
711 | ```json
712 | {
713 | "key": "ctrl+c",
714 | "command": "command-runner.run",
715 | "args": {
716 | "command": "quiver",
717 | "terminal": {
718 | "name": "runCommand",
719 | "shellArgs": [],
720 | "autoClear": true,
721 | "autoFocus": false
722 | }
723 | },
724 | "when": "editorTextFocus"
725 | },
726 | ```
727 |
728 | and also, define the command `quiver` as
729 |
730 | ```json
731 | "command-runner.commands": {
732 | "quiver": "open -na 'Google Chrome' --args --new-window /quiver/src/index.html"
733 | },
734 | ```
735 |
736 | Notice that you'll need to build it first if you want to use it offline! Please follow the tutorial [here](https://github.com/varkor/quiver). Otherwise, it's totally fine to use `"quiver": "open -na 'Google Chrome' --args --new-window https://q.uiver.app/"` as your command.
737 |
738 | This is what the workflow looks like.
739 |
740 |
741 |
742 |
743 |
744 | To use the package `tikz-cd`, you need to include the following in your header:
745 |
746 | ```latex
747 | % quiver style
748 | \usepackage{tikz-cd}
749 | % `calc` is necessary to draw curved arrows.
750 | \usetikzlibrary{calc}
751 | % `pathmorphing` is necessary to draw squiggly arrows.
752 | \usetikzlibrary{decorations.pathmorphing}
753 |
754 | % A TikZ style for curved arrows of a fixed height, due to AndréC.
755 | \tikzset{curve/.style={settings={#1},to path={(\tikztostart)
756 | .. controls ($(\tikztostart)!\pv{pos}!(\tikztotarget)!\pv{height}!270:(\tikztotarget)$)
757 | and ($(\tikztostart)!1-\pv{pos}!(\tikztotarget)!\pv{height}!270:(\tikztotarget)$)
758 | .. (\tikztotarget)\tikztonodes}},
759 | settings/.code={\tikzset{quiver/.cd,#1}
760 | \def\pv##1{\pgfkeysvalueof{/tikz/quiver/##1}}},
761 | quiver/.cd,pos/.initial=0.35,height/.initial=0}
762 |
763 | % TikZ arrowhead/tail styles.
764 | \tikzset{tail reversed/.code={\pgfsetarrowsstart{tikzcd to}}}
765 | \tikzset{2tail/.code={\pgfsetarrowsstart{Implies[reversed]}}}
766 | \tikzset{2tail reversed/.code={\pgfsetarrowsstart{Implies}}}
767 | % TikZ arrow styles.
768 | \tikzset{no body/.style={/tikz/dash pattern=on 0 off 1mm}}
769 | ```
770 |
771 | You can certainly follow my [Template](https://github.com/sleepymalc/Academic-Template), which already includes all the requirement headers for you.
772 |
773 | ### Migrate to HyperSnips (02.18.22)
774 |
775 | Now, instead of using [HyperSnips for Math](https://marketplace.visualstudio.com/items?itemName=OrangeX4.hsnips), we're using [HyperSnips](https://marketplace.visualstudio.com/items?itemName=draivin.hsnips), namely the **original one**! Since I just found out that we can trigger the snippets **only in math mode** by using the special keyword called `context`, I migrated to the original one. To migrate, you just need to uninstall [HyperSnips for Math](https://marketplace.visualstudio.com/items?itemName=OrangeX4.hsnips), install [HyperSnips](https://marketplace.visualstudio.com/items?itemName=draivin.hsnips) with the updated [latex.hsnips](./VSCode-setting/Snippets/latex.hsnips) I prepared for you, and then enjoy!
776 |
777 | ### Documenting Inkscape Shortcut Manager (07.30.22)
778 |
779 | I finally have time to document the configuration of the [Inkscape shortcut manager](#inkscape-shortcut-manager) and make some changes to make this document more readable. Personally, I have used this workflow for more than half of a year, so I think this is stable and will not be changed shortly.
780 |
781 | ## Credits
782 |
783 | Again, thanks to Gilles Castel, this workflow fits my style. Although it originally worked in Linux+Vim only, the idea is the most important thing. Without his wonderful post, I can't even imagine this is possible. But now it is! Go to his original post to show him some love.
784 |
785 | ## Related Project
786 |
787 | 1. [LaTeX-Template](https://github.com/sleepymalc/LaTeX-Template)
788 | 2. [Notes](https://github.com/sleepymalc/Notes)
789 | 3. [gillescastel/inkscape-figures](https://github.com/gillescastel/inkscape-figures)
790 | 4. [gillescastel/inkscape-shortcut-manager](https://github.com/gillescastel/inkscape-shortcut-manager)
791 | 5. [chipsenkbeil/choose](https://github.com/chipsenkbeil/choose)
792 | 6. [varkor/quiver](https://github.com/varkor/quiver)
793 |
794 | ## Star History
795 |
796 |