Latex学习


一、Markdown与Latex的区别

latex是纯学术风格,写paper写书用;markdown是程序员风格,写笔记贴代码片段用 简单说,latex适合长篇.精致,比如数学公式.图片位置调整.表格样式调整.而markdown就是粗线条,简易编辑。本篇文章就是以markdown语法进行编写。

markdown语法的官方教程

二、Latex的背景

LaTeX(LATEX,音译“拉泰赫”)是一种基于ΤΕΧ的排版系统,由美国计算机学家莱斯利·兰伯特(Leslie Lamport)在20世纪80年代初期开发,利用这种格式,即使使用者没有排版和程序设计的知识也可以充分发挥由TeX所提供的强大功能,能在几天、甚至几小时内生成很多具有书籍质量的印刷品。对于生成复杂表格和数学公式,这一点表现得尤为突出。因此它非常适用于生成高印刷质量的科技和数学类文档。这个系统同样适用于生成从简单的信件到完整书籍的所有其他种类的文档。

Latex新手指南:The Short Introduction to LaTeX2e (Chinese Simplified)

三、Latex语法基础:命令与环境

1.命令与环境

命令

什么是命令

不同于其他编程语言(C/C++, Python等)会使用关键词,函数和类来实现程序,Latex语法中大多以命令的形式存在
而每一个命令都会有具体的功能,如标题制作,目录制作,或者是设置文档的编码格式等等

命令的格式
Latex中的命令具体有以下三种形式:

1
2
3
无参数
\commad
\maketitle
1
2
有n个必须参数
\command{参数1}{参数2}...{参数n}
1
2
有n个可选参数
\command[可选参数1]...[可选参数n]

Latex语法举例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
%===============文章体裁:论文================
\documentclass[UTF8]{article}

%=================导言部分===================
%导入包
\usepackage{ctex}
\usepackage{authblk}


%标题制作
\title{论文示例}
\author{孙新斌,\ Jack\ Wang}
\affil{College of Computer Science \& Technology, QDU}
\date{\today}


%=================正式开始书写===============
\begin{document}

\maketitle

\section{摘要}
这是一个LaTex 论文示例,由Texstudio编写。


\end{document}

示例如图所示

1
2
3
4
5
6
7
8
9
10
11
12
13
\documentclass[]{}
\usepackage{}
\usepackage{}
\title{}
\author{}
\affil{}
\date{}
\today
\begin{}
\maketitle
\section{}
\end{}
%常用命令

环境

什么是环境
我们编写的文档内容为了避免代码和我们书写的正文内容混杂,我们从夹杂代码和正文内容的纯文本的连续上下文中划分出部分空间,这部分空间将作为独立的整体,我们可以将正文内容写在这些空出来的部分中

对于这些空间,我们可以提前使用一些命令,设定好这些空间的环境,即设定好这些空间的环境,即编译好之后最终的展示格式,因此环境其实就是我们为特定部分的正文设定的格式,这个特定部分可以是后面的全文,也可以是正文的部分,例如一段话,一张图片等等

例如下面的例子(\\\表示换行)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
%===============文章体裁:论文================
\documentclass[UTF8]{article}
\usepackage{ctex}


\begin{document}


这是一个有article,ctex,document环境的语句\\
%下面的this具有article,ctex,documnet和textbf环境
{\textbf {this}}\\
这句话也只有documentclass和document环境,没有this环境


\end{document}

正如代码中提到的,我们的命令其实可以分为两种,第一种是产生效果的命令,例如制作标题的\maketitle,另外一种就是对后文添加环境的命令

环境的作用和作用域

就像诸多编程语言中变量具有作用域,我们的环境其实也具有作用域,具体来说就是环境可以影响的正文的范围

有的环境的作用域是全文,例如上面例子中一开始的\documentclass[UTF8]{article}就规定了文章的体裁是论文,使用的编码方式是UTF-8,其作用域就是全文.

