Skip to content

Instantly share code, notes, and snippets.

@neolee
Created May 3, 2012 10:46
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save neolee/2584972 to your computer and use it in GitHub Desktop.
Save neolee/2584972 to your computer and use it in GitHub Desktop.
XeLaTeX Sample
%!TEX TS-program = xelatex
%!TEX encoding = UTF-8 Unicode
% 以上设定默认使用 XeLaTeX 编译,并指定 Unicode 编码,供 TeXShop 自动识别
% XeLaTeX 示例
\documentclass[12pt]{article}
% XeTeX 配合 fontspec 可以非常方便的设置字体
\usepackage{fontspec}
% fontspec 套件指令
% 预设字体:内文使用楷体,也可以使用任何 OSX 系统里已激活的字体名称!
% 其他设置:
% 沿用 Latex 的一些习惯的标点转换,例如 en-dash 以两个减号表示
% 如果此字体内置 ligatures 定义则启用
% 斜体用 Times Italic (严格来说只有拉丁字母有斜体)
% 粗体用黑体
\setromanfont[Mapping=tex-text, %
Ligatures={Required,Common}, %
ItalicFont={Times Italic}, %
BoldFont={Hei}]%
{Kai}
% 设置等宽字体
\setmonofont[Scale=0.8]{Monaco}
% 设置特殊字体,例如这里是准备为日文准备的字体
\newfontfamily{\j}{Osaka}
% 定义 rmfont 的快捷命令,可以用于在中文中显示指定的其他英文字体,参见下面的例子
\newfontinstance\rmfont{Garamond}
\newcommand{\af}[1]{{\rmfont #1}}
% XeTeX 指令
% 重要:使得中文可以正确的断行!
\XeTeXlinebreaklocale "zh"
\XeTeXlinebreakskip = 0pt plus 1pt
\begin{document}
以下内文开始了。你可以自由的使用各种语言和字体!\\
Regular(普通). {\bfseries Bold(粗体)}
我们来试试覆盖预设字体的方法:只在大括号内局部之字串有效。
% \fontspec 用法和前面預設字形之 \setromanfont 一樣,只是可更自由地使用
{
\fontspec{Hei}
中华人民共和国 (简 黑体)
}
用字体别名命令更简单:
{\j Firefox の意外と知らない10の基本機能}
然后回到正常字形,就用正常 \LaTeX\ 的写法使用就行了。
最后看看中英文混排时,如果英文部分想用不同的字体,可以使用开始时定义的 af 指令:
\af{This is Garamond font. The 'field' contains ligatures.}
下面是对照的中文字体里的英文字:
This is Chinese font. The `field' does not have ligatures.
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment