2.1.1 正文段落
-
分段
Latex使用空行表示分段,在自定义命令中,也常用\par命令分段 -
首行缩进
每个自然段在第一行有一个固定的缩进,可以用长度变量\parindent控制 -
段与段之间的垂直距离
由长度变量\parskip控制,中文排版中常使用\setlength{\parskip}{0pt}把段间距定义为固定长度,禁止段落间距离伸长 -
对齐方式
Latex是默认两端均匀对齐的,也可以改为左对齐、右对齐或居中模式
- 左对齐 \raggedright
- 右对齐 \raggedleft
- 居中 \centering
Latex提供三个环境来排版不同对齐方式的文字:
- flushleft 环境左对齐
- flushright 环境右对齐
- center 环境居中
- 控制段落宽度
整体段落:
- \leftskip
- \rightskip
悬挂缩进:
- hangafter
- hangindent
2.1.2 文本环境
- 引用环境有两种:
- quote环境
quote环境再段前没有首行缩进,适合小段的内容引用:
\begin{quote}
学而时习之,不亦说乎?
有朋自远方来,不亦乐乎?
\end{quote}
- quotation环境
quotation环境则再每段前有首行缩进,因而适用于多段的文字引用:
\begin{quotation}
学而时习之,不亦说乎?
有朋自远方来,不亦乐乎?
默而识之,学而不厌,诲人不倦,何有于我哉?
\end{quotation}
-
诗歌环境
\begin{verse}
内容
\end{verse} -
摘要环境
摘要魂晶abstract是article和repert文档类(包括中文的ctexart和ctexrep)定义的,它产生一个类似quotation的小号字环境,并增加标题:
\begin{abstract}
本书讲解 \LaTeX{} 的使用
\end{abstract}
摘要的标题由\abstractname 定义,英文默认是"Abstract",中文是"摘要"。ctexart中用\CTEXoptions设置:
\CTEXoptions[abstractname={摘\quad 要}]
2.2.3 列表环境
2.2.3.1 基本列表环境
- 编号的enumerate环境
\begin{enumerate}
\item 中文
\item English
\item Francais
\end{enumerate}
- 不编号的itemize环境
\begin{itemize}
\item 中文
\item English
\item Francais
\end{itemize}
- 使用关键字的description环境,其中\item[内容]中的可选参数[内容]可以把“内容”加粗:
\begin{description}
\item[中文] 中国的文字
\item[English] 英国的文字
\item[Francais] 法国的文字
\end{description}
上面三种环境可以嵌套使用(至多四层)
2.2.3.2 计数器与编号
2.2.3.3 定制列表环境
2.2.4 定义类环境
定义类环境是LaTeX中的一类重要的文本环境,它可以产生一个标题、标号和特定格式的文本,如:
\newtheorem{thm}{定理} % 一般在导言区
\begin{thm}
直角三角形斜边的平方等于两腰的平方和。
\end{thm}
\newtheorem{thm}{定理}用来声明一个新的定理环境,两个参数分别是定理类环境名和定理输出的标题名。新定义的thm环境允许带有可选参数表示定理的小标题:
\begin{thm}[勾股定理]
直角三角形斜边的平方等于两腰的平方和。
\end{thm}
2.2.5 抄录和代码环境
2.2.5.1 抄录命令与环境
LaTeX输入特殊符号相当复杂。当我们需要大量用到LaTeX中有特殊意义的符号时,此时就需要用抄录功能。
\verb命令可以用来表示行文中的抄录,其语法格式如下:
\verb<符号><抄录内容><符号>,如:
\verb"\LaTeX \& \TeX" \qquad
\verb!\/}{#$%&~!
2.2.5.2 程序代码与listings
- listings基础
可以使用verbatim环境排版程序代码,不过,如果还想在程序代码中增加语法高亮功能,就需要listings宏包。例如:
\usepackage{listings} % 导言区使用
\begin{lstlisting}[language=Python]
import pandas as pd
import numpy as np
def fun(num):
if num <= 0:
return 0
return num + fun(num - 1)
a = [1, 2, 3, 4, 5]
for i in a:
print(fun(i))
\end{lstlisting}
可以通过设置column选项为flexible(默认为fixd),或使用flexiblecolumns选项来把字符列设置为非等宽的。采用非等宽的列可以让默认的Roman字体看上去也比较顺眼:
\lstset{flexiblecolumns}
\begin{lstlisting}[language=Python]
import pandas as pd
import numpy as np
def fun(num):
if num <= 0:
return 0
return num + fun(num - 1)
a = [1, 2, 3, 4, 5]
for i in a:
print(fun(i))
\end{lstlisting}
事实上,listings宏包还可以设置背景颜色、强调内容、标题、目录等
- listings进阶
将代码块设计为csdn代码块样式:
\lstset{
backgroundcolor=\color[RGB]{250, 250, 250},
tabsize=4,
basicstyle=\ttfamily,
breaklines=true,
%framextopmargin=50pt,
%frame=bottomline,
columns=fixed,
numbers=left, % 在左侧显示行号
frame=none, % 不显示背景边框
keywordstyle=\color[RGB]{70, 130, 180},
commentstyle=\color[RGB]{105, 105, 105},
stringstyle=\color[RGB]{110, 139, 61},
numberstyle=\footnotesize\color{darkgray}, % 设定行号格式
xleftmargin=-2em,
xrightmargin=-2em,
aboveskip=0em,
showstringspaces=false, % 不显示字符串中的空格
language=python, % 设置语言
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,numpy,graph,path,append,extend,
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{CPPViolet},
}
效果:
参考文献:
[1]官方文档:http://texdoc.net/texmf-dist/doc/latex/listings/listings.pdf
[2]wikibooks:https://en.wikibooks.org/wiki/LaTeX/Source_Code_Listings
2.2.6 tabbing环境
tabbing环境用来排版制表位,让不同的行在指定的地方对齐。在tabbing环境中,行与行之间用\分隔,使用=命令来设置制表位,用>命令则可以跳到下一个前面已经设置的制表位,因而可以用tabbing环境制作比较简单的无线表格
尽管使用tabbing环境可以自由地排版复杂的算法,但是太复杂了。clrscode可以按《算法导论》中的格式进行算法排版;另外还有algorithm2e和algorithmicx宏包
2.2.7 脚注与边注
1.脚注
LaTeX使用\footnote{脚注内容}产生脚注,以\footnotesize的字号输出。
另一种常见的脚注是带圈的的数字:
\renewcommand\thefootnote{\textcircled{\arabic{footnote}}}
使用pifont宏包提供的带圈数字符号效果更好些,使用pifont宏包的\ding命令可以输出符号表中的符号,查表找到阳文带圈数字从172号符号开始,代码为:
\usepackage{pifont}
\renewcommand\thefootnote{\ding{\numexpr171+\value{footnote}}}
2.2.8 垂直间距与垂直盒子
\vspace{长度}和\vspace*{长度}生成垂直间距