声明:以下内容部分整理自B站“奇乐编程学院”的视频BV11h41127FD,仅供个人存档使用。

LaTeX是一款开源免费、应用广泛的排版工具,用于对文字、图片和公式等内容进行精确复杂的排版,并能确保全文各个章节的一致性。类似于Markdown,LaTeX也是一种将内容和排版区分开的标记语言。

同时,LaTeX提供相当数量的宏包,作为对其功能的扩充。其用途囊括了论文、书籍、简历、乐谱等的排版。


快速入门

下载与安装

常用的Tex发行版一般有两个:TeX Live和MiKTeX,前者的优势是包覆盖全面,但同时占用空间大;后者反之,占用小,但本地缺少包时需联网下载。

对于想要快速尝试的用户,overleaf作为一款可以线上免费使用的LaTeX编辑器是一个不错的选择(https://www.overleaf.com/)。

编辑器

  • TeX Live携带一款编辑器,名为TeXworks。
  • VScode亦可以使用。

VScode的环境配置

[TBD]

基本语法

基本格式

\command[code]{optional_parameters}

LaTeX中所有命令均以\开头,后接[]指定编码类型(UTF8为LaTeX默认编码格式),{}作为可选的参数。

如:\documentclass{article} ,此处的\documentclass 为指定文档类型的命令,可选择的参数有以下几个常见的:article代表文章,book代表书籍,report代表报告,beamer代表幻灯片,ctexart代表中英混输文档。

前言区(Preamble)

\begin{document}命令前的部分被称作“前言”,类比HTML语言中的head标签,可以设置文档格式、页面尺寸和需导入的宏包等。

  • \title{} 用于设置文档标题。
  • \author{} 用于设置文档作者。
  • \date{} 用于指定文档修改日期,可使用复合命令\date{\today}自动生成当天日期。
  • \maketitle 添加在正文区,可以在其插入位置生成文档标题、作者和日期等前言信息。

正文区(Body)

正文区介于\begin{document}\end{document}命令之间,在此区域输入的文字都会被排版到文档中。

基础格式化命令

  • \textbf{} 加粗(Bold font)
  • \textit{} 斜体(italic)
  • \underline{} 下划线
  • 新段落添加 点按两次回车(enter)以实现换自然段的渲染

章节划分

  • \part{} 添加新部,在{}中输入部名(最大一级)
  • \chapter{} 添加新章,在{}中输入章名(中级)
  • \section{} 添加新节,在{}中输入节名(最小一级)
  • \subsection{} 二级子节
  • \subsubsection{} 三级子节(最高)

插入图片

  • \usepackage{graphicx} 在插入图片前,必须在前言部分使用上述命令调用graphicx包。
  • \includegraphics[parameter]{name} 在正文中输入的插入图片指令,{}中输入文件名,可省略扩展名部分。
  • 可在{}前添加[]以包含更多参数,如[width=0.5\textwidth] 可调整图片大小。

添加标题、居中显示

  • 使用\begin{figure}\end{figure}嵌套在正文中,并添加\caption命令,见下例:
\begin{document}

\begin{figure}
\includegraphics[width=0.5\textwidth]{image}
\caption{this is an image about xxx}
\end{figure}

\end{document}
  • \begin{figure}\end{figure}之间也可以加入\centering命令使图片居中显示。

列表

无序列表/bullet points

  • document环境下使用itemize环境:\begin{itemize}\end{itemize}
  • 使用\item xxx编辑列表内容。

有序列表

  • document环境下使用enumerate环境。
  • 使用\item xxx编辑列表内容。

数学公式

行内公式(inline equation)

需要写在两个美元符号$ ... $之间:

\begin{document}

爱因斯坦在1905年发现的质能守恒方程为:$E=mc^2$。

\end{document}

单独成行的公式

可以使用equation环境或是\[...\]

\begin{document}

爱因斯坦在1905年发现的质能守恒方程为:

\begin{equation}
E=mc^2
\end{equation}

\end{document}
\begin{document}

爱因斯坦在1905年发现的质能守恒方程为:

\[
E=mc^2
\]

\end{document}

复杂公式

表格

基础表格

可以使用tabular环境:

\begin{document}

\begin{tabular}{ c c c }
cell 1 & cell 2 & cell 3 \\
cell 4 & cell 5 & cell 6 \\
cell 7 & cell 8 & cell 9
\end{tabular}

\end{document}
  • 使用{}传入信息的参数,上述的{ c c c }表示表格有三列,每一列居中对齐。

若使用l代替c,则表示左对齐,使用r表示右对齐。

  • 在环境中使用如上格式编辑表格内容,单元格之间使用&隔开,每行末尾使用\\作为分隔符。

添加横竖线

\begin{document}

\begin{tabular}{|c|c|c|}
\hline
cell 1 & cell 2 & cell 3 \\
\hline
cell 4 & cell 5 & cell 6 \\
\hline
cell 7 & cell 8 & cell 9
\hline
\end{tabular}

\end{document}
  • 添加竖线和横线的命令格式如上所示(在列之间加竖线|,在行之间加上\hline)。
  • \hline的一个变体是,输入两次(即\hline\hline)可添加双横线效果。

指定列宽

{|c|c|c|}中的c改为p{},在{}中输入列宽,如2cm

添加标题

  • tabular环境嵌套在table环境中,并输入\caption{}命令。
  • 也可以加入\center命令使图片居中显示。如下所示:
\begin{document}

\begin{table}
\center
\begin{tabular}{|c|c|c|}
\hline
cell 1 & cell 2 & cell 3 \\
\hline
cell 4 & cell 5 & cell 6 \\
\hline
cell 7 & cell 8 & cell 9
\hline
\end{tabular}
\caption{标题}
\end{table}

\end{document}

学习资料

一份不太简短的LaTeX介绍:https://github.com/CTeX-org/lshort-zh-cn

最后修改:2025 年 06 月 29 日
喵~