vscode latex代码片段
在 Visual Studio Code (VSCode) 中,可以通过设置代码片段 (Snippets) 来快速插入 LaTeX 模板或代码段。以下是详细步骤:
步骤 1:打开用户代码片段配置
打开命令面板:
按下Ctrl+Shift+P(或Cmd+Shift+P),输入Preferences: Configure User Snippets,然后按回车。选择语言:
在弹出的列表中搜索LaTeX,点击latex.json以打开 LaTeX 的代码片段配置文件。如果没有latex.json,系统会自动创建一个。
步骤 2:添加代码片段
在打开的 latex.json 文件中,按照以下格式添加代码片段:
基本格式
{
"片段描述": {
"prefix": "触发关键词",
"body": [
"代码的第一行",
"代码的第二行",
"代码的其他行"
],
"description": "片段的详细描述"
}
}
示例:插入 LaTeX 图片模板
{
"Insert Image Template": {
"prefix": "imgtpl", // 触发关键词
"body": [
"\\begin{figure}[htbp]",
" \\centering",
" \\includegraphics[width=\\textwidth]{${1:image.jpg}}",
" \\caption{${2:Caption text}}",
" \\label{fig:${3:label}}",
"\\end{figure}"
],
"description": "Insert a LaTeX figure environment with an image"
}
}
示例:插入表格模板
{
"Insert Table Template": {
"prefix": "tabletpl",
"body": [
"\\begin{table}[htbp]",
" \\centering",
" \\begin{tabular}{|c|c|c|}",
" \\hline",
" ${1:Header 1} & ${2:Header 2} & ${3:Header 3} \\\\",
" \\hline",
" ${4:Data 1} & ${5:Data 2} & ${6:Data 3} \\\\",
" \\hline",
" \\end{tabular}",
" \\caption{${7:Table caption}}",
" \\label{tab:${8:label}}",
"\\end{table}"
],
"description": "Insert a LaTeX table environment"
}
}
步骤 3:保存并测试
- 保存
latex.json文件。 - 打开一个 LaTeX 文件(扩展名
.tex)。 - 输入触发关键词(如
imgtpl或tabletpl),然后按下Tab键或Enter键。 - 模板代码会自动插入,并提供占位符导航(如
${1}、${2})。
功能说明
**
prefix**:- 触发片段的关键词。
- 输入该关键词后按
Tab或Enter自动插入模板。
**
body**:- 定义代码片段的内容。
- 支持多行代码,
${n}用于占位符,按Tab可以快速跳转。
**
description**:- 片段的详细说明,在触发时作为提示信息显示。
扩展功能
1. 自定义片段位置
如果需要更复杂的片段组织,可以为特定工作区单独定义片段。在工作区设置中创建片段文件(如 latex.code-snippets)。
2. 配合 LaTeX Workshop
LaTeX Workshop 插件已内置一些代码片段,您可以直接使用或覆盖它们。
3. 多光标编辑
插入片段后,使用 Tab 键可以快速导航占位符,按 Shift+Tab 返回上一个占位符。
优点
- 提高编辑效率:快速插入常用模板,减少重复输入。
- 可维护性高:集中管理代码片段,修改后全局生效。
我的代码片段
latex.json
通过快捷键 ctrl + shipt + p,输入 configure snippets,找到 latex.json:
{
"Insert miniImage Template": {
"prefix": "imgmini", // 触发关键词
"body": [
"\\begin{minipage}[t]{0.48\\textwidth}",
" \\centering",
" \\includegraphics[width=\\textwidth]{${1:image.jpg}}",
" \\caption*{${2:Caption text}}",
"\\end{minipage}"
],
"description": "Insert a LaTeX figure environment with minipage"
},
"Insert Image Template": {
"prefix": "imgtpl", // 触发关键词
"body": [
"\\begin{figure}[htbp]",
" \\centering",
" \\includegraphics[width=\\textwidth]{${1:image.jpg}}",
" \\caption{${2:Caption text}}",
" \\label{fig:${3:label}}",
"\\end{figure}"
],
"description": "Insert a LaTeX figure environment with an image"
},
"Insert quickbird": {
"prefix": "gf", // 触发关键词
"body": [
"\\subsubsection*{商业高分辨率影像}",
"\\begin{figure}[H]",
"\\begin{minipage}[t]{0.48\\textwidth}",
" \\centering",
" \\includegraphics[width=\\textwidth]{../ylqd/${1:}}",
" \\caption*{2005年${2:}Quickbird影像}",
"\\end{minipage}",
"\\begin{minipage}[t]{0.48\\textwidth}",
" \\centering",
" \\includegraphics[width=\\textwidth]{../ylqd/${3:}}",
" \\caption*{2010年${4:}IKONOS影像}",
"\\end{minipage}\\",
"\\begin{minipage}[t]{0.9\\textwidth}",
" \\centering",
" \\includegraphics[width=0.5\\textwidth]{../ylqd/${5:}}",
" \\caption*{2021年${6:}JL-1影像}",
"\\end{minipage}",
"\\caption{${7:}高分辨率影像}",
"\\label{fig.qb.${8:}}",
"\\end{figure}"
],
"description": "high resolution"
},
"New Literature Section": {
"prefix": "litnote",
"body": [
"%%%%%%%%%%%%%%%%%%%%%%%%%%%%",
"\\newpage",
"\\section{${1:新文献标题}}",
"\\subsection{文献基本信息}",
"\\begin{itemize}[leftmargin=2em]",
" \\item 文献标题:${2:文献标题}",
" \\item 作者:${3:作者列表}",
" \\item 来源:${4:期刊名称、会议名称或出版机构}",
" \\item 发表日期:${5:发表日期}",
" \\item DOI 或链接:\\href{${6:http://example.com}}{${6:http://example.com}}",
"\\end{itemize}",
"",
"\\subsection{内容摘要}",
"\\subsubsection{研究背景}",
"\\begin{itemize}[leftmargin=2em]",
" \\item ${7:研究背景内容}",
"\\end{itemize}",
"",
"\\subsubsection{核心问题}",
"\\begin{itemize}[leftmargin=2em]",
" \\item ${8:核心问题内容}",
"\\end{itemize}",
"",
"\\subsubsection{方法与技术}",
"\\begin{itemize}[leftmargin=2em]",
" \\item ${9:方法与技术内容}",
"\\end{itemize}",
"",
"\\subsubsection{主要结论}",
"\\begin{itemize}[leftmargin=2em]",
" \\item ${10:主要结论内容}",
"\\end{itemize}",
"",
"\\subsubsection{结果意义}",
"\\begin{itemize}[leftmargin=2em]",
" \\item ${11:结果意义内容}",
"\\end{itemize}",
"",
"\\subsection{重要内容记录}",
"\\subsubsection{关键公式、图表或实验结果}",
"\\begin{itemize}[leftmargin=2em]",
" \\item ${12:实验结果内容}",
"\\end{itemize}",
"",
"\\subsubsection{重要理论或方法}",
"\\begin{itemize}[leftmargin=2em]",
" \\item ${13:重要理论内容}",
"\\end{itemize}",
"",
"\\subsubsection{引人深思的观点}",
"\\begin{itemize}[leftmargin=2em]",
" \\item ${14:观点内容}",
"\\end{itemize}",
"",
"\\subsection{个人评注}",
"\\subsubsection{文献优点}",
"\\begin{itemize}[leftmargin=2em]",
" \\item ${15:优点内容}",
"\\end{itemize}",
"",
"\\subsubsection{文献不足}",
"\\begin{itemize}[leftmargin=2em]",
" \\item ${16:不足内容}",
"\\end{itemize}",
"",
"\\subsubsection{对后续研究的启示}",
"\\begin{itemize}[leftmargin=2em]",
" \\item ${17:启示内容}",
"\\end{itemize}",
"",
"\\subsubsection{可能的改进方向}",
"\\begin{itemize}[leftmargin=2em]",
" \\item ${18:改进方向内容}",
"\\end{itemize}",
"\\subsubsection{相关参考文献}",
"\\printbibliography"
],
"description": "Insert a new literature note"
}
}
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 星辰大海!






