3

I'm using Visual Studio Code text editor to type up a LaTeX .tex document. Most text editors will auto-close brackets for you, so that when you type "{" it adds "}" and then moves your cursor in between. Since dollar signs are used to signify inline math statements in LaTeX, (i.e. $\bigcup$) I would like to have $ auto-close with another $ and then automatically move the cursor in between. I know some people prefer \(...\), but this is not what I'm asking about. Is there a setting I can find to change this?

Note: I've changed the text of this question since there was some confusion between $...$ and $$...$$.

starball
  • 123

1 Answers1

2

Note that the builtin language support extension for VS Code support wrapping the selected text like $selection$ if you press $ while text is selected (see the surroundingPairs property in https://github.com/microsoft/vscode/blob/main/extensions/latex/latex-language-configuration.json). So all that's missing is the case when there's nothing selected (which would have been the autoClosingPairs if it was implemented builtin).

Using keyboard shortcuts

You can kind of hack this by defining a keybinding that types out the dollars and then just moves the caret left (put this in your keybindings.json (run Preferences: Open Keyboard Shortcuts (JSON) in the command palette)):

{
    "key": "shift+4", // or whatever it is on your keyboard layout
    "command": "runCommands",
    "args": {
        "commands": [
            {
                "command": "type",
                "args": {"text": "$$" },
            },
            "cursorLeft",
        ],
    },
    "when": "editorTextFocus && (!editorHasSelection) && (editorLangId == latex || editorLangId == tex)"
},

Honestly, I think using shift+4 will get annoying when you actually want to type just a single dollar (I wager you'll get annoyed enough by it to change it to something else), which is probably why it's not builtin to VS Code in the autoClosingPairs property.

Using user snippets

You could take a different approach and just use user snippets, putting something like this in your snippets file (use the Snippets: Configure User Snippets command in the command palette):

"dollar math": {
    "scope": "tex,latex",
    "prefix": "dm", // this is what you type in the editor to get the snippet suggestion
    "body": ["\\$$1\\$"],
},

Sidenote: This feature-request has been brought up several times in one of the popular LaTeX extension for VS Code: https://github.com/James-Yu/LaTeX-Workshop/issues/968, https://github.com/James-Yu/LaTeX-Workshop/issues/544, https://github.com/James-Yu/LaTeX-Workshop/issues/235. Every time, it's declined (fine- I totally respect that), with the message:

Please up vote microsoft/vscode/issues/38352.

Which would allow users to configure auto-closing brackets at the user-level.

I haven't tried this, but you could potentially edit the various *-language-configuration.json files in your installation of this extension (look under its syntax/ subdirectory). Extensions are typically installed to the .vscode/extensions/ directory in your user home directory. The property you'd want to edit is autoClosingPairs for whichever of the multiple language configuration files you want to change.

Technically you could do the same thing for modifying the builtin language configuration JSON files for LaTeX in your VS Code installation, but VS Code would probably start issuing a corruption warning once you do that.

starball
  • 123