【教程】vscode里面markdown写公式自动补全,畅快写论文,而且不需要额外插件
首先你有一个正常能用的 visual studio code,然后默认是肯定支持snippet的。
在编程中,snippet 指的是一种代码快捷方式或者代码模板。它允许程序员通过输入简短的关键词或缩写来自动补全或生成常见的代码块。几乎所有的现代代码编辑器和集成开发环境(IDE),如 Visual Studio Code、Atom、Sublime Text、RStudio 等,都支持 snippet 功能。例如,在编写JavaScript测试时,输入“desc”并敲击回车,编辑器会自动扩展成完整的describe函数模板。这极大地提高了编码效率,减少了重复劳动,使得程序员能够更快地编写和修改代码。
如果你想了解snippet怎么玩,可以看:
一个案例学会 VSCode Snippets,极大提高开发效率 – 知乎 (zhihu.com)
关于插件
我被坑了一下午,反正目前市面上没什么插件能完美支持。要么有一些莫名其妙的bug,要么行内公式无法正确预览。目前vsc自带的markdown查看器虽然没一些插件的那么帅气,但是性能好啊,而且够用了。一些仅预览的插件其实是不影响的,因为本文的snippet方案和那些插件无关,你只要保证没有冲突性的自动补全插件即可。
snippet是vsc自带的一个代码模板功能。基本上够用了。
snippet除了vsc,其他什么emacs,vim还有 intellij IDEA 什么的应该都是支持的。都可以参考。当然vsc写markdown支持实时预览,而且比较轻量。
如何配置vscode内的markdown支持数学公式自动补全?
首先,按下ctrl+shift+P启动交互指令终端,输入
> Preferences: Open User Settings (JSON)
打开设置。然后在末尾添加这么一行配置:
"[markdown]": { // 作用是开启markdown下的snippet功能。vsc默认是禁用的
"editor.quickSuggestions": {
"other": true, // 是否开启其他情况的snippet
"comments": true, // 是否开启注释下的snippet
"strings": true // 是否开启句子的snippet
},
"editor.acceptSuggestionOnEnter": "on" // 允许使用回车键选中snippet,而不仅仅是tab
}
然后,再次按下ctrl+shift+P启动交互指令终端,输入
> snippets: Configure Snippets
然后:
- 输入
latex并点开这个语言模式,应该会自动打开一个叫latex.json的文件 - 再按下
ctrl+shift+P,打开交互指令终端,输入snippets: Configure Snippets。 - 输入
markdown并点开这个语言模式,应该会自动打开一个叫markdown.json的文件
这两个json会自动存储在vsc的配置文件夹下。你现在要做的就是编辑这两个文件,加入我们用户自定义的snippet。
下面两个snippet是我按照我的习惯写的,基本应付了大多数情况。你可以按照需求继续修改升级。
建议先通读一遍,看看我都添加了哪些自动补全。
snippet代码
markdown的代码
首先,编辑 markdown.json ,在里面粘贴以下内容:
// markdown.json
{
"Tex - 单行": {
"prefix": "dd",
"body": [
"$\\displaystyle $0$"
],
"description": "公式区:单行"
},
"Tex - 单行(小)": {
"prefix": "dd",
"body": [
"$$0$"
],
"description": "公式区:单行,但是不使用 \\display 进行声明"
},
"Tex - 跨行": {
"prefix": "dd",
"body": [
"$$",
"\t$0",
"$$"
],
"description": "公式区:多行公式"
},
"Tex - 跨行 align": {
"prefix": "dd",
"body": [
"$$",
"\\begin{align}",
"\t$0",
"\\end{align}",
"$$"
],
"description": "公式区:多行公式 align"
},
"Tex - 跨行 aligned": {
"prefix": "dd",
"body": [
"$$",
"\\begin{aligned}",
"\t$0",
"\\end{aligned}",
"$$"
],
"description": "公式区:多行公式 aligned"
}
}
你可以照着snippet的语法改成你需要的模式。
latex的代码
然后,编辑latex,在里面粘贴以下内容。
vsc会将markdown内的代码段识别为latex,所以写在markdown的snippet是不生效的,坑了我好久!
// latex.json
{
"\\left(": {
"prefix": "\\left",
"body": [
"\\left( $0 \\right)"
],
"description": "括号 ( )"
},
"\\left[": {
"prefix": "\\left",
"body": [
"\\left[ $0 \\right]"
],
"description": "括号 [ ]"
},
"\\left{": {
"prefix": "\\left",
"body": [
"\\left{ $0 \\right}"
],
"description": "括号 { }"
},
"big{": {
"prefix": "\\left",
"body": [
"\\left\\{ $0 \\right."
],
"description": "分段函数括号 { 一个大括号"
},
"abs": {
"prefix": "abs",
"body": [
"\\left| $0 \\right|"
]
},
"\\degree": {
"prefix": "\\deg",
"body": [
"^{\\circ} "
]
},
"\\mathbf": {
"prefix": "\\bf",
"body": [
"\\mathbf{$1}$0"
]
},
"\\mathbb": {
"prefix": "\\bb",
"body": [
"\\mathbb{$1}$0"
]
},
"\\partial": {
"prefix": "\\part",
"body": [
"\\partial "
]
},
"cases": {
"prefix": "cases",
"body": [
"\\begin{cases}",
"\t$1 &\\MDtext{$2}\\\\\\\\",
"\t$3 &\\MDtext{$4}\\\\\\\\$5",
"\\end{cases} $0"
]
},
"align": {
"prefix": "align",
"body": [
"\\begin{${1|align,align*,aligned|}}",
"\t$2\\\\\\\\",
"\t$3\\\\\\\\$4",
"\\end{$1} $0"
]
},
"\\frac": {
"prefix": "\\frac",
"body": [
"\\frac{$1}{$2}$0"
]
},
"\\sqrt": {
"prefix": "\\sqrt",
"body": [
"\\sqrt{$1}$0"
]
},
"\\sqrt[]": {
"prefix": "\\sqrt",
"body": [
"\\sqrt[$1]{$2}$0"
]
},
"sin": {
"prefix": "sin",
"body": [
"\\sin \\left( $1 \\right) $0"
]
},
"cos": {
"prefix": "cos",
"body": [
"\\cos \\left( $1 \\right) $0"
]
},
"tan": {
"prefix": "tan",
"body": [
"\\tan \\left( $1 \\right) $0"
]
},
"sec": {
"prefix": "sec",
"body": [
"\\sec \\left( $1 \\right) $0"
]
},
"csc": {
"prefix": "csc",
"body": [
"\\csc \\left( $1 \\right) $0"
]
},
"cot": {
"prefix": "cot",
"body": [
"\\cot \\left( $1 \\right) $0"
]
},
"exp": {
"prefix": "exp",
"body": [
"\\exp \\left( $1 \\right) $0"
]
},
"log": {
"prefix": "log",
"body": [
"\\log \\left( $1 \\right) $0"
]
},
"ln": {
"prefix": "ln",
"body": [
"\\ln \\left( $1 \\right) $0"
]
},
"lg": {
"prefix": "lg",
"body": [
"\\lg \\left( $1 \\right) $0"
]
},
"sinh": {
"prefix": "sinh",
"body": [
"\\sinh \\left( $1 \\right) $0"
]
},
"cosh": {
"prefix": "cosh",
"body": [
"\\cosh \\left( $1 \\right) $0"
]
},
"tanh": {
"prefix": "tanh",
"body": [
"\\tanh \\left( $1 \\right) $0"
]
},
"coth": {
"prefix": "coth",
"body": [
"\\coth \\left( $1 \\right) $0"
]
},
"arg": {
"prefix": "arg",
"body": [
"\\arg "
]
},
"arcsin": {
"prefix": "arcsin",
"body": [
"\\arcsin \\left( $1 \\right) $0"
]
},
"arccos": {
"prefix": "arccos",
"body": [
"\\arccos \\left( $1 \\right) $0"
]
},
"arctan": {
"prefix": "arctan",
"body": [
"\\arctan \\left( $1 \\right) $0"
]
},
"arccsc": {
"prefix": "arccsc",
"body": [
"\\operatorname{arccsc} \\left( $1 \\right) $0"
]
},
"arcsec": {
"prefix": "arcsec",
"body": [
"\\operatorname{arcsec} \\left( $1 \\right) $0"
]
},
"arccot": {
"prefix": "arccot",
"body": [
"\\operatorname{arccot} \\left( $1 \\right) $0"
]
},
"arsinh": {
"prefix": "arsinh",
"body": [
"\\operatorname{arsinh} \\left( $1 \\right) $0"
]
},
"arcosh": {
"prefix": "arcosh",
"body": [
"\\operatorname{arcosh} \\left( $1 \\right) $0"
]
},
"artanh": {
"prefix": "artanh",
"body": [
"\\operatorname{artanh} \\left( $1 \\right) $0"
]
},
"csch": {
"prefix": "csch",
"body": [
"\\operatorname{csch} \\left( $1 \\right) $0"
]
},
"sech": {
"prefix": "sech",
"body": [
"\\operatorname{sech} \\left( $1 \\right) $0"
]
},
"arcsch": {
"prefix": "arcsch",
"body": [
"\\operatorname{arcsch} \\left( $1 \\right) $0"
]
},
"arsech": {
"prefix": "arsech",
"body": [
"\\operatorname{arsech} \\left( $1 \\right) $0"
]
},
"arcoth": {
"prefix": "arcoth",
"body": [
"\\operatorname{arcoth} \\left( $1 \\right) $0"
]
},
"min": {
"prefix": "min",
"body": [
"\\min \\left( $1 \\right) $0"
]
},
"max": {
"prefix": "max",
"body": [
"\\max \\left( $1 \\right) $0"
]
},
"inf": {
"prefix": "\\inf",
"body": [
"\\infty"
]
},
"lim": {
"prefix": "\\lim",
"body": [
"\\lim_{${1:x} \\to ${2:\\infty}} $0"
]
},
"sum": {
"prefix": "\\sum",
"body": [
"\\sum_{$1}^{$2}$0"
]
},
"prod": {
"prefix": "\\prod",
"body": [
"\\prod_{$1}^{$2}$0"
]
},
"integral": {
"prefix": "\\int",
"body": [
"\\int_{$1}^{$2}$0"
]
},
"iint": {
"prefix": "\\iint",
"body": [
"\\iint_{$1}^{$2}$0"
]
},
"iiint": {
"prefix": "\\iiint",
"body": [
"\\iiint_{$1}^{$2}$0"
]
},
"oint": {
"prefix": "\\oint",
"body": [
"\\oint_{$1}^{$2}$0"
]
},
"begin": {
"prefix": "\\begin",
"body": [
"\\begin{${1|align,aligned,matrix,array,equation|}}",
"\t$2",
"\\end{$1}",
"$0"
]
},
"matrix": {
"prefix": "matrix",
"body": [
"\\begin{matrix}",
"\t${1|1|} & ${2|2|} \\\\\\",
"\t${3|3|} & ${4|4|} \\\\\\",
"\\end{matrix}"
],
"description": "矩阵"
},
"matrix[]": {
"prefix": "matrix",
"body": [
"\\left[\\begin{matrix}",
"\t${1|1|} & ${2|2|} \\\\\\",
"\t${3|3|} & ${4|4|} \\\\\\",
"\\end{matrix}\\right]"
],
"description": "矩阵: 有括号 [ ]"
},
"array": {
"prefix": "array",
"body": [
"\\begin{array}{$1}",
"\t${2|x|} & ${3|s.t.\\;\\;x>1|} \\\\\\",
"\t${4|-x|} & ${5|s.t.\\;\\;x\\leq1|} \\\\\\",
"\\end{array}"
],
"description": "阵列,比如你想弄那种分段函数之类的"
}
}
大功告成!现在你可以开心用markdown写论文公式了!
配置好后实验一下
试验一下:输入dd,选择align,然后输入y=, 然后输入 \left 补全为分段函数括号,然后输入array自动补全
$$
\begin{align}
y=\left\{ \begin{array}{}
x & s.t.\;\;x>1 \\
-x & s.t.\;\;x\leq1 \\
\end{array} \right.
\end{align}
$$
2024年9月27日