接下来的\usepackage{ctex}指明我们将使用ctex这个包/宏包. 包/宏包类似于编程语言的库,库中有很多写好的函数给我们使用,宏包中也有很多能够产生各种效果的命令.这里使用的ctex宏包的一个功能就是提供中文的显示和排版.所以\usepackage{ctex}这个命令的作用域也是全文.

\begin\end命令用于开始和结束特定的环境,我们这里使用的\begin{document}和\end{document}就是开始和结束名为document的环境,两个命令之间的内容都将受到环境document的作用. document的作用就是展示文档, 在document环境的作用域范围内的内容在编译后都将会展示出来.在document环境内的内容才会被展示, 而在document环境外的内容不会被展示

2.隔离上下文

接下来我们继续向下看刚才的例子,我们用上面的例子引出 隔离上下文

我们编写了这样一句话{\textbf {this}},命令\textbf的作用是加粗处于其作用域的文字.

我们首先看下\textbf的作用域

示例如图所示

我们能够发现第一句仅加粗了t,而第二句加粗了整句话,而从\textbf这句话本身来看,其作用域就是其后紧接着的一个字符(注意由于其只能作用一个字符,因此如果我们加粗的第一个字符是中文的话就会报错,因此中文汉字作为第一个字符就会报错)

在正式编写的时候,我们通常将不同的段落放在不同的{ }中来管理环境,例如(空行表示另起一段)

四、Latex语法基础:Latex文件格式简介

Latex基本文件的基本结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
\documentclass[12pt, letterpaper]{article}%用于设置LaTex文件所生成文档的格式
%正文字体大小为12pt, 页面规格是A4, 使用article文档格式

\usepackage[utf8]{inputenc}%设置在编译LaTex文件时要导入的扩展包
\usepackage{comment}

% Title
\title{Document Title}
\author{Nobody \thanks{Somebody}}
\date{Some Date}
%==================导言=======================
\begin{document}

\begin{titlepage}
\maketitle%生成封面
\end{titlepage}

\tableofcontents%生成目录


\begin{abstract}
This is a simple paragraph at the beginning of the
document. A brief introduction about the main subject.
\end{abstract}

First document. This is a simple example, with no
extra parameters or packages included.

% Comments
\begin{comment}
This text won't show up in the compiled pdf
this is just a multi-line comment. Useful
to, for instance, comment out slow-rendering
while working on the draft.
\end{comment}

\end{document}

# $ % ^ & _ { } \ ——这些字符(reserved characters)在Latex中有特殊的意义,要想在生成的文档中显示这些字符,Latex文档中这些字符前加反斜杠”\\’’

$\backslash$ 输出反斜杠(因为两个反斜杠”\\”在Latex中是换行命令)

~波浪线(tidle)在LaTeX中是插入空格命令,可用数学公式环境的$ \sim $向TeX文档中插入波浪线.

1
2
3
\pagenumbering{arabic}%设置页码数字类型
\setcounter{page}{page number}%设置页码
\thispagestyle{empty}%当前页不标页码

eg.如果在封面不标记页码,目录页使用小写罗马数字标记页码,正文部分使用阿拉伯数字标记页码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
\begin{document}

%% Making title pate
\begin{titlepage}
\maketitle
\thispagestyle{empty}
\end{titlepage}

%% Contents
\pagenumbering{roman}
\tableofcontents

\newpage
\pagenumbering{arabic}
\setcounter{page}{1}
======正文======

版面设置

段落行间距

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
\documentclass[UTF8]{article}
\usepackage{setspace}%使用该扩展包

\begin{document}

%%双倍行间距
\begin{spacing}{2.0}
段落内容。
\end{spacing}

%%单倍行间距
\begin{spacing}{1.0}
段落内容。
\end{spacing}

\end{document}

段落间空白

1
2
\hspace{1cm}
\vspace{5pt}

居中

1
2
3
\begin{center} 
...
\end{center}

左、右对齐

1
2
3
4
5
6
7
\begin{flushleft}
...
\end{flushleft}

\begin{flushright}
...
\end{flushright}

