Latex 入門教程


Latex 入門教程

學習途徑:LaTex入門_嗶哩嗶哩_bilibili

運行環境:texlive2021、texstudio-4.1.2-win-qt6

1. 基本結構

整個 Latex 文件分為兩個部分,第一個部分為導言區,第二個部分為正文區。具體內容如下

% 導言區
\documentclass{article}  %book, report, letter

\title{數字孿生}
\author{xiaxiang}
\date{\today}
% 顯示中文包
\usepackage{ctex}

% 正文區 有且只有一個 document 環境
\begin{document}
	% 讓 title 顯示出來
	\maketitle
	
	第一小節
\end{document}

第一節課

2. 中文處理

  1. 設置編輯器,查看 選項-》設置-》構建-》默認編輯器-》XeLaTex
  2. 確保 Latex 源文件為 UTF-8 格式,才能支持中文,選項在 Texstudio 編輯器右下角

第二節課1

  1. 導入中文宏包,\usepackage{ctex}

查看各類相關文檔 texdoc xxx

  • texdoc ctex
  • texdoc lshort-zh這份資料是 Latex 中文的入門教程,很不錯

3. 字體設置

在 Latex 中,一個字體有五種屬性,字體編碼,字體族,字體系列,字體形狀,字體大小

相關設置如下

% 一般只有 10-12 磅,[12pt] 代表正常字體的大小
\documentclass{article}[12pt]  %book, report, letter

\usepackage{ctex}

% 推薦使用字體方式為 自定義字體,而非使用系統字體
\newcommand{\myfont}{\textrm{\textmd}}

% 有且只有一個 document 環境
\begin{document}
	
	% 字體系列 (羅馬字體,無襯線字體、打字機字體)
	\textrm{Roman Family} \textsf{Sans Serif Family} \texttt{Typewriter Family}
	
	% 字體作用域
	{\rmfamily Roman Family} {\sffamily Sans Serif Family} {\ttfamily Typewriter Family}
	\sffamily Hello global family
	
	% 字體系列設置
	\textmd{Medium Series} \textbf{Boldface Series}
	{\mdseries Medium Series} {\bfseries Boldface Series}
	
	% 字體形狀
	\textup{Upright Shape} \textit{Italic Shape}
	%\textsl{Slanted Shape} \textsc{Small Caps Shape}
	%{\upshape Upright Shape} {\itshape Italic Shape} {\slshape Slanted Shape} {\scshapeSmall Caps Shape }
	
	% 中文字體
	{\songti 宋體} \quad {\heiti 黑體}  \quad {\fangsong 仿宋}  \quad {\kaishu 楷書}
	
	% 字體大小 tiny scriptsize footnotesize small normalsize large Large LARGE huge Huge
	{\tiny Hello}
	{\zihao{5} 你好}
\end{document}

更加詳細的文檔可以參考 texdoc ctex。其中需要注意的是在

  1. documentclass 處可以設置 normal font size
  2. 在使用時一般自定義字體,而不是直接使用系統字體

4. 篇章結構

  1. section、subsection、subsubsection 構成篇章結構,\tableofcontents構成目錄
\begin{document}
		% 目錄
	\tableofcontents
	
	% \chapter{緒論}
	\section{引言}
	
	% \par 分段 \\ 換行 通常使用空行
	\section{實驗方法}
	\section{實驗結果}
	\subsection{數據}
	\subsection{圖表}
	\subsubsection{實驗條件}
	\subsubsection{實驗過程}
	\subsection{結果分析}
	\section{結論}
	\section{致謝}
	
\end{document}

空行可以進行分段;\par也可以進行分段,開頭會空兩格;\\ 不會分段,只會換行,開頭不會空兩格

第四節1

  1. 使用\chapter也可以分章節,但是這時候文檔類別得設置成 book,在這種情況下 subsubsection會無效
  2. 如果想自定義標題樣式,可以設置 \ctexset,具體實現方法見 texdoc ctex

5. 特殊字符

  1. 空白符合
