vscode 编写latex笔记

2022-06-24 09:07:49

记录设置全局中文字体及英文字体,调整页面大小到与 word 页面相同,设置常用的字体大小及行间距。

安装vscode插件

注意:需要确保本机已经安装好 Latex 环境,macbook 下可安装MacTex(安装好后会自带一个编辑器 TexShop)。

在 vscode 扩展中搜索LaTex Workshop 并安装。
LaTex Workshop

设置 XeLaTex 编译

默认的 Latex 编译方式是不支持中文的,所以需要修改插件编译方式,采用XeLaTex 来编译(支持 UTF-8)。在 vscode 的settings.json 中添加如下配置(用来修改编译方式以及删除一些编译过程中产生的额外文件等)。

"latex-workshop.latex.tools":[{"name":"xelatex","command":"xelatex","args":["-synctex=1","-interaction=nonstopmode","-file-line-error","-pdf","%DOC%"]},{"name":"latexmk","command":"latexmk","args":["-synctex=1","-interaction=nonstopmode","-file-line-error","-pdf","%DOC%"]},{"name":"pdflatex","command":"pdflatex","args":["-synctex=1","-interaction=nonstopmode","-file-line-error","%DOC%"]},{"name":"bibtex","command":"bibtex","args":["%DOCFILE%"]}],"latex-workshop.latex.recipes":[{"name":"xelatex","tools":["xelatex"]},{"name":"pdflatex -> bibtex -> pdflatex*2","tools":["pdflatex","bibtex","pdflatex","pdflatex"]}],"latex-workshop.latex.autoClean.run":"onBuilt","latex-workshop.latex.clean.fileTypes":["*.bbl","*.blg","*.idx","*.ind","*.lof","*.lot","*.out","*.toc","*.acn","*.acr","*.alg","*.glg","*.glo","*.gls","*.fls","*.log","*.fdb_latexmk","*.snm","*.synctex(busy)","*.synctex.gz(busy)","*.nav"],"latex-workshop.view.pdf.viewer":"tab",

设置全局字体

字体名称依操作系统的差异会有所不同,如下字体为 macbook 中的名称。

\usepackage{fontspec}  % 用于英文字体
\usepackage{xeCJK}  % 用于中文字体

% 设置英文字体为 Times New Roman
\setmainfont{Times New Roman}

% 设置中文字体为简宋,粗体为黑体,斜体为楷体
% 设置有些中文字体时,vscode会出现'CJK'警告,一般能够正确编译,暂未找到解决办法
\setCJKmainfont[BoldFont={Heiti SC Medium},ItalicFont={STKaiti}]{SimSong}

调整页面边距

虽然网上的资料都说 Latex 默认页面大小是 A4 字大小,但对比 word 中 A4 纸内容,同样的字体每行显示的字数 Latex 要少一些。下面的参数是 WPS 默认 A4 纸的边距数值。

\usepackage{geometry}

\geometry{ 
    a4paper,
    top=25.4mm,
    bottom=25.4mm,
    left=31.75mm,
    right=31.75mm
}

定义常用字体大小

定义几个常用的字体大小,从上到下依次为:三号,小三,四号,小四,五号,小五。(字体对应的磅数来源与百度,不一定百分百准确)

\documentclass[12pt]{article}  % 设置全局字体为小四

\newcommand{\sanhao}{\fontsize{16pt}{\baselineskip}{\selectfont}}
\newcommand{\xiaosanhao}{\fontsize{15pt}{\baselineskip}{\selectfont}}
\newcommand{\sihao}{\fontsize{14pt}{\baselineskip}{\selectfont}}
\newcommand{\xiaosihao}{\fontsize{12pt}{\baselineskip}{\selectfont}}
\newcommand{\wuhao}{\fontsize{10.5pt}{\baselineskip}{\selectfont}}
\newcommand{\xiaowuhao}{\fontsize{9pt}{\baselineskip}{\selectfont}}

\begin{document}
	% 某部分内容字体改成三号
	{\sanhao 文字内容}

	% 从当前开始往后,都改成三号字体
	\sanhao
\end{document}

设置行间距

暂时为找到设置行间距为多少磅的方式,以下设置为全局 1.5 倍行距,标题单倍行距。

\usepackage{setspace}

\renewcommand{\baselinestretch}{1.5}  % 全局 1.5 倍行距

\begin{document}
	\begin{spacing}{1.0}
		\section{标题}  % 标题为单倍行距
	\end{spacing}
	正文xxx
\end{document}

设置标题样式

网上提及修改标题的宏包为titlesec ,在终端输入texdoc titles 可查看该宏包说明文档。用到的命令如下:
\titleformat{<command>}[<shape>]{<format>}{<label >}{<sep>}{<before-code>}[<after-code>]

  • command: 填修改的标题命令,如\section,\part 等。
  • shape: 设置表示显示风格,默认风格为\section 样式(序号在前,标题内容在后)。
  • format: 填写标题格式,字体大小,是否加粗,居中等。
  • label: 设置标题编号内容。
  • sep: 设置标题内容与序号之间的空隙,必须填,可以为0。
  • before-code 在标题名称左边显示的内容。
  • after-code 在标题名称下新一行显示的内容。

如下设置可以将\section 设置成居中显示加粗三号字体的:第1章 导言。

\titleformat{\section}{\centering\sanhao\bfseries}{第 \thesection 章}{4pt}{}

效果

最终效果:
最终效果图

  • 作者:午夜涂猪男
  • 原文链接:https://blog.csdn.net/qq_19078137/article/details/123534553
    更新时间:2022-06-24 09:07:49