我寫了一個中文LaTeX模板,主要解釋中文字體設置、中文豎排。
\documentclass[11pt]{article}
\usepackage[margin=1in]{geometry} % 設置頁邊距
%Ref1: https://latex.lierhua.top/docs/Font-Family/
%Ref2: https://gitcode.csdn.net/65e7dbd31a836825ed78ac91.html
\usepackage{ctex} % 中文
\setCJKmainfont{Alibaba PuHuiTi} % 設置中文字體,阿里普惠體。開源字體還有華為鴻蒙、思源。Roman 衬线字体 \textrm{ } \rmfamily
\setCJKsansfont{HarmonyOS Sans TC} % Sans-serif 无衬线字体 \textsf{ } \sffamily
\setCJKmonofont{Source Han Sans K} %Typewriter 等宽字体(打字机字体) \texttt{ } \ttfamily
%%%%%%%%%%%%%%中文豎排設置開始%%%%%%%%%%%%%
%\setCJKmainfont[RawFeature={vertical:+vert}]{Alibaba PuHuiTi}%中文豎排
%\setCJKsansfont[RawFeature={vertical:+vert}]{HarmonyOS Sans TC} % Sans-serif 无衬线字体 \textsf{ } \sffamily
%\setCJKmonofont[RawFeature={vertical:+vert}]{Source Han Sans K} %Typewriter 等宽字体(打字机字体) \texttt{ } \ttfamily
%% 将纸张順時針旋转90度
%\RequirePackage{atbegshi}
%\AtBeginShipout{%
% \global\setbox\AtBeginShipoutBox\vbox{%
% \special{pdf: put @thispage <</Rotate 90>>}%
% \box\AtBeginShipoutBox
% }%
%}%
%%%%%%%%%%%%%%中文豎排設置結束%%%%%%%%%%%%%
\setmainfont{Verdana} % 設置英文字體
\setsansfont{Tahoma}
\setmonofont{Tahoma}
\linespread{1.5} % 設置1.5倍行距
\usepackage{hyperref} % 超鏈接
\usepackage[style=iso]{datetime2} % 設置日期格式為YYYY--MM--DD
%%%%%% 繁體字模板專用部分,開始 %%%%%%
\renewcommand\contentsname{目錄}
\renewcommand\refname{參考文獻}
%%%%%% 繁體字模板專用部分,結束 %%%%%%
\parindent 0em
\title{\Huge\textbf{一個LaTeX中文模板}}
\author{}
\begin{document}
\maketitle
\tableofcontents
\begin{abstract}
一個LaTeX中文模板。
\end{abstract}
\section{前言}
中文模板baseline:
\begin{verbatim}
\documentclass{ctexart}
\begin{document}
您好,世界。
\end{document}
\end{verbatim}
本文討論如何設置字體。
\section{中文字體package}
中文字體package:ctex,xeCJK。
ctex文章固有部分的標題名稱是中文,簡體字。ctex有默認的中英文字體。對於ctex來說,還可以直接將documentclass改成ctexart\cite{ol},但是那樣格式不同於article,標題在中間,而非左對齊。
xeCJK文章固有部分的標題名稱是英文,例如參考文檔部分標題顯示References,需要手動修改。
ctex的底層是xeCJK[]。
結論是:ctex。
如何設置中英文字體?字體的名稱可在Terminal窗口或DOS窗口輸入命令查詢中文字體。
fc-list :lang=zh family
這方法很酷,其實查詢結果亂,看不懂。
Mac OS可查詢Font book查看所有字體,中文字體單獨列,看起來很清晰。
但真正設置使用哪個值,還需要在Word可選字體中查詢有哪些字體。可是這列表從名字看也無法確定哪些是中文字體,因為示例都是英文字母。
\subsection{字體設置}
字體設置有3層含義。
第一層是文字編碼,比如最初計算機只有ASCII編碼,用一個字節表示一個字或字符,因為英文只有26個字母,大小寫、數字,1個字節可以表示256個字符。後來擴展到別的語言,比如歐洲西文,再擴展到亞洲語言,成為unicode編碼。unicode也有多種類型,UTF8,我國還有GB2312。這些字體在通信中需要,比如短信編碼。
但是這些在LaTeX底層processor處理,作為用戶,我們很少設置這樣的編碼。如果在Terminal中輸入texdoc fontenc,所得的文章\LaTeX\ font encodings\cite{fontenc}講的是文字編碼。
第二層,是字的形狀,這是我們日常所說的『字體』,宋體、黑體、雅黑。商業上有種種產權官司,說要字體收費,是發生在這個層次的故事。免費的字體即開源字體有阿里普惠體、華為鴻蒙、香港思源。
texdoc fontspec打開The fontspec package Font selection for XeLaTeX and LuaLaTeX \cite{fontspec},這篇文章寫的是字體如何設置。
LaTeX字體需要做什麼設置呢?
需要設置3個字體:{\rmfamily 襯線體(阿里普惠體)},{\sffamily 無襯線體 (華為鴻蒙體)},{\ttfamily 等寬打印體(思源)}。襯線體比如宋體,線條有粗有細,還有一些線條的裝飾,比如橫的末尾有個小三角。上述三個開源字體恰好都是無襯線字體,就是線條粗細一樣。中文沒有等寬不等寬之說,每個字等寬。但是這三種字體還是要設置。
因為英文就是標準的三種字體都有。
華為鴻蒙繁體字字體把標點符號都居中了,適合中文豎排。
每一種字體又有不同的shape,典型的英文字體有normal、bold、italic、bolditalic。打開Mac OS的Font book英文字體是這四種類型。但中文就很多shape了,比如阿里普惠體有Light、Medium、Regular、Bold、Heavy。華為鴻蒙Light、Medium、Regular、Italic、Light Italic、Medium Italic、Black、Bold、Black Italic、Bold Italic、Thin、Thin Italic。但是想想看,在Word中我們只用到Bold和Italic。
這一層LaTeX字體設置的命令如:
\begin{verbatim}
\setmainfont{<font>}[<font features>]
\setsansfont{<font>}[<font features>]
\setmonofont{<font>}[<font features>]
\end{verbatim}
例如:
\begin{verbatim}
\setmainfont{texgyrepagella-regular.otf}[
BoldFont = texgyrepagella-bold.otf ,
ItalicFont = texgyrepagella-italic.otf ,
BoldItalicFont = texgyrepagella-bolditalic.otf ]
\end{verbatim}
這是英文設置命令,中文對應的命令是:
\begin{verbatim}
\setCJKmainfont、\setCJKsansfont、\setCJKmonofont
\end{verbatim}
我設置阿里普惠體,但是我不知道怎麼設置Bold和Italic options。
ctex編譯的時候有警告:
\begin{verbatim}
Package fontspec Warning: Font "Songti SC Light" does not contain requested
(fontspec) Script "CJK".
Package fontspec Info: Font family 'SongtiSCLight(0)' created for font 'Songti
(fontspec) SC Light' with options
(fontspec) [Script={CJK},BoldItalicFont={Kaiti SC
(fontspec) Bold},BoldFont={Songti SC Bold},ItalicFont={Kaiti SC}].
(fontspec)
(fontspec) This font family consists of the following NFSS
(fontspec) series/shapes:
(fontspec)
(fontspec) - 'normal' (m/n) with NFSS spec.: <->"Songti SC
(fontspec) Light/OT:language=dflt;"
(fontspec) - 'small caps' (m/sc) with NFSS spec.:
(fontspec) - 'bold' (b/n) with NFSS spec.: <->"Songti SC
(fontspec) Bold/OT:language=dflt;"
(fontspec) - 'bold small caps' (b/sc) with NFSS spec.:
(fontspec) - 'italic' (m/it) with NFSS spec.: <->"Kaiti
(fontspec) SC/OT:language=dflt;"
(fontspec) - 'italic small caps' (m/scit) with NFSS spec.:
(fontspec) - 'bold italic' (b/it) with NFSS spec.: <->"Kaiti SC
(fontspec) Bold/OT:language=dflt;"
(fontspec) - 'bold italic small caps' (b/scit) with NFSS spec.:
\end{verbatim}
所以,ctex設置字體setCJKmainfont是Songti SC Light。
font shape option是:\\
BoldItalicFont={Kaiti SC Bold}\\
BoldFont={Songti SC Bold}\\
ItalicFont={Kaiti SC}]\\
第三層『字體』含義是大小,font size,例如LaTeX說tiny,scriptsize, small,all the way to HUGErrr。還有字體的顏色。
這需要修改title、section title、subsection title、正文等字體的大小,參考fontsize package:texdoc fontsize。
我使用titlesec package修改pagestyle,主要是section標題的字體和大小、頁眉頁腳,我寫在《如何編寫LaTeX package 和class》中。
\section{中文豎排}
『院長大人』博主解釋中文豎排的原理:中文有一種輸出是每個字原地逆時針旋轉90°,就是頭要往左側這樣閱讀。將紙張瞬時針旋轉90°,得到中文豎排格式。\cite{vertical}
中文逆時針旋轉90°\cite{vertq}:
\verb"\setCJKmainfont[RawFeature={vertical:+vert}]{Alibaba PuHuiTi}"
将纸张順時針旋转90度:
\begin{verbatim}
\RequirePackage{atbegshi}
\AtBeginShipout{%
\global\setbox\AtBeginShipoutBox\vbox{%
\special{pdf: put @thispage <</Rotate 90>>}%
\box\AtBeginShipoutBox
}%
}%
\end{verbatim}
\section{繁體字模板section標題}
ctex的標題是簡體字,如果改成繁體字(或者,xeCJK的標題是英文,如果改成中文),需要手工修改。如何修改?比如修改``參考文獻''標題\cite{tangxiaoxiaomi}:\verb"\renewcommand\refname{參考文獻}"。
這樣的標題有哪些呢?見表\ref{tab:1}\cite{chen}。
\begin{table}[h!]
\centering
\caption{標題markup}
\label{tab:1}
\begin{tabular}{|l|l|l|}
\hline
標題顯示名稱 & 標題markup & 預設值\\ \hline
目錄 & contentsname & 目录\\
圖目錄 & listfigurename & 插图\\
表目錄 & listtablename & 表格\\
圖標號 & figurename & 图\\
表標號 & tablename & 表\\
摘要 & abstractname & 摘要\\
索引 & indexname & 索引\\
附錄 & appendixname & 附录 \\
參考文獻 & bibname & 参考文献 \\
證明 & proofname & 证明\\
參考文獻(beamer)& refname & 参考文献 \\
方程式 & algorithmname & 算法\\
接續字樣 & (beamer)& (续)\\
\hline
\end{tabular}
\end{table}
本文修改了目錄、參考文獻。
這篇文章\cite{chen}討論了更多繁體字中文文章需要在ctex package上特別設定的部分,並給出ctex package專用的修改方法。
\section{寫作日期和其他}
系統默認的日期格式為``August 30, 2024'',改成YYYY-MM-DD\cite{date}。
為了生成的PDF文章中,參考文檔的鏈接可直接點擊,使用hyperref package。
默認的頁邊距過大,改為1in。
默認的行間距不夠,擴展為1.5倍\cite{gsgbgxp}。
編譯distributor:XeLaTeX。
\begin{thebibliography}{}
\bibitem{tangxiaoxiaomi}
tangxiaoxiaomi, ``Latex里参考文献是Reference,我要写成中文怎么办'', 2017-11-27, \url{https://zhidao.baidu.com/question/1819326097708966228.html}
\bibitem{ol}
Overleaf, ``Chinese'', \url{https://www.overleaf.com/learn/latex/Chinese}
\bibitem{chen}
陳家瑋, ``在 LaTeX 建立中文文件'', 2023-01-22,
\url{https://kuaz.info/posts/2023/01/latex-tutorial-part-ctex/#%E7%9B%AE%E9%8C%84%E9%99%84%E9%8C%84}
\bibitem{gsgbgxp}
gsgbgxp, ``LaTeX中的中英文字体设置、混排'', 2024-01-11, \url{https://blog.csdn.net/gsgbgxp/article/details/124510269}
\bibitem{date}
提問者:Vincent,回答者:user13225,``Date format: YYYY-MM-DD'',回答日期2015-4-6,\url{https://tex.stackexchange.com/questions/152392/date-format-yyyy-mm-dd}
\bibitem{fontspec}
WILL ROBERTSON With contributions by Khaled Hosny, Philipp Gesang, Joseph Wright, and others, ``The fontspec package Font selection for X LEATEX and LuaLATEX v2.9a", 2024/02/13, \url{http://wspr.io/fontspec/}
\bibitem{fontenc}
Frank Mittelbach, Robin Fairbairns, Werner Lemberg, ``LATEX font encodings", 18 February 2016
\bibitem{vertical}
院长大人,``使用xelatex进行中文竖排的一些尝试",2021-04-03,https://zhuanlan.zhihu.com/p/362046672
\bibitem{vertq}
小鬼,雾月,``关于中文竖排文本的三个问题",2022-04-24,\url{https://ask.latexstudio.net/ask/question/7407.html}
\end{thebibliography}
\end{document}
首頁: