TA

TA

LaTeX 入門

初識 LaTeX#

TODO:LaTeX 的意義,用途,安裝

編譯 tex 文檔

xelatex gougu.tex % 初次編譯
bibtex gougu.aux % 生成引用
xelatex gougu.tex % 再次編譯,添加引用

一些 tex 源碼示例

\maketitle % 生成 title
\tableofcontents % 生成 TOC(需要至少編譯兩次)
\usepackage{graphicx} % 使用宏包
\includegraphics[width=3cm]{input.pdf} % 還可使用其他圖片格式:.png, .jpg, .eps 等
\hline % horizontal line (用於表格)

% 環境
\begin{figure}[ht] % Here Top(圖片浮動的位置設定)
  \centering
  \includegraphics[scale=0.6]{xiantu.pdf} \caption{這是圖片的說明。} % xiantu.pdf 是圖片,還可使用其他格式
  \label{fig:xiantu} % label 用於文中的說明引用
\end{figure}

一些宏包的說明#

  • fontenc % font encoding
  • fontspec
  • ccfonts % concrete

用英文字符指定希臘字符

\usepackage[greek,english]{babel}
\greektext{abcde}

TODO:fontenc,inputenc

Q\@.E\@.D\@. 即“證明完畢”。 % \@. (防止縮寫字母中的 period 被認為是句子結尾 fullstop)

\CJKsetecglue{} % set e(nglish) c(hinese) glue to "no glue"

\phantom{嗯} % 占位,但是不顯示內容
\vphantom % 類似 \phantom
\hphantom % 類似 \phantom

\\ % linebreak, 也可使用 `\linebreak`
\par

{\You {\emph should} emphasis it.}
\textit{
You {\emph should} emphasis it.}
\newcommand\Emph{\textbf}
\underline % make text appeared with an underline

\S
\dag
\ddag % double dagger
\texttrademark % 不帶 text 是文本模式和數學模式通用的

% symbol
\symbol{'\$} % WYSIWYG
\symbol{'Z}
\symbol{"6a} % #hex(16 進制)
\symbol{'07} % #oct(8 進制)

% font family
\textrm{text in Roman Family} \rmfamily
\textsf{text in Sans Serif font Family} \sffamily
\texttt{text in Typewritter font family} \ttfamily % t(ypewritter) t(ext)
% font shape
\textup{text in Upright shape} \upshape
\textit{text in Italic shape} \itshape
\textsl{text in Slanted shape} \slshape
\textsc{text in Small Capital} \scshape % small caption
% font series
\textmd{median text} \mdseries
\textbf{bold face} \bfseries
% default setup
\rmfamily\mdseries\upshape

% CJK font family
{\CJKfamily{hei}這是黑體}
{\CJKfamily{kai}這是楷書}
{\CJKfamily{song}這是宋體}
{\CJKfamily{fs}這是仿宋}

{\songti 宋體}
{\heiti 黑體}
{\fangsong 仿宋}
{\kaishu 楷書}

關於字體

  • New Font Selection Scheme (NFSS)
  • 字體編碼
    1. OT1
    2. T1
    3. OML
    4. OMS
    5. OMX
    6. U
    7. etc.
  • 字體族
    1. cmr
    2. cmss
    3. cmtt
    4. cmm
    5. cmsy
    6. cmex
  • 字體系列
    1. m
    2. b
    3. bx
    4. sb
    5. c
  • 字體形狀
    1. n

    2. it

    3. sl

    4. sc

Set Font

% Set font --- method 1
\fontencoding{OT1}\fontfamily{pzc}\fontseries{mb}\fontshape{it}\fontsize{14}{17} % size, line
\selectfont % 使其生效
So far so good
% Set font --- method 2
\usefont{T1}{pbk}{db}{n} % 編碼,族,系列,形狀
Not too bad

txfonts

\documentclass{article}
\usepackage{txfonts} % free, excellent
\begin{document}
\end{document}

fontspec

% \setmainfont[<optional>]{<font-name>}
% setmainfont, setsansfont, setmonofont
\usepackage{fontspec}
\setmainfont{Times New Roman}
\setsansfont{Verdana}
\setmonofont{Courier New}

fonts

  • Type 1
  • TrueType
  • OpenType
% size of font
\tiny
\scriptsize
\footnotesize
\small
\large
\Large
\LARGE
\huge
\Huge
% chinese font size
\zihao{0} % 初號
\zihao{-0} % 小初號
\zihao{5} % 五號 (c5size)
\zihao{-5} % 小五號 (cs5size)
% default font size
\normalfont % 10pt, or c5size(CTeX)
% line
\linespread{<因子>} % 基本行距的倍數
\setstretch % spacing
\singlespacing
\onehalfspacing
\doublespacing

Dimensions

  • pt point
  • pc pica (12 pt)
  • in inch (72.27 pt, 72 bp)
  • bp big point
  • cm centimeter (2.54 cm = 1 in)
  • mm millimeter
  • dd didot point
  • cc cicero
  • sp scaled point (65536 sp = 1 pt)
  • em M 的寬度(等於 \quad
  • ex x 的高度
  • en

Spaces

% 不可換行的水平間距
\thinspace \, % for numbers: 1\,234\,567\,890
\negthinspace \!
\enspace % 0.5 em
\nobreakable ~ % tie, for names: Donald~E. Knuth

% 可以換行的水平間距
\quad % 1 em
\qquad % 2 em
\enskip % 0.5 em
\  % 空格
% hspace
\hspace{2em} % Space\hspace{1cm}1\,cm, 
\hspace*{1cm}Text
\hspace{2em plus 1em minus 0.25 em}
left\hspace{\fill}middle\hfill right % align to left, middle, right
left\hspace{\stretch{2}}middle\hspace{\fill}right

\hrulefill % 下劃線
\dotfill % 點
% length
\setlength{\parindent}{8em}
\addtolength{\parindent}{2em}
\newlength\mylen % new a length

Boxs

% mbox makebox
\mbox{cannot be broken}
\makebox[1em]{\textbullet}text
\makebox[5cm][s]{some text} % 寬度,位置(clrs: center, left, right, stretched)
\makebox[0pt][l]{abc}xyz % overlap
abc\llap{xyz} % llap(更高層的表達方式)
\rlap{abc}xyz % rlap
% fbox framebox
\fbox % \setlength{\fboxsep}{2pt}, \framebox % 比 \fbox 強,可自定義
\fboxrule{0.4pt} (default)
% sbox savebox
\sbox \savebox
\newsavebox{\mybox}
\sbox{\mybox}{a box made by me}
\usebox{\mybox}
% set box param
\settowidth{10pt}{content}
\settoheight{10pt}{content}
\settodepth{10pt}{content}
% get box param
\wd{\fbox}
\ht{\mbox}
\dp{\mybox}
\framebox[2\width]{文字寬度兩倍的寬} % width, height, depth, totalheight

Alignment

\indent % generate an indent (\parindent)
% 段落
\parskip % 0pt plus 1pt
\setlength{\parskip}{0pt}
\raggedright % 右側不對齊
\raggedleft
\centering

\begin{center} % or `flushright`, `flushleft`
居中
\end{center}

MISC

% hyphenation 放到導言區
\hyphenation{man-u-script com-pu-ter gym-na-sium}

% Lettrine 首字母超大多行效果
\usepackage{lettrine}
\lettrine{S}{tay} hungry, stay foolish

% shapepar
\usepackage{shapepar}
\heartpar{這一段文字會變成心形} 

% syntonly
\usepackage{syntonly} % syntax only
\syntaxonly
% quote
\begin{quote}
  quot
\end{quote}
% quotation 段首縮進
\begin{quotation}
  Better for multiparagraph quotation
\end{quotation}
% verse
\begin{verse}
  For Poetry
\end{verse}
% abstract
\begin{abstract} % article, report
  Abstract goes here % \abstractname 摘要名稱
  % \CTEXoptions[abstractname={摘\quad 要}]
\end{abstract}

Lists

% enumerate
\begin{enumerate} % numbered
  \item one
  \item two
\end{enumerate}

\begin{enumerate}
  \item 中文
  \item[1a.] 漢語 % set "bullet" by hand
\end{enumerate}

% itemize
\begin{itemize} % bullet
  \item not one
  \item not two
\end{itemize}

% description
\begin{description} %
  \item[\tau] Greek alphabet \tau
\end{description}

% 手動設置
% counter
\item This is number\theenumi % enumi, enumii, enumiii, enumiv
\arabic{enumi}
\alph{enumi}
\roman{enumi}
\Alph{enumi}

% reset
\renewcommand\theenumi{\roman{enumi}}
\renewcommand\labelenumi{\theenumi}

% 頁碼也是一個 counter
\thepage % current page number

% define your own counter
\newcounter{mycnt}
\setcounter{mycnt}{0}
\renewcommand\themycnt{\arabic{mycnt}}
\stepcounter{mycnt}\themycnt
\addtocounter{mycnt}{-1}

% amsmath
\usepackage{amsmath}
\numberwithin{equation}{section}

% 定制列表環境
% general list
\begin{list} % \list{標籤}{設置命令}
\item
\end{list}

\newcounter{mylist}
\begin{list}{\#\themylist}{\usecounter{mylist}}
  \item 中文
  \item English
\end{list}

% 或者使用 enumitem 包
\usepackage{enumitem}
\begin{enumerate}[itemsep=0pt,parset=0pt,label=(\arabic*)] % set up
  \item 中文
  \item English
\end{enumerate}

Theorem

\usepackage{theorem}
% theorem
\newtheorm{thm}{定理} % “定理” 是定理標題
\begin{thm}
  一個定理
\end{thm}
\begin{thm}[勾股定理]
  一個有名字的定理
\end{thm}
% lemma
\newtheorm{lemma}{引理}[chapter] % 按章
\begin{lemma}
   ...
\end{lemma}
% proposition
\newtheorm{prop}[thm]{命題} % thm 是編號的方式
\begin{prop}
  ...
\end{prop}

% 設置 theorem 格式
\usepackage{theorem}
\theoremstyle{changebreak}
\theoremheaderfont{\sffamily\bfseries}
\theorembodyfont{\normalfont}
\newtheorem{definition}{定義}[chapter]
\theoremstyle

% 或者更強大的 `ntheorem` 宏包,或者 `amsthm`
\usepackage{amsthm}
\renewcommand\proofname{證明}
\renewcommand\qedsymbol{\ensuremath{\Box}} % 證明完畢(Q.E.D)

Verbatim

% verbatim:\ver<符號><抄錄內容><符號>
\verb"\LaTeX"
\verb!\LaTeX!
\verb*!1 2  3   4! % *(顯示空格)

% 使用環境
\begin{verbatim}
  #!usr/bin/env perl
  ...
\end{verbatim}
\begin{verbatim*}
   ...
\end{verbatim*}

% 使用宏包
\usepackage{fancyvrb}
\SaveVerb{myverb}|shit[({=-+[/})|
\fbox{套中 \UseVerb{myverb}}

% 程序高亮
\usepackage{listings}
\begin{lstlisting}[language=C]
  ...
\end{lstlisting}
\lstset{ % 整體設置
  column=flexible % 等於 lstset{flexiblecolumns}
  commentstyle=\rmfamily\itshape}

tabbing

\begin{tabbing}
  格式\hspace{3em} \= 作者 \kill % be killed,這一行不會顯示
  Plain \TeX \> 高德納 \\ % \\ 以換行
  \LaTeX \> Leslie Lamport % 最後一行不用換行
\end{tabbing}

footnote

\footnote{This is a footnote}
\footnote[1]{This is another footnote} % 腳注符號是 thefootnote

\begin{minipage}{8em} % 對應地,是 thempfootnote
  這是一個小頁環境\footnote{腳注。}中的腳注
\end{minipage}

\renewcommand\thefootnote{\fnsymbol{footnote}}
\renewcommand\thefootnote{\textcircled{\arabic{footnote}}}

% 更好的方法
\usepackage{pifont}
\renewcommand\thefootnote{\ding{\numexpr171+\value{footnote}}} % 陽文帶圈數字從 171 開始

% footnotemark footnotetext
\begin{tabular}{r|r} % right-align, right-align
  自變量 & 因變量\footnotemark \\ \hline % 添加腳注標記
  $x$ & $y$ 
\end{tabular}
\footnotetext{$y=x^2$。} % 添加腳注內容

% 邊注 \marginpar
有邊注的文字\marginpar[\hfill$\rightarrow$]{\leftarrow 右}
\reversemarginpar
\normalmarginpar
\marginparwidth % 寬度
\marginparsep % 間隔,用 setlength 設置

space

\vspace{<長度>}
\vfill % 即 \vspace{\fill}
% skip
\smallskip % smallskipmount
\medskip % medskipmount
\bigskip % bigskipmount
% bottom
\raggedbottom
\flushbottom

\parbox[<位置>][<高度>][<內容位置>]{<Width>}{<Content>}
前沿\parbox{2em}{不搭後語}。 % 一個有趣的例子
\parbox[t]{2em}{Content} % top bounded
\parbox[b]{2em}{Content} % bottom bounded
\parbox[c]{2em}{Content} % center aligened
\parbox[s]{2em}{Content} % stretched

% rule
\rule[<升高距離>]{<Width>}{<Height}
\rule{1pt}{1em}Middle\rule{1p}
\rule[-0.1em]{1em}{1em} 可用作 Q.E.D.
% struct
\fbox{\strut---}\qquad

title page

\title{雜談勾股定理\thanks{感謝某某對本文的大力支持}} % 生成特殊的感謝腳注
\author{張三在左\\三明堂 \and 李四在右\\四合院}
\data\today % 可以省略

% 設置格式

層次名稱命令說明
-1part\part可選的最高層
0chapter\chapterreport, book 的最高層
1section\sectionarticle 的最高層
2subsection\subsection
3subsubsubsection\subsubsubsectionreport, book
4paragraph\paragraph不編號,不生成目錄
5subparagraph\subparagraph 不編號,不生成目錄
% 全書
\frontmatter % 書的前段
\mainmatter % 書的正文,頁碼重新計數
\backmatter % 書的後段,不編號

\clearpage
\include{lang-natural} % 引入 lang-natural.tex 文檔
\include{a-complex-table}
\includeonly{lang-natural} % 可以在 preamble 設置只引入某一章

一個例子

% main.tex
\documentclass{ctexart}
\input{preamble} % 複雜的導言區
\begin{document}
  ...
\end{document}
% a-complex-table.tex
...
\endinput % 停止導出
這是一個複雜的表格,最好從 main.tex 中分離出來。
請直接編譯主文檔:
  xelatex main.tex

設置 CTeX

% \CTEXsetup[<key>=<value>,etc.]{<對象類型>}
% 對象類型:part, chapter, section, subsection, subsubsection, paragraph, subparagraph
\CTEXsetup[name={第,節}]{section} % 變成 “第 1 節” 這樣
\CTEXsetup[number={\chinese{section}}]{section} % 變成 “第一節” 這樣
\CTEXsetup[format={\raggedright\bfseries}]{section}
% nameformat
% numberformat
% titleformat
% aftername,章節名與後面標題間的內容
\CEXsetup[aftername={\\vspace{2em}}]{part}
% beforeskip, afterskip 前後的 skip 距離
% indent 章節標題的縮進長度

% 增加格式
\CTEXsetup[titleformat+={\fansong}]{subsection} % 思考 “=” 與 “+=” 的區別
\documentclass[a4paper,titlepage]{article} % 標題單獨成頁
% a4paper, a5paper, b5paper, letterpaper, leagalpaper, executivepaper
% landscape 橫向紙張
% oneside, twoside
% 10pt, 11pt, 12pt
% onecolumn, twocolumn
% titlepage, notitlepage
% openany, openright(每章從奇數頁開始)
% leqno 公式左邊編號
% fleqn 公式左對齊,固定長度(默認居中)
% draft 編譯出的 pdf 有提示
% final (默認值)
% openbib

CTEXart, CTEXrep, CTEXbook#

article, report, book vs. ctexart, ctexrep, ctexbook % ctex.sty, ctexcap.sty

ctexcap.sty

\documentclass{ltxdoc}
\usepackage{ctexcap}
\zihao{5}
\begin{document}
  ...
\end{document}

ctex.sty

\documentclass{moderncv} % 簡歷
\usepackage{ctex}
\zihao{-4}
\begin{document}
  ...
\end{document}

About UTF-8

\XeTeXinputencoding "GBK" % XeTeX 處理非 UTF-8 編碼文檔
% ctexutf8, ctexcaputf8 % 類似 documentclass[UTF-8]{ctexart} 之類
% ctexartutf8, ctexreputf8, ctexbookutf8

Geometry

\paperwidth \hoffset
\paperheight \voffset
\topmargin
\headhight \headsep
\textheight \textwidth
\marginparsep % 邊注的 sep
\marginparwidth
\footskip % 為什麼不是 footsep?因為最後一行是變化的,而 footnote 跟在緊跟當頁最後一行,用一個 skip 來分隔

% Set geometry method 1
\usepackage[a4paper,left=3cm,right=3cm]{geometry}

% Set geometry method 2
\usepackage{geometry}
\geometry{a4paper,left=3cm,right=3cm} % 右邊距

Page Style

\pagenumbering{roman}

% Page style:
%            empty:無 head,footnote
%            plain:無 head,footnote 為居中頁碼
%            headings:無 footnote,head 是章節名
%            myheadings:沒有 footnote,head 是頁數和自定義內容

\pagestyle{<風格>} % 設置整體
\thispagestyle{<風格>} % 設置本頁

\documentclass{article}
\pagetsyle{headings}

% 大幅圖片,當前頁使用 plain 風格
\begin{figure}[p]
  \thispagestyle{plain}
  ...
\end{figure}

fancyhdr

\lhead{<contents>} \chead{<c>} \rhead{<c>}
\lfoot{<contents>} \cfoot{<c>} \rfoot{<c>}
\fancyhead[<位置>]{<c>} % E/O + L/C/R, even/odd page number
\fancyfoot[<位置>]{<c>} % E/O + L/C/R, left/center,right
\fancyhf[<位置>]{<c>} % H/F + E/O + L/C/R

% Example:
\documentclass[twoside]{ctexrep}
\usepackage{fancyhdr}
\pagestyle{fancy}
\cfoot{\thepage}
\fancyhead[CO]{張三}
\fancyhead[CE]{論語言}
\fancyfoot[RO,LE]{$\heartsuit$}

% \leftmark \rightmark 通常就是“章”“節”名
\fancyhead[LE,RO]{\slshape \rightmark}
\fancyhead[LO,RE]{\slshape \leftmark}
\fancyfoot[C]{\thepage}

% 重新定義風格
\renewcommand\headrulewidth{0.6pt} % default: 0.4pt
\renewcommand\footrulewidth{0.6pt} % default: 0.4pt

\fancypagestyle{plain}{%
  \fancyhf{}
  \cfoot{--\textit{\thepage}--} % 頁碼形狀
  \renewcommand\headrulewidth{0pt}
  \renewcommand\footrulewidth{0pt}
}

% columns
\documentclass[twocolumn]{article}
%    \onecolumn
%    \twocolumn

Macro

  1. command
    \boldface{boldfaced text}
    
  2. environment
    \begin{tabular}
      ...
    \end{tabular}
    
% \newcommand<命令>[<參數個數>][<首個參數默認值>]{具體定義}
\newcommand\PRC{People's Republic of \emph{China}}
\newcommand\drives[2]{#1 drives #2} % \loves{Driver}{car} === driver drives car
\newcommand\does[3][drives]{#2 #1 #3} % \does{driver}{car} ===> driver drives car, \does{like}{I}{fish} ===> I like fish

% renewcommand 和 呢我command類似,但是用於重定義
\renewcommand\abstractname{內容簡介}

% providecommand 提供一個簡單版本,有別的版本,此版本就被替代
\providecommand\url[1]{\texttt{#1}} % 一個簡單的 URL 實現,只是怕沒有這個命令(但是料想應該有,所以只提供一個“簡陋”的實現)

% 或者兩者組合出現
\providecommand \renewcommand % 不管之前是否有定義,都定義一個新 command

一個複雜的例子

\newcommand\Emph[1]{\emph{#1}}
\newcommand\setEmph[1]{%
  \renewcommand\setEmph[1]{%
    #1{##1}}}

TODO:這部分自己實踐一下

% command vs. environment
{\quote ... \endquote}
\begin{quote}
  ...
\end{quote}

\newenvironment{<環境名>}[<參數個數>][<首參數默認值>]{環境前定義}{<環境後定義>}
\renewenvironment{<環境名>}[<參數個數>][<首參數默認值>]{環境前定義}{<環境後定義>}

\newenvironment{myabstract}[1][摘要] %
  {\small
    \begin{center}\bfseries #1\end{center} 
	\begin{quotation}} % 環境前定義
  {\end{quotation}} % 環境後定義(這裡不能使用環境參數)

劉海洋的一段總結:

定義命令和環境是進行 LaTeX 格式定制、打成內容與格式分離目標的利器。使用自定義的命令和環境把字體、字號、縮進、對齊、間距等各種瑣細的內容包裝起來,賦以一個有意義的名字,可以使文檔結構清晰、代碼整潔、易於維護。在使用宏定義的功能時,要綜合利用各種已有的命令、環境、變量等功能,事實上,前面所介紹的長度變量與盒子,字體字號等內容,大多並不直接出現在正文中,而主要用在實現各種結構畫得宏定義裡。

自動化工具#

\tableofcontents % 至少編譯兩次才出現目錄
\listoffigures
\listoftables

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。