1. 程式人生 > 實用技巧 >【工具】LaTex 基礎

【工具】LaTex 基礎

LaTex 基礎

LaTeX 簡介

高德納為了讓他的鉅著《計算機程式設計藝術》排版更精美髮明瞭 TeX,與 Markdown 類似,有程式語言的味道。

LaTeX 是一種工具,基於 TeX 格式定義了很多更方便使用的控制命令,它將使用者按照它的格式編寫的文件解釋成 TeX 引擎能理解的形式並交付給 TeX 引擎處理,再將最終結果返回給使用者。

CTeX, MiKTeX, TeX Live 都是 TeX 的發行,包含渲染引擎,巨集包,編輯器等其他工具的集合。

TeX 引擎

TeX, pdfTeX, XeTeX, LuaTeX都是 TeX 引擎,按照先程序度遞增(LuaTeX 尚未完善)。

  • TeX,僅支援最原始的 plain tex 格式,晦澀難懂。
  • pdfTeX: pdfTeX 包含了 PDF 和 DVI 格式的輸出,被許多 TeX 的發行版用作預設的 TeX 引擎。
  • XeTeX: 之前的引擎只支援 ASCII 字元,即不支援中日韓字元。XeTeX 則原生支援 Unicode(需要使用 utf-8 編碼)
  • LuaTeX: 基於 pdfTeX 並支援 Luau 指令碼的 TeX 引擎,最初被作為 pdfTeX 的下一代版本但事實上形成了一個獨立的分支。目前還不完善。

LaTeX 發行版

TeX 有許多種發行版,根據喜好選擇即可。例如:

Windows/Linux 推薦使用 texlive,Mac 則推薦使用 mactex(雖然 texlive 也可以在 Mac 下用 Unix 的方式安裝)。mactex 是一個基於 texlive 之上的封裝,二者主要區別是:

  • 採用 Mac 專用的方式打包,安裝簡便,不勞心配置。
  • 封裝了一系列為 Mac 設計的老古董工具(LaTeXit 和 TeXshop,可以移除)。

Mac 還有一個精簡版的 basictex,和 mactex 類似,也是對 texlive 的封裝。但是 basictex 中缺少很多巨集包,在使用的時候需要先手工安裝這些巨集包。

LaTeX 中文環境的配置

最初 TeX 只支援 ASCII 編碼,沒有考慮中文的使用環境, 不過現在可以使用ctex巨集包來支援中文。

  • 文件使用utf-8編碼。
  • 引入巨集包\usepackage{ctex},並且使用xelatex編譯。
  • 另外,ctex中內建了 6 種中文字型:heiti, kaishu, songti, lishu, youyuan, fangsong
\documentclass[UTF8]{article}
% 若設定 ctexart 是使用 ctex 的 article,下面不需要再引用 ctex 巨集包了
\usepackage{ctex}
\begin{document}
    中文 && English
\end{document}

LaTeX 原始檔的基本結構

  • LaTeX 原始檔分為導言區正文區兩部分構成。
  • 一個 LaTeX 原始檔有且只有一個document環境。
  • $符號內的為數學模式,外的為文字模式。且$**$為行內模式,$$**$$為行間模式。
  • 一個或多個空行表示段落的分隔。
% 導言區
% 導言區用於全域性的設定,引入巨集包等。
\documentclass{article} % 除了 article 外,還有 book, report, letter 等型別
\usepackage{ctex} % usepackage 用於匯入巨集包,這裡匯入了 ctex 巨集包

\title{My first document}
\author{Zhigao Hong}
\date{\today}

% 正文區
% 正文區用於書寫文章的正文部分
\begin{document} % begin 用於開始一個環境,一個文章有且只有一個 document 環境
    \maketitle
    Hello, \LaTeX.

    Let $f(x)$ be defined by $f(x)=3x^2+1$.
\end{document}

LaTeX 的篇章結構

在 LaTeX 中,可以使用section{xx}, subsection{xx}, subsubsection{xx}等命令來撰寫文章的大致結構。

\documentclass{article}
\usepackage{ctex}

\title{My first document}
\author{Zhigao Hong}
\date{\today}

\begin{document}
    \maketitle

    \section{Introduction}
    \label{sec::introduction}

    \section{Proposed scheme}
    \label{sec::proposed method}

    \subsection{method1}
    \label{subsec::method1}

    \subsection{method2}
    \label{subsec::method2}

    \section{Experimental results and discussion}
    \label{sec::experimental results and discussion}

    \section{Conclusion}
    \label{sec::conclusion}

    \section{References}
    \bibliography{mybibfile}

