LaTeX

来自寒夜雨的个人维基
WhiteWallClosetFun留言 | 贡献2025年7月30日 (三) 00:58的版本
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转到导航 跳转到搜索

LaTeX是一个科研领域常用的排版系统。[1]

在VSCode编辑器上配置LaTeX编写系统

需要安装的软件:

具体的步骤为:

  1. 安装MikTeX。
  2. 安装VSCode编辑器。
  3. 在VSCode编辑器中,安装LaTeX Workshop插件。
  4. 按住Ctrl+,组合键,搜索“latex tool”,并打开settings.json文件。
  5. 在JSON块中,增加下面的配置行:
  6. // 禁止自动编译
    "latex-workshop.latex.autoBuild.run": "never",
    // 编译后自动清理无用文件
    "latex-workshop.latex.autoClean.run": "onBuilt",
    "latex-workshop.latex.clean.subfolder.enabled": true,
    // 自动使用上一次的编译器
    "latex-workshop.latex.recipe.default": "lastUsed",
    // 根据使用的包自动补全
    "latex-workshop.intellisense.package.enabled": true,
    
  7. latex-workshop.latex.toolsJSON块中,增加下面的配置行。这些配置文件的目的是引入bibtex工具。
  8. {
        "name": "bibtex",
        "command": "bibtex",
        "args": [
            "%DOCFILE%"
        ],
        "env": {}
    },
    
  9. latex-worksop.latex.recipesJSON块中,增加下面的配置行。这些配置的引入目的是便于中文编译,以及最终引用格式的生成。
  10. {
        "name": "xelatex -> bibtex -> xelatex*2",
        "tools": [
            "xelatex",
            "bibtex",
            "xelatex",
            "xelatex"
        ]
    },
    

一个示例中文LaTeX模板

\documentclass[%12pt 文字大小
]{article}
\usepackage{amsmath,amssymb}
\linespread{1.5}
\usepackage[UTF8]{ctex}
\usepackage{lmodern}
\usepackage{iftex}
\usepackage{indentfirst}
\usepackage{setspace}
\usepackage{enumitem} 
\usepackage{graphicx}
\usepackage{listings}
\usepackage{float} %指定图片位置
\usepackage{subfigure}%并排子图 共享标题 有子标题
\usepackage{hyperref}
\usepackage{tcolorbox}
\usepackage[backend=bibtex,
style=numeric,
sorting=none]{biblatex}
\usepackage{color}
\usepackage{xcolor}
\usepackage{booktabs}
\tcbuselibrary{skins,breakable}

\usepackage[lined,boxed,commentsnumbered]{algorithm2e}
\hypersetup{hidelinks,
	colorlinks=true,
	allcolors=black,
	pdfstartview=Fit,
	breaklinks=true}

\lstset{
	basicstyle          =   \sffamily,          % 基本代码风格
	keywordstyle        =   \bfseries,          % 关键字风格
	commentstyle        =   \rmfamily\itshape,  % 注释的风格,斜体
	stringstyle         =   \ttfamily,  % 字符串风格
	flexiblecolumns,                % 别问为什么,加上这个
	numbers             =   left,   % 行号的位置在左边
	showspaces          =   false,  % 是否显示空格,显示了有点乱,所以不现实了
	numberstyle         =   \zihao{-5}\ttfamily,    % 行号的样式,小五号,tt等宽字体
	showstringspaces    =   false,
	captionpos          =   t,      % 这段代码的名字所呈现的位置,t指的是top上面
	frame               =   lrtb,   % 显示边框
	breaklines          =   true,
}


\setlength{\parindent}{2em}
% 定义
\def\degree{{}^{\circ}}
% 中英文摘要格式

% 标题、作者、日期
\title{\textbf{\huge{标题}}}
\author{作者1,作者2, 作者3\
	所属单位}
\date{} % 这一行用来去掉默认的日期显示
%\newcommand{\supercite}[1]{\textsuperscript{\cite{#1}}}
\renewcommand{\contentsname}{目录} % 目录变为中文
\renewcommand{\refname}{参考资料}
\author{}
\date{}
\title{}
\begin{document}
	\maketitle
	\newpage
	\tableofcontents
	\newpage

\end{document}

编写贴士

跨栏表格的编写

跨栏表格列下面还包含子列的表格,编写方式为:

\begin{table}[!ht]
  \caption{\textbf{Caption}.}
  \smallskip
  \label{caption}
  \centering
  \begin{tabular}{cccccc}
    \toprule
    \multicolumn{1}{c}{\textbf{Main1}} & 
    \multicolumn{2}{c}{\textbf{Main2}} &
    \multicolumn{3}{c}{\textbf{Main3}}  
    \\
    \midrule
     & \textbf{Sub1} & \textbf{Sub2} & \textbf{Sub3} & \textbf{Sub4} & \textbf{Sub5} \\
    \midrule
    \textbf{Element1} & & & & & \\
    \bottomrule
  \end{tabular}
\end{table}

问题解决

无法搜寻到引用相关命令

这个问题的表现形式通常为下面的报错:

I found no \citation commands---while reading file myFile.aux
I found no \bibdata command---while reading file myFile.aux
I found no \bibstyle command---while reading file myFile.aux

解决方法为,在.tex文件中,加入或修改:

\usepackage[backend=bibtex]{biblatex}

错误的引用命令

这个问题的表现形式通常为下面的报错:

! Package biblatex Error: '\bibliographystyle' invalid.
l.260 \bibliographystyle{unsrt} Use the package option 'style' instead. I'm ignoring this command. ! LaTeX Error: Can be used only in preamble.

解决方法为,在.tex文件中,加入或修改:

\addbibresource{document.bib} % 应当在\begin{document}前添加引用文件。

Missing $ inserted

这个问题的表现形式通常为下面的报错:

Missing $ inserted

这个问题出现的原因是转义字符的问题,需要检查文档,为_等字符添加转义:

\par{
	This is a test_sentence. % 错误
	This is a test\_sentence. % 正确
}

图片模糊不清

一个好习惯是,想要向LaTeX文档中嵌入图片时,请将图片设置为.pdf格式,而非pngjpg等格式。这是因为,除了PDF格式,其它的格式往往面临着放大发虚的问题。

参考资料