样例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
\begin{center} 
登鹳雀楼 \\
~~~~~~~~ {\footnotesize 王之涣} \\
\hfill \\
白日依山尽 \\
黄河入海流 \\
欲穷千里目 \\
更上一层楼 \\
\end{center}

\begin{flushleft}
这一行在左侧 \\
这一行也要在左侧 \\
\end{flushleft}

\begin{flushright}
这一行在右侧 \\
这一行也要在右侧 \\
\end{flushright}

verbatim 居中显示及其作用

verbatim环境(抄录环境)使LaTex源文件的内容原样呈现于最终文档。这些内容不受center, flushleft, flushright等命令的影响。若想让verbatim环境中内容居中显示,需要使用 verbatimbox等扩展包.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
%首先在导言区加入:
\usepackage{verbatimbox}
%正文中示例:
\begin{verbbox}
#include <stdio.h>

void main()
{
printf("Hello World! \n");
}
\end{verbbox}

\begin{figure}[ht]
\centering %% 居中显示
\theverbbox
\end{figure}

线框

使用\fbox给文字加线框; 使用\parbox给段落添加线框.

样例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
\fbox{A}

\hfill

\fbox{
Use $\backslash fbox$ environment for creating line box.
}

\begin{center}
\fbox{
Box lies in center. However
}
\end{center}

\begin{center}
\fbox{
\parbox{\textwidth}{
\begin{center}
This is a \\
Full size box \\
\end{center}
}
}
\end{center}

\begin{center}
\fbox{
\parbox{0.4\textwidth}{
\begin{center}
This is a \\
Small size box \\
\end{center}
}
}
\end{center}