\end{document}

LaTeX 的字型設定

字型的設定可以使用字型命令和字型宣告兩種設定方式,同時可以使用{}限定其作用範圍,否則自設定起,後面一直有效至重新設定字型。

字型族

字型族有:羅馬字型、無襯線字型、打字機字型。

  • 羅馬字型:\textrm {xxx}或者{\rmfamily xxx}
  • 無襯線字型:\textsf {xxx}或者{\sffamily xxx}
  • 打字機字型:\texttt {xxx}或者{\ttfamily xxx}

字型加粗

  • 加粗:\textbf {xxx}或者{\bfseries xxx}

字型斜體

  • 斜體:\textit {xxx}或者{\itshape xxx}

中文字型

設定中文字型必須引入ctex巨集包,其中內建了 6 種中文字型。

  • 黑體:{\heiti xxx}
  • 其他類似:kaishu, songti, lishu, youyuan, fangsong

字型大小

  • 字型的大小也是通過一系列字型宣告實現:tiny, scriptsize, footnotesize, small, normalsize, large, Large, LARGE, huge, Huge
  • 這些大小描述是相對於normalsize而言的,而normalsize的大小是在導言區的documentclass[10pt]{article}設定的,一般為 10,11,12 磅。
  • 中文字型還可以使用\zihao{字號值}設定,如同 word 文件一樣。

LaTeX 中的特殊字元

空白字元

  • 空行分段,多個空行等同於 1 個。
  • 英文中多個空格視為 1 個,中文中忽略空格。
  • 當要輸入多個空格時,要使用\quad命令。

控制符

由於許多符號有著特殊的含義,需要轉義:\#, \$, \%, \{, \}, \~, \^, \_, \&等等。

排版符號和 LaTeX 的標誌符號

引號

  • 單引號:數字1左邊的點號表示左單引號,'表示右單引號。
  • 雙引號:兩個左單引號為左雙引號,兩個右單引號為右雙引號。

連字元

  • 短連字元:-
  • 中連字元:--
  • 長連字元:---

LaTeX 中的圖片

  • 在 LaTeX 中使用graphicx這個巨集包插入圖片。
    • 導言區:\usepackage{graphicx}用於引入巨集包。\graphicspath{{目錄1/}, {目錄2/}}用於指定圖片目錄。
    • 語法:\includegraphics[可選引數]{檔名},其中,檔名不加字尾。
    • 支援格式:eps, pdf, png, jpg等。
  • 可選引數,支援指定大小,縮放比例等。
    • scale=0.3,縮放因子。
    • height=2cm, width=0.15\textwidth,指定長寬。
    • angle=-45,指定旋轉角度。

LaTeX 中的表格

直接使用線上 LaTeX 表格編輯器,用命令做表格簡直不能忍。

LaTeX 中的浮動體

使用浮動體,可以對圖片和表格的位置進行靈活管理。這需要使用begin命令來建立浮動體環境。

  • 在環境後,還可以加入允許位置引數:h(here)、t(top)、b(bottom)、p(page),預設為 tbp。
  • 使用caption用於標題的顯示。
  • 使用subfigure實現圖片的混排。

figure 浮動體

\begin{figure*}[htb]
    \centering
    \subfigure[]{
        \includegraphics[width=0.15\textwidth]{Baboon}
    }
    \subfigure[]{
        \includegraphics[width=0.15\textwidth]{Barbara}
    }
    \subfigure[]{
        \includegraphics[width=0.15\textwidth]{Boat}
    }
    \subfigure[]{
        \includegraphics[width=0.15\textwidth]{Goldhill}
    }
    \subfigure[]{
        \includegraphics[width=0.15\textwidth]{Lena}
    }
    \caption{Six test images: (a) Baboon. (b) Barbara. (c) Boat . (d) House. (e) Lena.}
    \label{fig6}
\end{figure*}

table 浮動體

