写在前面
为了方便展示,我直接将所有的LaTeX代码放进了该文章,如有需要即可将其复制到.tex文件中生成pdf,将会更有利于您学习。同时我也加入了每个知识点对应的LaTeX代码。
LaTeX基础排版代码
% 导言区
\documentclass{article} % book, report, letter
\usepackage{ctex}
\usepackage{geometry}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{hyperref} % 生成带链接的目录
\hypersetup{
citecolor=black,
filecolor=black,
urlcolor=black
}
\graphicspath{{imgs/}}
\input{tmp/code_tmp.tex}
\geometry{left=3.18cm, right=3.18cm, top=2.54cm, bottom=2.54cm}
\title{Learning \LaTeX}
\author{Deze Zhao}
\date{\today}
% 正文区
\begin{document}
% 生成标题
\maketitle
% 设置目录页码格式是罗马数字
\pagenumbering{Roman}
% 生成目录
\tableofcontents
% 插入新页
\newpage
% 设置正文页码格式是阿拉伯数字
\pagenumbering{arabic}
\section{字体}
\subsection{字体族设置}
% 插入LaTeX代码块
\begin{lstlisting}[style=TeX]
% 字体族设置
{\textrm{Roman Family}} {\textsf{Sans Serif Family}} {\texttt{Typewriter Family}}
{\rmfamily{Roman Family}} {\sffamily{Sans Serif Family}} {\ttfamily{Typewriter Family}}
\end{lstlisting}
% 字体族设置(罗马字体 无衬线字体 打字机字体)
{\textrm{Roman Family}} {\textsf{Sans Serif Family}} {\texttt{Typewriter Family}}
{\rmfamily{Roman Family}} {\sffamily{Sans Serif Family}} {\ttfamily{Typewriter Family}}
\subsection{字体粗细设置}
% 插入LaTeX代码块
\begin{lstlisting}[style=TeX]
%字体粗细 宽窄设置
{\textmd{Medium}} {\textbf{Bold Face}}
{\mdseries{Medium}} {\bfseries{Bold Face}}
\end{lstlisting}
%字体粗细 宽窄设置
{\textmd{Medium}} {\textbf{Bold Face}}
{\mdseries{Medium}} {\bfseries{Bold Face}}
\subsection{字体形状设置}
% 插入LaTeX代码块
\begin{lstlisting}[style=TeX]
% 字体形状设置 直体 斜体 伪斜体 小型大写
{\textup{Upright shape}} {\textit{Italic shape}}
{\textsl{Slanted shape}} {\textsc{Small Caps Shape}}
% 第二种设置方法
{\upshape{Upright shape}} {\itshape{Italic shape}}
{\slshape{Slanted shape}} {\scshape{Small Caps Shape}}
\end{lstlisting}
% 字体形状设置 直体 斜体 伪斜体 小型大写
{\textup{Upright shape}} {\textit{Italic shape}}
{\textsl{Slanted shape}} {\textsc{Small Caps Shape}}
% 第二种设置方法
{\upshape{Upright shape}} {\itshape{Italic shape}}
{\slshape{Slanted shape}} {\scshape{Small Caps Shape}}
\subsection{中文字体设置}
% 插入LaTeX代码块
\begin{lstlisting}[style=TeX]
{\songti 宋体} \quad {\kaishu 楷书} \quad
{\fangsong 仿宋} \quad {\heiti 黑体}
\end{lstlisting}
{\songti 宋体} \quad {\kaishu 楷书} \quad {\fangsong 仿宋} \quad
{\heiti 黑体}
\section{特殊符号}
\subsection{空白字符}
\begin{lstlisting}[style=TeX]
% 在英文中多个空格算为一个空格
% 中文中多个空格被忽略
% 自动缩进 不能用空格代替
% 汉字与其他字符的间距由XeLatex自动处理
% 禁止使用中文全角空格
dsncdjk sndkj vsdnk cnsdncdsjk cndjcndj hello nihao woshi zdz!
在本次讲座中,教授主要 从粒计算(Granular Computing, GrC)概念出发,
借助其研究的该领域经典理论模型——粗糙集,为我们阐述了粒计算在“不精确问题”领域的
应用方式及对人工智能领域的贡献。同时也为我们介绍了粒计算从无到有的发展历程、
人工智能领域的相关概念。
% 加入空格
% 1em
a\quad b
% 2em
a\qquad b
% 约1/6em
a\, b==a\thinspace b
% 0.5em
a\enspace b
% 空格
a\ b
\end{lstlisting}
% 在英文中多个空格算为一个空格
% 中文中多个空格被忽略
% 自动缩进 不能用空格代替
% 汉字与其他字符的间距由XeLatex自动处理
% 禁止使用中文全角空格
dsncdjk sndkj vsdnk cnsdncdsjk cndjcndj hello nihao woshi zdz!
在本次讲座中,教授主要 从粒计算(Granular Computing, GrC)概念出发,
借助其研究的该领域经典理论模型——粗糙集,为我们阐述了粒计算在“不精确问题”领域的
应用方式及对人工智能领域的贡献。同时也为我们介绍了粒计算从无到有的发展历程、
人工智能领域的相关概念。
% 加入空格
% 1em
a\quad b
% 2em
a\qquad b
% 约1/6em
a\, b
% 0.5em
a\enspace b
% 空格
a\ b
\subsection{\LaTeX 控制符}
\begin{lstlisting}[style=TeX]
% 反斜杠用\textbackslash 产生 \\表示换行 \_{}防止_与后面的字符作用,用{}切断
\# \qquad \$ \qquad \%
\qquad \{ \qquad \} \qquad \_{} \qquad \^{} \qquad \textbackslash \qquad \&
\end{lstlisting}
% 反斜杠用\textbackslash 产生 \\表示换行 \_{}防止_与后面的字符作用,用{}切断
\# \qquad \$ \qquad \% \qquad \{ \qquad \} \qquad \_{} \qquad \^{} \qquad \textbackslash \qquad \&
\subsection{排版符号}
\begin{lstlisting}[style=TeX]
\S \qquad \P \qquad \dag \qquad \ddag \qquad \copyright \qquad \pounds
\end{lstlisting}
\S \qquad \P \qquad \dag \qquad \ddag \qquad \copyright \qquad \pounds
\subsection{\TeX 标志符号}
\begin{lstlisting}[style=TeX]
\TeX{} \qquad \LaTeX{} \qquad \LaTeXe{}
\end{lstlisting}
\TeX{} \qquad \LaTeX{} \qquad \LaTeXe{}
\subsection{引号}
\begin{lstlisting}[style=TeX]
` 左单引号 \qquad
' 右单引号 \qquad
`` 左双引号 \qquad
'' 右双引号 \qquad
``你好''
\end{lstlisting}
` 左单引号 \qquad
' 右单引号 \qquad
`` 左双引号 \qquad
'' 右双引号 \qquad
``你好''
\subsection{连字符}
\begin{lstlisting}[style=TeX]
- \qquad -- \qquad ---
\end{lstlisting}
- \qquad -- \qquad ---
\section{图}
\subsection{插图}
\begin{lstlisting}[style=TeX]
\includegraphics{dog.jpg}
\includegraphics[scale=0.5]{dog.jpg}
\end{lstlisting}
\includegraphics{dog.jpg}
\includegraphics[scale=0.5]{dog.jpg}
\section{表格}
\subsection{插表}
\begin{lstlisting}[style=TeX]
% p{1.5cm} 指定列宽度
\begin{tabular}{|l|c|c|c|p{3cm}|}
\hline
姓名 & 年龄 & 语文 & 数学 & 英语 \\
\hline
张x & 13 & 99 & 89 & 99 \\
\hline
李X & 14 & 88 & 88 & 100 \\
\hline
王Y & 14 & 88 & 98 & 97 \\
\hline
\end{tabular}
\end{lstlisting}
% p{1.5cm} 指定列宽度
\begin{tabular}{|l|c|c|c|p{3cm}|}
\hline
姓名 & 年龄 & 语文 & 数学 & 英语 \\
\hline
张x & 13 & 99 & 89 & 99 \\
\hline
李X & 14 & 88 & 88 & 100 \\
\hline
王Y & 14 & 88 & 98 & 97 \\
\hline
\end{tabular}
\section{浮动体}
\subsection{浮动图表}
\begin{lstlisting}[style=TeX]
% figure 用来浮动图像
% table 用来浮动表格
图\ref{fig-dog}中有一只小狗在偷偷笑。
\begin{figure}[htbp] % 任何位置显示
\centering % 居中显示
\includegraphics[scale=0.3]{dog.jpg}
\caption{一只小狗在偷偷地笑}\label{fig-dog}
\end{figure}
表\ref{tab-grades}中是学生的成绩和年龄。
\begin{table}[htbp]
\centering
\begin{tabular}{|l|c|c|c|r|}
\hline
姓名 & 年龄 & 语文 & 数学 & 英语 \\
\hline
张x & 13 & 99 & 89 & 99 \\
\hline
李X & 14 & 88 & 88 & 100 \\
\hline
王Y & 14 & 88 & 98 & 97 \\
\hline
\end{tabular}\
\caption{学生成绩单}\label{tab-grades}
\end{table}
\end{lstlisting}
% figure 用来浮动图像
% table 用来浮动表格
图\ref{fig-dog}中有一只小狗在偷偷笑。
\begin{figure}[htbp] % 任何位置显示
\centering % 居中显示
\includegraphics[scale=0.3]{dog.jpg}
\caption{一只小狗在偷偷地笑}\label{fig-dog}
\end{figure}
表\ref{tab-grades}中是学生的成绩和年龄。
\begin{table}[htbp]
\centering
\begin{tabular}{|l|c|c|c|r|}
\hline
姓名 & 年龄 & 语文 & 数学 & 英语 \\
\hline
张x & 13 & 99 & 89 & 99 \\
\hline
李X & 14 & 88 & 88 & 100 \\
\hline
王Y & 14 & 88 & 98 & 97 \\
\hline
\end{tabular}\
\caption{学生成绩单}\label{tab-grades}
\end{table}
\section{数学}
\subsection{公式formula}
行内公式: $f(x) = 3x^2 + 1$
行间公式有两种:
第一种:
\begin{lstlisting}[style=TeX]
$$f(x) = 4x_1^2 + 3x_2^2 + 2$$
\end{lstlisting}
$$f(x) = 4x_1^2 + 3x_2^2 + 2$$
第二种:
\begin{lstlisting}[style=TeX]
\[
f(x) = 4x_1^2 + 3x_2^2 + 2
\]
\end{lstlisting}
\[
f(x) = 4x_1^2 + 3x_2^2 + 2
\]
可编号:(不编号则用equation*)
\begin{lstlisting}[style=TeX]
\begin{equation}\label{f1}
f(x) = 4x_1^2 + 3x_2^2 + 2x_3^3 + 4
\end{equation}
\begin{equation}\label{f2}
f(\alpha, \beta, \gamma) = 4\alpha^2 + 3\beta^2 + 2\gamma^3 + 4
\end{equation}
编号与交叉引用
如公式\ref{f1}和公式\ref{f2}所示。
\end{lstlisting}
\begin{equation}\label{f1}
f(x) = 4x_1^2 + 3x_2^2 + 2x_3^3 + 4
\end{equation}
\begin{equation}\label{f2}
f(\alpha, \beta, \gamma) = 4\alpha^2 + 3\beta^2 + 2\gamma^3 + 4
\end{equation}
编号与交叉引用
如公式\ref{f1}和公式\ref{f2}所示。
\subsection{矩阵matrix}
\begin{lstlisting}[style=TeX]
\[
\begin{matrix}
1 & 2 \\
3 & 4
\end{matrix}
\]
\end{lstlisting}
\[
\begin{matrix}
1 & 2 \\
3 & 4
\end{matrix}
\]
\begin{lstlisting}[style=TeX]
\[
\begin{bmatrix}
1 & 2 \\
3 & 4
\end{bmatrix}
\]
\end{lstlisting}
\[
\begin{bmatrix}
1 & 2 \\
3 & 4
\end{bmatrix}
\]
\begin{lstlisting}[style=TeX]
\[
\begin{Bmatrix}
1 & 2 \\
3 & 4
\end{Bmatrix}
\]
\end{lstlisting}
\[
\begin{Bmatrix}
1 & 2 \\
3 & 4
\end{Bmatrix}
\]
\begin{lstlisting}[style=TeX]
\[
\begin{vmatrix}
1 & 2 \\
3 & 4
\end{vmatrix}
\]
\end{lstlisting}
\[
\begin{vmatrix}
1 & 2 \\
3 & 4
\end{vmatrix}
\]
\begin{lstlisting}[style=TeX]
\[
\begin{Vmatrix}
1 & 2 \\
3 & 4
\end{Vmatrix}
\]
\end{lstlisting}
\[
\begin{Vmatrix}
1 & 2 \\
3 & 4
\end{Vmatrix}
\]
\begin{lstlisting}[style=TeX]
\[
\begin{pmatrix}
1 & 2 \\
3 & 4
\end{pmatrix}
\]
\end{lstlisting}
\[
\begin{pmatrix}
1 & 2 \\
3 & 4
\end{pmatrix}
\]
\begin{lstlisting}[style=TeX]
\[
A =
\begin{bmatrix}
a_{11} & \cdots & a_{1n} \\
& \ddots & \vdots \\
0 & & a_{nn} \\
\end{bmatrix}_{n \times n}
\]
\end{lstlisting}
\[
A =
\begin{bmatrix}
a_{11} & \cdots & a_{1n} \\
& \ddots & \vdots \\
0 & & a_{nn} \\
\end{bmatrix}_{n \times n}
\]
这是一个行内小矩阵
\begin{math}
\left(
\begin{smallmatrix}
a & b \\
-c & d
\end{smallmatrix}
\right)
\end{math}
\begin{lstlisting}[style=TeX]
\begin{math}
\left(
\begin{smallmatrix}
a & b \\
-c & d
\end{smallmatrix}
\right)
\end{math}
\end{lstlisting}
使用array环境排版,类似于tabular
\begin{lstlisting}[style=TeX]
\[
\begin{array}{r | r}
\frac{1}{\alpha} & 3 \\
\hline
0 & \frac{a}{Bc} \\
\end{array}
\]
\end{lstlisting}
\[
\begin{array}{r | r}
\frac{1}{\alpha} & 3 \\
\hline
0 & \frac{a}{Bc} \\
\end{array}
\]
\subsection{多行公式}
使用gather环境实现多行公式排版,并且还会对每行公式进行编号,同时在行间居中显示公式
\begin{lstlisting}[style=TeX]
\begin{gather}
a + b = b + a \\
f(x) = x^2 + 1
\end{gather}
\end{lstlisting}
\begin{gather}
a + b = b + a \\
f(x) = x^2 + 1
\end{gather}
使用gather*取消对公式编号,或者在前者的每行公式中使用notag命令阻止编号
\begin{lstlisting}[style=TeX]
\begin{gather*}
a + b = b + a \\
f(x) = x^2 + 1
\end{gather*}
\end{lstlisting}
\begin{gather*}
a + b = b + a \\
f(x) = x^2 + 1
\end{gather*}
align环境对多行公式进行对齐,并且编号;align*则不编号
\begin{lstlisting}[style=TeX]
\begin{align}
y & = x + 1 \\
x + y & = 3 + 5 + \alpha
\end{align}
\begin{align*}
y & = x + 1 \\
x + y & = 3 + 5 + \alpha
\end{align*}
\end{lstlisting}
\begin{align}
y & = x + 1 \\
x + y & = 3 + 5 + \alpha
\end{align}
\begin{align*}
y & = x + 1 \\
x + y & = 3 + 5 + \alpha
\end{align*}
split环境在equation环境中实现对一个公式的多行排版,只进行一次编号
\begin{lstlisting}[style=TeX]
\begin{equation}
\begin{split}
y &= x + y \\
&= 3 + 23 + 5
\end{split}
\end{equation}
\end{lstlisting}
\begin{equation}
\begin{split}
y &= x + y \\
&= 3 + 23 + 5
\end{split}
\end{equation}
cases环境实现分段函数
\begin{lstlisting}[style=TeX]
\begin{equation}
D(x) =
\begin{cases}
0, & if \ x \in \mathbb{R}, \\
1, & if \ x \in \mathbb{R}\setminus \mathbb{Q}
\end{cases}
\end{equation}
\end{lstlisting}
\begin{equation}
D(x) =
\begin{cases}
0, & if \ x \in \mathbb{R}, \\
1, & if \ x \in \mathbb{R}\setminus \mathbb{Q}
\end{cases}
\end{equation}
\section{插入代码块}
\lstinputlisting[
style = TeX,
caption = {\bf code-template.tex},
label = {code-template.tex}
]{./tmp/code_tmp.tex}
\subsection{插入Python代码块}
\begin{lstlisting}[style=TeX]
\lstinputlisting[
style = Python,
caption = {\bf ff.py},
label = {ff.py}
]{./source/ff.py}
\end{lstlisting}
\lstinputlisting[
style = Python,
caption = {\bf ff.py},
label = {ff.py}
]{./source/ff.py}
\subsection{插入C++代码块}
\begin{lstlisting}[style=TeX]
\lstinputlisting[
style = C++,
caption = {\bf tt.cpp},
label = {tt.cpp}
]{./source/tt.cpp}
\end{lstlisting}
\lstinputlisting[
style = C++,
caption = {\bf tt.cpp},
label = {tt.cpp}
]{./source/tt.cpp}
\subsection{插入\LaTeX{}代码块}
\begin{lstlisting}[style=TeX]
\lstinputlisting[
style = TeX,
caption = {\bf aa.tex},
label = {aa.tex}
]{./source/aa.tex}
\end{lstlisting}
\lstinputlisting[
style = TeX,
caption = {\bf aa.tex},
label = {aa.tex}
]{./source/aa.tex}
\begin{lstlisting}[style=TeX]
\[ f(x) =
\begin{cases}
1 & \text{如果x为有理数,} \\
2 & \text{如果x为无理数,} \\
0 & \text{其他.}
\end{cases}
\]
\end{lstlisting}
\end{document}
LaTeX中嵌入代码块
模板
\usepackage{listings}
\usepackage{xcolor}
% 定义可能使用到的颜色
\definecolor{Light} {HTML} {686868}
\definecolor{Steel} {HTML} {888888}
\definecolor{Dark} {HTML} {262626}
\definecolor{Blue} {HTML} {4172A3}
\definecolor{Green} {HTML} {3b7c50}
% \definecolor{DarkGreen} {HTML} {3ac9b0}
\definecolor{Brown} {HTML} {A07040}
\definecolor{Red} {HTML} {AD4D3A}
\definecolor{Violet} {HTML} {7040A0}
\definecolor{Gray} {HTML} {B8B8B8}
\lstset{
basicstyle = \zihao{-5}\ttfamily,
backgroundcolor = \color[RGB]{245,245,244},
numberstyle = \zihao{-5}\ttfamily, % 行号的样式,小五号,tt等宽字体
keywordstyle = \bfseries\color{blue}, % 关键字风格
commentstyle = \songti\slshape\color{Green}, % 注释的风格
stringstyle = \ttfamily\color{Brown}, % 字符串风格
columns = flexible,
numbers = left, % 行号的位置在左边
showspaces = false, % 是否显示空格
showstringspaces = false,
breaklines = true, % 自动换行,建议不要写太长的行
captionpos = t, % 这段代码的名字所呈现的位置,t指的是top上面
frame = single, % 显示边框
rulecolor = \color{black}, % 没有该color 换行之后边框将会是别的颜色
% escapeinside = {\%*}{*)},
extendedchars = false,
}
\lstdefinestyle{C++}{
morekeywords={alignas,continute,friend,register,true,alignof,decltype,goto,
reinterpret_cast,try,asm,defult,if,return,typedef,auto,delete,inline,short,
typeid,bool,do,int,signed,typename,break,double,long,sizeof,union,case,
dynamic_cast,mutable,static,unsigned,catch,else,namespace,static_assert,using,
char,enum,new,static_cast,virtual,char16_t,char32_t,explict,noexcept,struct,
void,export,nullptr,switch,volatile,class,extern,operator,template,wchar_t,
const,false,private,this,while,constexpr,float,protected,thread_local,
const_cast,for,public,throw,std},
emph={map,set,multimap,multiset,unordered_map,unordered_set,
unordered_multiset,unordered_multimap,vector,string,list,deque,
array,stack,forwared_list,iostream,memory,shared_ptr,unique_ptr,
random,bitset,ostream,istream,cout,cin,endl,move,default_random_engine,
uniform_int_distribution,iterator,algorithm,functional,bing,numeric,},
emphstyle = \color{Violet}\bfseries,
% basewidth = 0.52em,
% columns = fixed, % 如果不加这一句,字间距就不固定,很丑,必须加
language = C++,
}
\lstdefinestyle{Python}{
language = Python,
keywordstyle = [2]\color{teal},
% columns = fixed, % 如果不加这一句,字间距就不固定,很丑,必须加
% basewidth = 0.52em,
}
\lstdefinestyle{TeX}{
language = TeX,
keywordstyle = \color{blue},
morekeywords = {begin,end,color,text,geometry,section,subsection,
subsubsectio,includegraphics,cdots,ddots,vdots,times,mathbb,in,pounds,caption},
emph = {cases,tabular,figure,table,gather,align,math,equation,matrix, pmatrix,bmatrix,
Bmatrix,vmatrix,Vmatrix,smallmatrix,document, documentclass, lstlisting,
lstinputlisting,array,split},
emphstyle = \color{teal},
emph = {[2]usepackage},
emphstyle = [2]\color{Violet},
}
写在后面
上述代码中使用的
ff.py
、tt.cpp
、aa.tex
文件其实就是普通的Python
文件、C++
文件、Tex
文件,您可以自定义其中的内容,我是为了方便从文件直接读入代码块,所以使用了简单的文件作为引入。注意这些文件需要在整个LaTex工程目录下。
最终效果