图片插入及引用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
\usepackage[pdftex]{graphicx}
% 设置图片文件存放路径
\graphicspath{{../figures}

\begin{document}
% 在正文中引用图片时使用\ref
In Figure \ref{fig:foo}
\begin{figure}
%设置对齐格式
\centering %图片居于页面中部
%指定图形大小和图形名称
\includegraphics [width=0.8,height=2.5]{foo.png}
%设置标题
\caption{Foo}
%设置图形引用名称
\label{fig:foo}
\end{figure}

\end{document}

表格

1
2
3
4
5
6
7
8
9
10
11
12
13
\begin{tabular}{llr}%定义表格
\hline
\multicolumn{2}{c}{Item} \\
\cline{1-2}
Animal & Description & Price (\$) \\
\hline
Gnat & per gram & 13.65 \\
& each & 0.01 \\
Gnu & stuffed & 92.50 \\
Emu & stuffed & 33.33 \\
Armadillo & frozen & 8.99 \\
\hline
\end{tabular}

表格中行间距

1
\renewcommand{\arraystretch}{1.5}%\arraystretch命令可以设置行间距(默认值是1.0)

使用\noalign{\smallskip} 也可以改变行间距. \smallskip 命令等价于\vspace{smallskipamount}。smallskipamount的大小依赖于文档类型. 与\smallskip 命令类似的还有\medskip,\bigskip . 另外,也可以在每一行尾的 换行符 后设置行间距。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
\begin{tabular}{c}
\hline
Normal \\
\hline
smallskip(under the characters) \\ \noalign{\smallskip}
\hline \noalign{\smallskip}
smallskip(over the characters) \\
\hline \noalign{\medskip}
medskip \\
\hline \noalign{\bigskip}
bigskip \\
\hline
0.5cm \\[0.5cm]
\hline
\end{tabular}

三线表样例

Latex中默认的线条宽度是0.4pt, 如果想要使用粗一点的线条,可以使用 booktabs环境包. 这需要在Latex文档的导言部分添加命令:

1
\usepackage{booktabs}  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
\begin{table}[h]
\centering
\begin{tabular}{ccc}
\toprule
Name & ID & Gender\\
\midrule
Tom & 001& Male\\
Rose & 002& Female\\
\bottomrule
\end{tabular}
\caption{这是一张三线表}
\end{table}
%其中\toprule 命令是画出表格最上边的一条粗实线(rule). \bottomrule命令是画出表格最下边的一条粗实线.
%\midrule 命令是画出表格中间的细实线。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
\documentclass[a4paper,oneside,12pt]{article}

\usepackage{ctex}
\usepackage{authblk}
\usepackage{graphicx}
\usepackage{CJK}
\usepackage{indentfirst}

\usepackage[a4paper]{geometry}
\geometry{left=2cm, right=2cm, top=3cm, bottom=3.5cm,}
\setlength{\footskip}{3cm}
% 另外一种调用方式
% \usepackage[paperwidth=5.5in, paperheight=8.5in]{geometry}


\begin{document}


\thispagestyle{empty}
\begin{center}
\includegraphics{logo1}
\end{center}


\vspace{50pt}

\begin{center}
\Huge 2022年秋季学期\\
\vspace{50pt}
{\fontsize{36pt}{0pt}操作系统课程设计报告}%小初

\vspace{120pt}

\huge 组长:\underline{\qquad\quad 孙新斌\qquad\quad}\\
\huge 成员:\underline{\qquad\quad 李明阳\qquad\quad}\\
\huge 成员:\underline{\qquad\quad 刘泳良\qquad\quad}\\
\end{center}



\newpage
\pagenumbering{arabic}

\begin{center}
{\fontsize{22pt}{22pt}课程设计选题一\quad 多线程单词统计}\\%二号
\end{center}

\section*{\fontsize{16pt}{16pt}一、设计要求}%三号

\begin{itemize}
\item[]
{\fontsize{14pt}{14pt}\selectfont 1、从网上下载一些英文小说,用多线程实现单词总数的统计}\\
{\fontsize{14pt}{14pt}\selectfont 2、用多线程统计单词出现频率并从中找出Top-N热词}\\
{\fontsize{14pt}{14pt}\selectfont 3、在以上多线程程序中直观展示出多线程并发调度的过程}\\
{\fontsize{14pt}{14pt}\selectfont 4、用单线程实现以上功能,并比较单线程和多线程的时间效率}
\end{itemize}

\setlength{\parindent}{2em}

\section*{\fontsize{16pt}{16pt}二、背景知识及设计思路}%三号

\begin{itemize}
\item[]
{\fontsize{14pt}{14pt}\selectfont 设计思路:}\\
{\fontsize{14pt}{14pt}\selectfont 1、根据文件的数量创建多个线程,一个线程统计一个文件中的单词}\\
{\fontsize{14pt}{14pt}\selectfont 2、英文单词为由英文字母或数字组成的字符序列,大小写不敏感}\\
{\fontsize{14pt}{14pt}\selectfont 3、如果两个线程需要安全地共享一个公共计数器,需要把公共计数器加锁}
\end{itemize}

\section*{\fontsize{16pt}{16pt}三、设计方案}%三号

\begin{itemize}
\item[]
{\fontsize{14pt}{14pt}\selectfont 1.数据结构设计}
\end{itemize}

\begin{itemize}
\item[]
{\fontsize{14pt}{14pt}\selectfont 2.程序结构设计(模块图,流程图,函数调用关系图)}
\end{itemize}

\begin{itemize}
\item[]
{\fontsize{14.0pt}{14pt}\selectfont 3.用到的Linux系统调用函数说明}
\end{itemize}

\section*{\fontsize{16pt}{16pt}四、设计源代码}%三号

\begin{itemize}
\item[]
{\fontsize{14pt}{14pt}\selectfont
1、xxxx}

\end{itemize}

\section*{\fontsize{16pt}{16pt}五、调试方案及调试过程问题分析}%三号

\begin{itemize}
\item[]
{\fontsize{14pt}{14pt}\selectfont 1、xxxx}

\end{itemize}

\section*{\fontsize{16pt}{16pt}六、运行结果与分析}%三号

\begin{itemize}
\item[]
{\fontsize{14pt}{14pt}\selectfont 1、xxxx}

\end{itemize}

\section*{\fontsize{16pt}{16pt}七、总结}%三号

\begin{itemize}
\item[]
{\fontsize{14pt}{14pt}\selectfont 1、xxxx}

\end{itemize}




\end{document}