\begin{table*}[htbp]
    \centering
    \caption{Comparison of four methods on test images in \myref{fig6}{Fig} based on QF = 60}
    \label{tab1}
    \begin{tabular}{ccccccc}
    \hline
    \multirow{2}{*}{\textbf{Images}}  &  \multicolumn{6}{c}{\textbf{Payload(bits) / PSNR(dB)}}         \\ \cline{2-7}
                                      &             & 4000    & 6000    & 8000    & 10000   & 120000  \\ \hline
    \textbf{Baboon}                   & Huang et al & 41.6292 & 39.6411 & 38.3768 & 37.0705 & 36.3569 \\ \hline
                                      & Wedaj et al & 42.4274 & 40.7553 & 39.4249 & 37.7216 & 36.7317 \\ \hline
                                      & Hou et al   & 44.3296 & 42.0547 & 40.2064 & 38.6490 & 37.3514 \\ \hline
                                      & Proposed    & 43.4202 & 41.3573 & 39.8080 & 38.5896 & 37.4733 \\ \hline
    \textbf{Barbara}                  & Huang et al & 43.6504 & 41.8896 & 40.5739 & 38.9833 & 37.5838 \\ \hline
                                      & Wedaj et al & 43.4992 & 42.1739 & 40.5565 & 39.4102 & 38.6302 \\ \hline
                                      & Hou et al   & 45.9022 & 42.9165 & 40.5833 & 39.1323 & 37.8757 \\ \hline
                                      & Proposed    & 44.9008 & 42.7855 & 41.2021 & 39.9609 & 38.8448 \\ \hline
    \textbf{Boat}                     & Huang et al & 44.1428 & 42.1220 & 40.4407 & 38.9576 & 37.7936 \\ \hline
                                      & Wedaj et al & 44.0611 & 41.4388 & 40.4950 & 39.0900 & 38.0499 \\ \hline
                                      & Hou et al   & 45.0766 & 42.6497 & 40.8703 & 39.5630 & 38.4274 \\ \hline
                                      & Proposed    & 44.2932 & 42.0677 & 40.4485 & 39.2217 & 38.2122 \\ \hline
    \textbf{House}                    & Huang et al & 44.3822 & 42.4522 & 41.2621 & 40.2653 & 39.4005 \\ \hline
                                      & Wedaj et al & 42.7945 & 42.6797 & 41.4402 & 40.1956 & 39.1621 \\ \hline
                                      & Hou et al   & 44.9146 & 42.8700 & 41.2894 & 40.0333 & 39.0089 \\ \hline
                                      & Proposed    & 44.9769 & 42.8880 & 41.4127 & 40.3581 & 39.3878 \\ \hline
    \textbf{Lena}                     & Huang et al & 46.5117 & 44.6028 & 42.7461 & 41.1829 & 39.6356 \\ \hline
                                      & Wedaj et al & 45.0363 & 43.1874 & 41.6598 & 40.3515 & 39.3753 \\ \hline
                                      & Hou et al   & 46.2696 & 43.8200 & 41.8304 & 40.2713 & 38.7410 \\ \hline
                                      & Proposed    & 46.3358 & 44.0285 & 42.3115 & 40.8457 & 39.5046 \\ \hline
    \end{tabular}
\end{table*}

LaTeX 中的公式

直接使用線上 LaTeX 公式編輯器,用命令直接寫公式簡直不能忍,簡單公式還行,複雜公式。。。

行內公式

  • 美元符號:$f(x)=a^2x+bx+c$
  • 小括號:\(f(x)=a^2x+bx+c\)

行間公式

  • 雙美元符號:$$f(x)=a^2x+bx+c$$
  • 中括號:\[f(x)=a^2x+bx+c\]

自動編號公式

使用equation環境,該公式會單獨一行。

\begin{equation}
    y = x + 1
\end{equation}

參考文獻 BibTeX

將參考文獻單獨處理,實現一次管理,多次使用。

  • 新建檔案mybibifile.bib
  • 在導言區:\bibliographystyle{plain},此外還有alpha, abbrv等排序規則。
  • 在需要輸出參考文獻的地方使用:\bibliography{mybibfile.bib}
  • 預設下是不會出現未引用的文獻,如果要強制加上為引用文獻:在上一個命令前加\nocite{*}

其他命令

  • texdoc ctex:終端命令,檢視 ctex 巨集包手冊。
  • texdoc lshort-zh:終端命令,檢視 latex 使用教程。
  • \label{xxx}:設定標籤名為 xxx,以用於交叉引用。
  • \ref{xxx}:引用標籤名為 xxx 的內容。
  • \cite{xxx}:引用 bib 檔案的文章。
  • \newcommand{命令名}[引數個數]{具體定義}:例如,\newcommand{\PRC}{People's Republic of China}\newcommand{\loves}[2]{#1 喜歡 #2}\loves{貓}{魚}\newcommand{\myref}[1]{Eq.\ref{#1}}
  • \begin{enumerate} xxx \end{enumerate}:有序列表環境。
  • \begin{itemize} xxx \end{itemize}:無序列表環境。
  • \item xxx:列表中的單個元素。
  • \geometry{left=2cm, right=2cm, top=2cm, bottom=2.5cm}:設定頁邊距。
  • \\:在文字中表示換行。