/ LaTeX  

Vscode配置LaTeX代码片段

Vscode配置LaTeX代码片段

在使用LaTeX书写论文时,每次从文件中复制代码段无疑是不优雅的,那么如何才能更加方便快捷地重复使用大段LaTeX代码呢?我们可以利用Vscode自带的用户片段功能。

用户片段功能在哪里?

文件->首选项->用户片段里打latex,即可打开latex.json文件,在这里面就可以书写用户片段啦

用户片段

用户片段的基本格式

基本格式

1
2
3
4
5
6
7
8
"Print to console": {
"prefix": "log",
"body": [
"console.log('$1');",
"$2"
],
"description": "Log output to console"
}

用户片段用json来书写,主要包括以下部分(主要来自参考文献3的翻译):

  • 片段名称:即Print to console。
  • 触发词(prefix):我们可以通过prefix定义一个或多个显示代码段的触发词。即上文的log。
  • 主体(body):是一行或多行内容,在插入时将作为多行连接。将根据插入代码段的上下文对分隔符和嵌入选项卡进行格式化。也就是最后实际显示在编辑器中的内容。
  • 描述(description):是对显示的代码段的可选说明。即Log output to console

书写的注意点

  • 在使用到反斜杠时,需要写\\,最终在编辑器中显示为\

  • body可以使用特殊构造来控制游标和要插入的文本

    • **按下Tab键后停止的位置:**使用Tab键,我们能够让编辑器的光标在显示的代码段中移动。使用Tab指定光标位置。数字(如$1,$2)是将访问选项卡停止的顺序,而$0表示最终光标位置。如果有同一编号被使用多次将会同步链接更新。值得注意的是这个数字大于等于10也是可行的。
    • **占位符:**复杂的代码片段会使用到占位符,占位符形如:${1:xxx}, ${2:xxx}, …, 顺序按照数字顺次排列,每个占位符中的xxx为占位符的实例内容,结束位置的占位符为$0
    • **选择占位符:**占位符可以选择在给定的范围内选择值。语法是将值用,分隔枚举,与|一起括起来。例如 ${1|c,t,b|}即表示在1位置,可以选择c,b,t三种选项,分别对应垂直居中(center),底部对齐(bottom)和顶部对齐(top)
    • 其他的操作都可以在Vscode官网上找到。

LaTeX\LaTeX 书写论文用户片段的基本案例

latex.json文件

下面给出的案例均来自于对论文常用代码的改写。主要包括

  • 单图:用image\image呼出
  • 多图
    • 子图格式:images_sub\images_sub
    • 全图格式:images_minipage\images_minipage
  • 普通三线表:tabletabular\table_tabular
  • 代码
    • 代码段:code\code
    • 代码文件:codefile\codefile
  • 【新增】beamer中常用的左图右文

