(技术积累)Latex正式入门

[技术积累]Latex正式入门

本科云计算课程粗浅地使用过Latex,这次算一次Latex的正式入门,学习+摘录整理。

LaTeX是一种“非所见即所得”的排版系统,用户需要输入特定的代码,保存在后缀为.tex的文件中,通过编译得到所需的pdf文件。对于以下的例子:

\documentclass{article}
\usepackage{graphicx} % Required for inserting images

\title{Latex Learning}
\author{shaw wu}
\date{October 2023}

\begin{document}

\maketitle

\section{Introduction}
Hello, world
\end{document}

编译结果为:

其中:

  1. \documentclass:
\documentclass{article}

表示文件类型,不同的文件类型,编写的过程中也会有一定的差异,如果直接修改文件类型的话,甚至会报错。

TeX有多种文档类型可选,笔者较常用的有如下几种类型:

  • 对于英文,可以用bookarticlebeamer
  • 对于中文,可以用ctexbookctexartctexbeamer,这些类型自带了对中文的支持。

如果要设置默认字体大小、纸张大小、打印方式等,都可以通过\documentclass修改:

% 设置默认字体大小为12pt,纸张大小为A4,单面打印
\documentclass[12pt, a4paper, oneside]{ctexart}
  1. \document:

文件的正文部分需要放入document环境中,在document环境外的部分不会出现在文件中。

  1. \usepackage:

为了完成一些功能(如定理环境),还需要在导言区,也即document环境之前加载宏包。加载宏包的代码是\usepackage{}。与数学公式与定理环境相关的宏包为amsmathamsthmamssymb,用于插入图片的宏包为graphicx,代码如下:

\usepackage{amsmath, amsthm, amssymb, graphicx}

另外,在加载宏包时还可以设置基本参数,如使用超链接宏包hyperref,可以设置引用的颜色为黑色等,代码如下:

\usepackage[bookmarks=true, colorlinks, citecolor=blue, linkcolor=black]{hyperref}
  1. \title、\author:

标题可以用\title{}设置,作者可以用\author设置,日期可以用\date{}设置,这些都需要放在导言区。为了在文档中显示标题信息,需要使用\maketitle


在正文中,Latex默认段落首行缩进,相邻的两行在编译时仍然会视为同一段, 另起一段的方式是使用一行相隔,例如:

\begin{document}

\maketitle

\section{Introduction}
Hello, world
Hello, world

Hello, world

Hello, world
\end{document}

编译结果:

​ 另起一页的方式是:

\newpage

​ 字体的特殊形式:

字体 命令
直立 \textup{}
意大利 \textit{}
倾斜 \textsl{}
小型大写 \textsc{}
加宽加粗 \textbf{}

插入图片:

\begin{figure}[htbp]
    \centering
    \includegraphics[width=8cm]{图片.jpg}
    \caption{图片标题}
\end{figure}

其中,[htbp]的作用是自动选择插入图片的最优位置,\centering设置让图片居中,[width=8cm]设置了图片的宽度为8cm,\caption{}用于设置图片的标题。

LaTeX中表格的插入较为麻烦,可以直接使用Create LaTeX tables online – TablesGenerator.com来生成。建议使用如下方式:

\begin{table}[htbp]
    \centering
    \caption{表格标题}
    \begin{tabular}{ccc}
        1 & 2 & 3 \\
        4 & 5 & 6 \\
        7 & 8 & 9
    \end{tabular}
\end{table}

LaTeX中的列表环境包含无序列表itemize、有序列表enumerate和描述description,以enumerate为例,用法如下:

\begin{enumerate}
    \item 这是第一点; 
    \item 这是第二点;
    \item 这是第三点. 
\end{enumerate}

另外,也可以自定义\item的样式:

\begin{enumerate}
    \item[(1)] 这是第一点; 
    \item[(2)] 这是第二点;
    \item[(3)] 这是第三点. 
\end{enumerate}

设置行间距,可以使用如下代码:

\linespread{1.5}

默认的页码编码方式是阿拉伯数字,用户也可以自己设置为小写罗马数字:

\pagenumbering{roman}

Reference