在 VS Code 中使用 LaTeX 编写时,如何在美元符号之间添加自动配对?
请注意,如果在选择文本时按
$selection$
,VS Code 的内置语言支持扩展支持将所选文本换行,如 $
(请参阅 https://github.com/microsoft/vscode 中的
surroundingPairs
属性) /blob/main/extensions/latex/latex-language-configuration.json)。因此,所缺少的是没有选择任何内容的情况(如果是内置实现的话,这将是 autoClosingPairs
)。
您可以通过定义一个键绑定来破解此问题,该键绑定键入美元,然后将插入符号向左移动(将其放入您的 keybindings.json 中(在
命令面板中运行
Preferences: Open Keyboard Shortcuts (JSON)
)):
{
"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)"
},
老实说,我认为当你实际上只想输入一美元时,使用
shift+4
会很烦人(我打赌你会因为它而恼火,将其更改为其他内容),这可能就是为什么它没有内置于 VS 中的原因代码位于 autoClosingPairs
属性中。
您可以采取不同的方法,只需使用用户片段,将类似的内容放入片段文件中(使用命令面板中的
Snippets: Configure User Snippets
命令):
"dollar math": {
"scope": "tex,latex",
"prefix": "dm",
"body": ["\\$$1\\$"],
},
旁注:此功能请求已在 VS Code 的流行 LaTeX 扩展之一中多次提出: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。每次,它都会被拒绝(好吧 - 我完全尊重这一点),并带有以下信息:
这将允许用户在用户级别配置自动关闭括号。