上述这些是我觉得比较实用的一些内容,所以就做成了用户片段,能帮助我们迅速完成图片、表格、代码等的插入。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
{
// Place your snippets for latex here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
// "Print to console": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
"Image": {
"prefix": [
"image",
"\\image"
],
"body": [
"\\begin{figure}[${5|htbp,H,h|}]",
" \\centering",
" \\includegraphics[width=$1\\textwidth]{${2:图片位置}}",
" \\caption{${3:图片标题}}",
" \\label{fig:${4:图片标签}}",
"\\end{figure}",
"$0"
],
"description": "插入单图"
},
"ImagesSub": {
"prefix": [
"images_sub",
"\\images_sub"
],
"body": [
"\\begin{figure}[${11|htbp,H,h|}]",
" \\centering",
" \\subfigure[${3:图片1标题}]{",
" \\includegraphics[width=$1\\textwidth]{${2:图片1位置}}",
" \\label{fig:${4:图片1标签}}}",
" \\subfigure[${7:图片2标题}]{",
" \\includegraphics[width=$5\\textwidth]{${6:图片2位置}}",
" \\label{fig:${8:图片2标签}}}",
" \\caption{${9:总图片标题}}",
" \\label{fig:${10:总图片标签}}",
"\\end{figure}",
"$0"
],
"description": "插入多图并列放置(子图格式)"
},
"ImagesMini": {
"prefix": [
"images_minipage",
"\\images_minipage"
],
"body": [
"\\begin{figure}[${13|htbp,H,h|}]",
" \\centering",
" \\begin{minipage}[${1|c,t,b|}]{$2\\textwidth} ",
" \\centering",
" \\includegraphics[width=$3\\textwidth]{${4:图片1位置}}",
" \\caption{${5:图片1标题}}",
" \\label{fig:${6:图片1标签}}",
" \\end{minipage}",
" \\begin{minipage}[${7|c,t,b|}]{$8\\textwidth} ",
" \\centering",
" \\includegraphics[width=$9\\textwidth]{${10:图片2位置}}",
" \\caption{${11:图片2标题}}",
" \\label{fig:${12:图片2标签}}",
" \\end{minipage}",
"\\end{figure}",
"$0"
],
"description": "插入多图并列放置(全图格式)"
},
"Table": {
"prefix": [
"table",
"tabular",
"\\table_tabular"
],
"body": [
"\\begin{table}[${7|!htbp,H|}]",
" \\begin{center}",
" \\caption{${5:表格标题}}",
" \\begin{tabular}[${1|c,t,b|}]{${2:列对齐}}",
" \\toprule",
" $3\\\\\\\\",
" \\midrule",
" $0",
" \\bottomrule",
" \\end{tabular}",
" \\label{tb:${6:表格标签}}",
" \\end{center}",
"\\end{table}",
],
"description": "插入传统三线表"
},
"Code": {
"prefix": [
"code",
"\\code"
],
"body": [
"\\begin{lstlisting}[language=$1,numbers=${2|none,left|}]",
" $0",
"\\end{lstlisting}",
],
"description": "插入代码段落"
},
"CodeFile": {
"prefix": [
"codefile",
"\\codefile"
],
"body": [
"\\noindent\\textsc{${1:来源章节}} - \\textbf{${2:实际显示文件名}}",
"\\lstinputlisting[language=$3]{./code/$0}"
],
"description": "插入代码文件"
},
"FigText":{
"prefix": [
"figtext",
"\\figtext"
],
"body": [
"\\begin{minipage}[m]{.6\\textwidth}",
"\\vspace{-15pt} \\centering\\includegraphics[width=\\textwidth]{${1:图片}}",
"\\end{minipage} ",
"\\begin{minipage}[m]{.35\\textwidth}",
"\\begin{exampleblock}{}",
"\\structure{\\faArrowCircleLeft}\\parbox{.95\\textwidth}{\\centering ${2:标题}}",
"\\end{exampleblock} ",
"$0",
"\\end{minipage}"
],
"description": "左图右文"
}
}

使用方式

  1. 输入触发关键词,例如image

    ex1

  2. 利用键选择对应的用户片段,按Enter键选中片段

    ex2

  3. 在光标跳动位置输入正确的内容,按Tab键移动到下一个占位符处,再次输入,以此类推,直到写完所有内容。

    ex3

参考文献

  1. VScode配置latex+代码片段让写作起飞. https://blog.csdn.net/cfunction/article/details/108591734

  2. VSCode插件开发:LaTeX Snippets. https://hexo.gyrojeff.moe/2020/03/04/VSCode插件开发:LaTeX-Snippets/

  3. Snippets in Visual Studio Code. https://code.visualstudio.com/docs/editor/userdefinedsnippets

本文标题:Vscode配置LaTeX代码片段

文章作者:Levitate_

发布时间:2021年02月06日 - 11:57:29

原始链接:https://levitate-qian.github.io/2021/02/06/latex-snippests/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。