shenapse / latex-snippets-for-vscode

A set of snippets to write LaTeX faster with VS Code.
MIT License
6 stars 0 forks source link

Overview

This repository provides a much more efficient method to input LaTeX code in VSCode compared to traditional user-snippets-based input method.

This repository is a kind of extension of preceding works latex-setup and Latex Snippets for Visual Studio Code. We utilize the Hypersnips Extension for VSCode to introduce "dynamic" snippets in addition to traditional VSCode user snippets. It provides a faster way of inputting routine commands, such as \alpha, \mathbb{R}, x_1, \\ldots, x_n and \lim_{n \to \infty}, as the demos below show.

This repository owes much to the preceding works cited above. In fact, many snippets are borrowed from them, often with slight modification. My work is summarized as:

Most of what follows can also be read in my Japanese article.

Demos

Each demo shows keystrokes and the result of the input. For simplicity Tab is represented by "|", which is frequently used to jump to the next tab stop. The point is that we apply various conversions to (a part of) preceding text instead of inputting pre-registered texts. As a result, most input is done without Enter and ←↑→↓.

Throughout demos, E represents Enter, and braces {} are not typed in reality. They are inserted in order to visualize word breaks that are otherwise difficult to see.

demo 1: definition of continuous real function

Let U{;;}|be an open subset of {;;}{R#}|.CE A function {;;}f:U to {R#}|is said to be {tit}contnuous at|{;;}{x0}|if for any {;;}{ze}>0|there is {;;}{zd}>0|such that {eqv}{E}{abs}x-x0|<{zd}{imp}{abs}f(x→-f(x0→<{ze}|.Ctrl+S, where C represents Ctrl.

demo 1 demo-continuity-png

Note that the word "to" in A function {;;}f:U to {R#}| is converted to \to but not in "is said to be", as expected.

demo 2: dual basis

Let {;;}e1{,,}{D}n|be a basis of a vector space V{;;}|and let ;;{lr)}{f1}{tt}{,,}{D}n|be the..., where D represents Delete.

demo 2 demo-dual-basis-png

demo 3: Taylor expansion of exponential function

{eqv}{E}{exp}x= {sum}{_}{n}|||{xNN}{tt}|/n!|{\\}{E}={lim} {_}{n}|| {lr)}1+x/n|{^^}nCtrl+S.

demo 3 demo-taylor-expansion-png

demo 4: Tensor isomorphism

;;V {oxx}W {sim}{eq}W {oxx}V|.{E}Indeed, the map ;;V {oxx}W {ni}(x,y→{mpt}(y,x→{in}W {oxx}V| is....

demo 4 demo-tensor-isomorphism-png

Snippets Overview

This repository provides two different types of snippets: static one (latex.code-snippets) and dynamic one (latex.hsnips). Our emphasis is on the latter.

Trigger Interactition Output Defined by
Static Text Yes
(Suggestion to user)
Registered Text JSON
Dynamic Context & Text No
(Auto conversion)
Transformation of Input Text RegExp & JS

Static Snippets

The static snippets are just typical VSCode snippets. It consists of snippets for \begin \end style math environments, such as \begin{equation} and \begin{proof}. They can be used everywhere on a tex file. If you skim through the first section of the cheat sheets and then try some commands such as eq, eqv, gat,gatv, you'll know how to use it.

For instance, cas gets you the suggestion for \begin{cases} \end{cases}. The "v"ed version casv gives you the non-numbered cases \begin{cases*} \end{cases*}. Here, v means "void". The suffix v works similarly for other \begin-\end style environments.

One thing to note here about static snippet is that defined snippets thm, dfn and prf etc. do not yield \begin{theorem}, \begin{definition} and \begin{proof} etc. Actually, it yields ALIASes for them such as \begin{thm}, \begin{dfn} and \begin{prf} defined in envs.sty. They are expected to be customized according to user preferences, but may of course be used as is.

Dynamic Snippets

For the practical usage of our dynamic snippets, we refer to the cheat sheet and snippets reference. If you encounter snippets that are not defined in de facto latex packages, commands.sty would be your help.

If you want a hands-on experience, I suggest you try to reproduce the above demos yourself. It's the essence of dynamic snippets in a nutshell. The demo directory contains a tex file for this exercise.

Math Context

HyperSnips Extension provides "context", which we use to tell our snippets whether cursor is within a math environment (such as \(x\), \[ \] and \begin-\end{equation}) or not. (Technically, it is an array of strings provided through vscode-textmate library.) Its benefit and limitation are very important to know since it plays a fundamental role in determining the domain of dynamic snippets.

Triggers for dynamic snippets for math are defined to get active only when we are in a math environment. Here is a demo. The point is that the word "to" is converted to \to only when it is typed within a math environment.

demo-math-context

More than 99% of our dynamic snippets are for math. So you may reasonably assume that dynamic snippets are designed to be used like this, but there are trivial exceptions:

A dynamic snippet for $ $ is intentionally not defined. More importantly, our dynamic snippets (more precisely, the function math(context) in our latex.hsnips) does not recognize $ based inline math as a math environment. In other words, $ math inline is not supported. This is not just because \( \) is recommended instead of $. In fact, $ based math environments can have a confusing context, and allowing it can cause unintended behavior; for instance, "to" is converted into \to outside math environment, which is really annoying. For this reason, I gave it up at least for the current version. (If you are interested, read my comments for math(context) function in latex.hsnips file.)

Experimental: UTF Snippets and Commands

This subsection might be safely skipped. We wrote the following code in demo 4:

% demo 4: tensor isomorphism
\( V \otimes W \simeq W \otimes V \).
Indeed, the map \( V \otimes W \ni x \otimes y \mapsto y \otimes x \in W \otimes V \) is an isomorphism.

If we allow ourselves to use UTF character in a command name (this is possible, for instance, with LuaLaTeX and XeLaTeX), then UTF version would look like:

% demo 4 UTF version
% PC browser might fail to render this 
\( V \⊗ W \≃ W \otimes V \).
Indeed, the map \( V \⊗ W \∋ x \⊗ y \↦ y \⊗ x \∈ W \⊗ V \) is an isomorphism.

But how can we input the characters like and in some reasonable way? Just use utf_latex.hsnips and commands_UTF.sty. They contain snippets for UTF commands and the definition of UTF commands.

We can use many UTF snippets by the simple rule "utf trigger = u + traditional trigger". For instance, uoxx -> \⊗, uin -> \∈, but ueqsim -> \≃ (an exception since we have simeq -> $\simeq$). As they are experimental, there are many problems to be solved, such as coverage of UTF characters, consistency of snippets names, and integrability with traditional tools etc. I introduced it here out of curiosity that some readers might be interested in UTF latex coding.

Install

For static snippets latex.code-snippets, see Snippets in Visual Studio Code. It can be installed locally and globally.

For dynamic snippets latex.hsnips, see Hypersnips Extension. UTF snippets are enabled just by adding its functions to a properly placed latex.hsnips. (Remember commands_UTF.sty if you compile UTF commands!)

You might want to turn off other snippets, for instance, those provided by LaTeX Workshop Extension. Then Control Snippets is useful.

Contribution

This repository was born as a byproduct of my TeX writing. My main objective has been "write faster". It makes snippets tend to be written for practical convenience rather than perfection, with the excuse that assumed use is limited to a pure interactive process (latex coding) and the risk of bugs is small. This resulted in a lot of code that needed to be fixed. So suggestion for improvements (whether in the direction of perfection or practical convenience) are REALLY welcome.

References