% 空行分段,多個空行等同一個
% 自動縮進,絕對不能使用空格代替
% 英文中多個空格處理為一個空格,中文中空格將被忽略
% 漢字與其他字符的間距會自動由 XeLaTeX 處理
% 禁止使用中文全角空格

\quad % 1em
\qquad % 2em
\,  \thinspace % 1/6em
\enspace % 0.5em
\  % 空格
~  % 硬空格(不能分割得空格)
\kern 1pc % 1pc=12pt=4.218mm 自定義長度得空格
\kern -1em % 使用負號長度
\\hskip 1em % 自定義
\hspace{35pt} % 自定義
\hphantom{xyz} % 占位寬度
\hfill % 彈性長度空白
  1. 控制符
% 轉義這些符合
\# \$ \% \{ \} \~{} \_{} \^{} \&
\textbackslash % 轉義 \\,原因 \\ 代表換行
  1. 排版符號,\S \P \dag \ddag \copyright \pounds

  2. 標識符號,\Tex{} \LaTeX{} \LaTeXe{}

  3. 引號

% 左邊的單引號和雙引號
` ``
% 右邊的單引號和雙引號
' ''
  1. 連字符 - -- ---

  2. 非英文字符 \oe \OE \ae \AE \aa \AA \o \O \l \L \ss \SS !

  3. 重音符號,以 o 為例

\`o \'o \^o \''o \~o \=o \.o \u{o} \v{o} \H{o} \r{o} \t{o} \b{o} \c{o} \d{o}

6. 插圖

使用插圖先安裝宏包,在引入圖片路徑,最后引用

  1. 使用宏包和引入圖片路徑
% 引入圖片宏包
\usepackage{graphicx}
% 可以指定多個路徑
\graphicspath{{figures/}, {pics/}}
  1. 插入圖片,使用圖片時可不用寫圖片后綴,支持 EPS,PDF,PNG,JPEG,BMP
\includegraphics{img2.jpg}
% 不帶后綴也可以
\includegraphics{tiger}
% 縮放旋轉
\includegraphics[scale=0.3]{tiger}

第6節

第六節2

命令細節 texdoc graphicx

7. 表格

表格有專門的環境:tabular

\begin{document}
	% | 表示表格豎線 || 雙豎線
	\begin{tabular}{|| l | c || c | c | p{1.5cm} ||}
		% hline 表示一條橫線,兩個為雙橫線
		\hline
		姓名 & 語義 & 數學 & 外語 & 備注 \\
		\hline \hline
		張三 & 15 & 10 & 20 & 好 \\
		李四 & 20 & 5 & 8 & 及格 \\
		王五 & 15 & 10 & 20 & 好 \\
	\end{tabular}

\end{document}

第七節

需要注意的是這些符號

  • l c r p:l 表示左對齊,c 表示中心對齊,r 表示右對齊,p表示指定寬度
  • \hline 表示一條橫線,連續兩個為雙橫線。 | 表示豎線,|| 表示雙豎線
  • & 分割一行中的每項數據
  • \\ 表示換行

詳細細節可以看texdoc booktab, texdoc longtab, texdoc tabu

8. 浮動體

舉兩個例子,圖像和表格,分別有自己的浮動環境

% 一般只有 10-12 磅
\documentclass{article}[12pt]  %book, report, letter

\usepackage{graphicx}
\graphicspath{{figures/}}
\usepackage{ctex}

% 有且只有一個 document 環境
\begin{document}
	
	這是一只可愛的貓(圖\ref{fig-tiger})
	
	\begin{figure}[htbp]
		\centering
		\includegraphics[scale=0.3]{tiger}
		\caption{tiger} \label{fig-tiger}
	\end{figure}

	圖像學基礎(圖\ref{fig-graphics})

	\begin{figure}[htbp]
		\centering
		\includegraphics[scale=0.3]{img2}
		\caption{tiger} \label{fig-graphics}
	\end{figure}

	這是三年級五班的成績(表\ref{tab-score})
		
	\begin{table}[h]
		\centering
		\caption{考試成績單} \label{tab-score}
		\begin{tabular}{|| l | c || c | c | p{1.5cm} ||}
			% hline 表格恆星,兩個為雙橫線
			\hline
			姓名 & 語義 & 數學 & 外語 & 備注 \\
			\hline \hline
			張三 & 15 & 10 & 20 & 好 \\
			李四 & 20 & 5 & 8 & 及格 \\
			王五 & 15 & 10 & 20 & 好 \\
			\hline
		\end{tabular}
	\end{table}
\end{document}

第8節

解釋下其中代碼含義

  • figure 和 table 分別是表格和圖像的浮動布局,用來設置兩種元素的位置
  • [htbp] 表示允許的位置,h,此處;t,頁頂;b,頁底;p,獨立一頁;默認tbp
  • \centering 表示橫向劇中
  • \caption 表示標題
  • \label 表示當前圖的表示,可以在其他地方引用,達到交叉引用的效果

圖和表分別計算,從 1 自動往后遞增。擴展:標題控制(caption,bicaption等)、並排與子圖表(subcaption、subfig、floatrow等)、繞排(picinpar、wrapfig等)

9. 數學公式

排版內容分為文本模式和數學模式。文本模式用於普通文本排版,數學模式用於數學公式排版

\begin{document}
	
	\section{簡介}
	
	排版內容分為文本模式和數學模式。文本模式用於普通文本排版,數學模式用於數學公式排版。
	
	\section{行內公式}
	\subsection{美元符號}
	$a+b=c=d+f=e$
	\subsection{小括號}
	\(a+b=c=d+f=e\)
	\subsection{math環境}
	\begin{math}
		a+b=c=d+f=e
	\end{math}

	\section{上下標}
	$x^3_1 + 3x^2_2 + 2x_{11} + 9 = 0$
	
	\section{希臘字母}
	$\alpha$ $\beta$ $\gamma$ $\epsilon$ $\pi$ $\omega$ $\Gamma$ $\Delta$ $\Theta$ $\Pi$
	
	$\alpha^2 + \beta^2 +\gamma^2 = 1$
	
	\section{數學函數}
	$\log$ $\sin$ $\cos$ $\arcsin$ $\arccos$ $\ln$
	
	$ y=\log_2{n} + \sqrt[3]{m} + \sin^-1$
	
	\section{分式}
	$a/b \frac{3}{8}$
	
	\section{行公式}
	\subsection{美元符號}
	$$
		y=\log_2{n} + \sqrt[3]{m} + \frac{3}{8}
	$$
	
	\subsection{中括號}
	\[	y=\log_2{n} + \sqrt[3]{m} + \frac{3}{8}\]
	
	\subsection{displaymath}
	\begin{displaymath}
		y=\log_2{n} + \sqrt[3]{m} + \frac{3}{8}
	\end{displaymath}
	\subsection{自動編號公式equation環境(如公式\ref{eq:y1})}
	\begin{equation}
		y=\log_2{n} + \sqrt[3]{m} + \frac{3}{8}
		\label{eq:y1}
	\end{equation}

\end{document}

第9節

第9節2

注意解釋的只有最后一個自動編號公式,該公式可以提取出當前公式序號,達到交叉引用的效果

10. 矩陣排版

矩陣運算需要有數學公式的環境,因此可以用到第 9 節的行外公式,首先介紹各種矩陣

	$
	\begin{matrix}
		0 & 0 \\
		1 & 1
	\end{matrix}
	$ \ 
	$
	\begin{pmatrix}
		0 & 0 \\
		1 & 1
	\end{pmatrix}
	$ \ 
	$
	\begin{bmatrix}
		0 & 0 \\
		1 & 1
	\end{bmatrix}
	$ \ 
	$
	\begin{Bmatrix}
		0 & 0 \\
		1 & 1
	\end{Bmatrix}
	$ \ 
	$
	\begin{vmatrix}
		0 & 0 \\
		1 & 1
	\end{vmatrix}
	$ \ 
	$
	\begin{Vmatrix}
		0 & 0 \\
		1 & 1
	\end{Vmatrix}
	$

第10節

上述分別為各種矩陣的環境,矩陣內同樣可以進行各種計算

	$$
	\begin{bmatrix}
		a^2_{11} & a^2_{12} & a^2_{13} \\
		0 & a^2_{22} & a^2_{23} \\
		0 & 0 & a^2_{33} \\
	\end{bmatrix}
	$$

第10節1

也可以寫各種省略符號

	\newcommand{\adots}{\mathinner{\mkern2mu%
		\raisebox{0.1em}{.}\mkern2mu\raisebox{0.4em}{.}%
		\mkern2mu\raisebox{0.7em}{.}\mkern1mu}}
	
	$$
	A = \begin{bmatrix}
		a^2_{11} & \dots & a^2_{1n} \\
		 \adots & \ddots & \vdots \\
		 &  & a^2_{nn} \\
	\end{bmatrix}_{n \times n}
	$$

第10節2

結合分塊矩陣和長省略號可得

	$$
	\begin{bmatrix}
		a_{11} & a_{12} & \dots & a_{1n} \\
		\hdotsfor{4} \\
		& & \ddots & \vdots \\
		\multicolumn{2}{c}{\raisebox{1.3ex}[0pt]{\Huge 0}}  &  & a^2_{nn} \\
	\end{bmatrix}
	$$

第10節4

使用 array 可以表示更為復雜的矩陣

$$
	\begin{array}{c@{\hspace{-5pt}}l}
		\left(
			\begin{array}{ccc|ccc}
					a & \cdots & a & b & \cdots & b \\
					 & \ddots & \vdots & \vdots & \adots \\
					 & 		  & a & b \\
					 &		  &   & c & \cdots & c \\
					 &		  &	  & \vdots & & \vdots \\
					 \multicolumn{3}{c}{\raisebox{2ex}[0pt]{\Huge 0}} & c & \cdots & c
			\end{array}
		\right)
		&
		\begin{array}{l}
			\left.\rule{0mm}{7mm} \right\}p\\
			\\
			\left. \rule{0mm}{7mm} \right\}q
		\end{array}
		\\[-5pt]
		% 第二行
		\begin{array}{cc}
			\underbrace{\rule{17mm}{0mm}}_m &
			\underbrace{\rule{17mm}{0mm}}_m
		\end{array}
	\end{array}
	$$

第10節3

解析這個表達式

  1. 最外層套了一個 array,它包括矩陣、pq 和兩個 m。大框架就已經確定了
  2. 矩陣為一個 array,它包括 a、b、0 和 c
  3. 只要把整體結構把握好,還是比較容易看出拼湊的邏輯

11. 多行數學公式

多行公式的環境有,gather|gather*, alig|align*, split, case

% 有且只有一個 document 環境
\begin{document}
	% 帶編號的公式
	\begin{gather}
		a+b=b+a \notag \\
		ab = ba
	\end{gather}

	% 不帶編號
	\begin{gather*}
		a+b=b+a \\
		ab = ba
	\end{gather*}

	% align 環境帶編號
	\begin{align}
		x & = a + \cos(b) + \sin(c) \\
		y & = 1 \\
		z & = \frac{20}{79} \times 987
	\end{align}

	\begin{align*}
		x &= t &  x &= \cos y \\
		y &= u &  y &= \sin (u+1)
	\end{align*}
	
	% split 對齊方式與 align 一致,但整體屬於一個公式
	\begin{equation}
	\begin{split}
		\cos 2x &= \cos^2 x - \sin^2 x \\
		 &= 2\cos^2 x -1
	\end{split}
	\end{equation}

	\begin{equation}
	D(x) = \begin{cases}
		1, & \text{如果} x \in \mathbb{Q} \\
		2, & \text{如果} x \in \mathbb{R} \setminus \mathbb{Q}
	\end{cases}
	\end{equation}
\end{document}

第11節

注意點:

  1. 帶星號是不帶編號的數學公式
  2. 在 gather 環境中,\notag 表示當前行沒有公式
  3. 在 align 環境中,可以用 & 進行對其操作
  4. 在 split 環境中,也可以使用 & 進行對齊,並且整體屬於一個公式
  5. 在 cases 環境中,整體屬於一個公式
  6. 在數學公式中不能輸入文字,但是可以通過 \text 的方式輸入文本

12. 參考文獻

參考文獻排版分為多類,如果只是單次使用,直接用 thebibliography 環境即可,使用 \cite 進行引用;如果想進行單次管理多次使用,可以將參考文獻使用另外的文件定義

第12節

  1. 單次使用
	引用第一篇文章 \cite{article1},引用第二篇書籍 \cite{book1} 等等
	
	% 參考文獻排版
	% 一次管理,一次使用,99為編號樣本
	\begin{thebibliography}{99}
		\bibitem{article1} 引用\emph{重點}文章1
		\bibitem{book1} 引用書籍1
	\end{thebibliography}
  1. 多次使用

新建一個 bib 格式的文件,text.bib,數據來源,谷歌學術,知網

@article{陶飛2018數字孿生及其應用探索,
	title={數字孿生及其應用探索},
	author={陶飛 and 劉蔚然 and 劉檢華 and 劉曉軍 and 劉強 and 屈挺 and 胡天亮 and 張執南 and 向峰 and 徐文君 and others},
	journal={計算機集成制造系統},
	volume={24},
	number={1},
	pages={1--18},
	year={2018}
}

@article{rego20153dmol,
	title={3Dmol. js: molecular visualization with WebGL},
	author={Rego, Nicholas and Koes, David},
	journal={Bioinformatics},
	volume={31},
	number={8},
	pages={1322--1324},
	year={2015},
	publisher={Oxford University Press}
}

第12節1

先點擊引用,后點擊 BibTeX 將得到需要的數據,復制粘貼進我們創建的 bib 文件中即可使用

使用方法

% 1 設置參考文獻樣式
\bibliographystyle{plain} % plain unsrt alpha abbrv

\begin{document}

	% \cite 后面的參數是 bib 單個數據中的第一個頭信息
	這是引用來自知網的參考文獻 \cite{__2021}

	% \citep{} \citet{}
	這是引用的 bib 文件下的參考文獻 \cite{陶飛2018數字孿生及其應用探索} 便於探索

	\nocite{*}
	\bibliography{text, zote}
	 
\end{document}
  • \nocite{*} 表示將沒有引用的文獻也羅列出來
  • \bibliography{text, zote} 表示導入 bib 數據庫,這里有兩個數據庫
  1. 導入知網中的文獻,下載 zotero 以及 google 的擴展工具,進入知網的搜索頁面,點擊擴展工具,可以獲得當前頁面的所有文獻信息。這些信息可以通過 zotero 導出成 bib 文件供我們使用,也可以把這個文件中的信息復制出來統一放在一個 bib 文件中

第12節2

第12節3

  1. 新參考文獻排版引擎 BibLaTeX 使用較麻煩,有興趣可以自行搜索

13. 定義新命令和環境

可以定義新命令使用、

% 一般只有 10-12 磅
\documentclass{article}[12pt]  %book, report, letter

\usepackage{ctex}

\newcommand{\newMessage}{這是一個新的命令字符串}

\newcommand{\loves}[2]{#1 喜歡 #2}

\newcommand{\lovess}[3]{#1 喜歡 #2, #2 喜歡 #3,#3 喜歡 #1}

% 第一個參數為可選參數,默認為喜歡
\newcommand{\defaultLove}[3][喜歡]{#2 #1 #3}

% \renewcommand{cmd}{def} 與 newcommand 使用方法
% \newenvironment{envname}{begdef}{enddef} 與 newcommand 用法一致,具體見文檔

% 有且只有一個 document 環境
\begin{document}
	 \newMessage
	 
	 \loves{貓兒}{小魚}
	 
	 \lovess{貓兒}{狗兒}{羊兒}
	 
	 \defaultLove{小花}{小明}
	 
	 \defaultLove[討厭]{小花}{小明}
\end{document}

第13節

  • 注意可以用默認參數
  • 自定義命令可以是輸出指定字符串
  • 重定義環境看相關文檔


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM