由于新的中英混排方法的引入,這里對于每一個效果提供兩份代碼(如果有必要)。如果沒有特別的需求,應當使用新方法。
## 作者、標題、日期
保存并用 XeLaTeX 編譯如下文檔,查看效果:
新方法:
~~~
\documentclass[UTF8]{ctexart}
\title{你好,world!}
\author{Liam}
\date{\today}
\begin{document}
\maketitle
你好,world!
\end{document}
~~~
舊方法:
~~~
\documentclass{article}
%
% 設置中文字體
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[SlantFont,BoldFont,CJKchecksingle,CJKnumber]{xeCJK}
\newcommand\fontnamehei{SimHei}% 黑體
\newcommand\fontnamesong{SimSun}% 宋體
\newcommand\fontnamekai{KaiTi_GB2312}% 楷體
\newcommand\fontnameyahei{Yahei Mono}% 雅黑
\defaultfontfeatures{Mapping=tex-text}
\setCJKmainfont[BoldFont=\fontnamehei,ItalicFont=\fontnamekai]{\fontnamesong}
\setCJKmonofont{\fontnameyahei}
\setCJKsansfont[BoldFont=\fontnamehei]{\fontnameyahei}
%
% 設置標題、作者、日期
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\title{你好,world!}
\author{Liam}
\date{\today}
\begin{document}
\maketitle
你好,world!
\end{document}
~~~
導言區復雜了很多,但和之前的文檔主要的區別只有兩處(新方法只有一處,此處第 2 條)。
1. 深入定義了中文字體以及斷行邏輯;
* 調用`xeCJK`宏包的時候,添加了一些可選參數;
* 使用`\newcommand`命令,將系統字體名映射為相應的命令,方便維護;
* 在定義主字體的時候,用`[]`添加了一些可選參數,增強功能。
2. 定義了 標題、作者、日期。
在`document`環境中,除了原本的`你好,world!`,還多了一個控制序列`maketitle`。這個控制序列能將在導言區中定義的 標題、作者、日期 按照預定的格式展現出來。
> 使用`titling`宏包可以修改上述默認格式。參考[TeXdoc](http://texdoc.net/texmf-dist/doc/latex/titling/titling.pdf).
## 章節和段落
保存并用 XeLaTeX 編譯如下文檔,查看效果:
新方法:
~~~
\documentclass[UTF8]{ctexart}
\title{你好,world!}
\author{Liam}
\date{\today}
\begin{document}
\maketitle
\section{你好中國}
中國在 East Asia.
\subsection{Hello Beijing}
北京是 capital of China.
\subsubsection{Hello Dongcheng District}
\paragraph{Tian'anmen Square}
is in the center of Beijing
\subparagraph{Chairman Mao}
is in the center of 天安門廣場。
\subsection{Hello 山東}
\paragraph{山東大學} is one of the best university in 山東。
\end{document}
~~~
舊方法:
~~~
\documentclass{article}
%
% 設置中文字體
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[SlantFont,BoldFont,CJKchecksingle,CJKnumber]{xeCJK}
\newcommand\fontnamehei{SimHei}
\newcommand\fontnamesong{SimSun}
\newcommand\fontnamekai{KaiTi_GB2312}%KaiTi
\newcommand\fontnameyahei{Yahei Mono}
\defaultfontfeatures{Mapping=tex-text}
\setCJKmainfont[BoldFont=\fontnamehei,ItalicFont=\fontnamekai]{\fontnamesong}
\setCJKmonofont{\fontnameyahei}
\setCJKsansfont[BoldFont=\fontnamehei]{\fontnameyahei}
%
% 設置標題、作者、日期
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\title{你好,world!}
\author{Liam}
\date{\today}
\begin{document}
\maketitle
\section{你好中國}
中國在East Asia.
\subsection{Hello Beijing}
北京是 capital of China.
\subsubsection{Hello Dongcheng District}
\paragraph{Tian'anmen Square}
is in the center of Beijing
\subparagraph{Chairman Mao}
is in the center of 天安門廣場。
\subsection{Hello 山東}
\paragraph{山東大學} is one of the best university in 山東。
\end{document}
~~~
在文檔類?`article`/`ctexart`?中,定義了五個控制序列來調整行文組織結構。他們分別是
* `\section{·}`
* `\subsection{·}`
* `\subsubsection{·}`
* `\paragraph{·}`
* `\subparagraph{·}`
> 在`report`/`ctexrep`中,還有`\chapter{·}`;在文檔類`book`/`ctexbook`中,還定義了`\part{·}`。
## 插入目錄
在上一節的文檔中,找到`\maketitle`,在它的下面插入控制序列`\tableofcontents`,保存并用 XeLaTeX 編譯兩次,觀察效果:
新方法:
~~~
\documentclass[UTF8]{ctexart}
\title{你好,world!}
\author{Liam}
\date{\today}
\begin{document}
\maketitle
\tableofcontents
\section{你好中國}
中國在 East Asia.
\subsection{Hello Beijing}
北京是 capital of China.
\subsubsection{Hello Dongcheng District}
\paragraph{Tian'anmen Square}
is in the center of Beijing
\subparagraph{Chairman Mao}
is in the center of 天安門廣場。
\subsection{Hello 山東}
\paragraph{山東大學} is one of the best university in 山東。
\end{document}
~~~
舊方法:
~~~
\documentclass{article}
%
% 設置中文字體
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[SlantFont,BoldFont,CJKchecksingle,CJKnumber]{xeCJK}
\newcommand\fontnamehei{SimHei}
\newcommand\fontnamesong{SimSun}
\newcommand\fontnamekai{KaiTi_GB2312}%KaiTi
\newcommand\fontnameyahei{Yahei Mono}
\defaultfontfeatures{Mapping=tex-text}
\setCJKmainfont[BoldFont=\fontnamehei,ItalicFont=\fontnamekai]{\fontnamesong}
\setCJKmonofont{\fontnameyahei}
\setCJKsansfont[BoldFont=\fontnamehei]{\fontnameyahei}
%
% 設置標題、作者、日期
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\title{你好,world!}
\author{Liam}
\date{\today}
\begin{document}
\maketitle
\tableofcontents
\section{你好中國}
中國在East Asia.
中國在East Asia.
\subsection{Hello Beijing}
北京是 capital of China.
\subsubsection{Hello Dongcheng District}
\paragraph{Tian'anmen Square}
is in the center of Beijing
\subparagraph{Chairman Mao}
is in the center of 天安門廣場。
\subsection{Hello 山東}
\paragraph{山東大學} is one of the best university in 山東。
\end{document}
~~~
> 試試交換`\maketitle`和`\tableofcontents`的順序,看看會發生什么。
請注意,在“你好中國”這一節中,兩次“中國在 East Asia.”中夾有一個空行,但輸出卻只有一個換行并沒有空行。這是因為 LaTeX 將一個換行當做是一個簡單的空格來處理,如果需要換行另起一段,則需要用兩個換行(一個空行)來實現。