2013年11月23日星期六

LaTex中文字体库的添加


最近帮朋友测试一个中文LaTex模版(上海交大硕士论文)的时候出现了一些中文字体的问题,总结如下。

首先是字体库的下载。由于Mac自带的中文字体并不完整,需要自己下载。在下面的第一个链接中有Adobe的四套字体和隶书,幼圆,这样就有了6套完整的中文字体。第二个链接只包括Adobe的四套字体,另外两个需要自己再到其他地方下载。

http://tinker-bot.googlecode.com/files/cfonts.tar.gz

http://ishare.iask.sina.com.cn/f/15105086.html

关于字体的安装,有以下几种方法:

  1. 在Mac的应用程序中选择“字体册”,点击添加即可,这样会把几种字体保存到用户的字体目录下,具体的目录是:/Users/***/Library/Fonts/
  2. 手动将字体文件拷贝到字体目录下,有几个选择(还有一些其他的搜索目录,这里就不一一列出了):
    • /Library/Fonts/
    • /usr/share/fonts

建议把中文6种基础字体都放在系统默认的字体目录下,即采取第二种方法,不然的话如果采用Tex源文件采用下面的开头:

\documentclass{ctexart} % 采用Mac字体
\documentclass[adobefonts]{ctexart} % 采用Adobe字体

会得到类似这样的报错:

...Font \@tempfontb="Adobe Heiti Std/ICU" at 10.54pt not loadable: Metric (TFM) file or installed font not found....

具体原因不知,个人猜测这是因为Texlive不在系统默认字体文件夹外寻找基本字体。

下面的步骤是在Texlive中对中文字库进行更新。由于Texlive默认的中文字体并不在Mac系统中(如“SimSun”),现在安装的新字体名称(如“STSong”)和原来的并不匹配。所以需要手动更改Texlive中win和adobe字体的名称:

win字体:以管理者的身份修改这个文件

/usr/local/texlive/2011/texmf-dist/tex/latex/ctex/fontset/ctex-xecjk-winfonts.def

更新内容如下:

\setCJKmainfont[BoldFont={STHeiti},ItalicFont=STKaiti]
  {STSong}
\setCJKsansfont{STHeiti}
\setCJKmonofont{STFangsong}

\setCJKfamilyfont{zhsong}{STSong}
\setCJKfamilyfont{zhhei}{STHeiti}
\setCJKfamilyfont{zhkai}{STKaiti}
\setCJKfamilyfont{zhfs}{STFangsong}
\setCJKfamilyfont{zhli}{LiSu}
\setCJKfamilyfont{zhyou}{YouYuan}

\newcommand*{\songti}{\CJKfamily{zhsong}} % 宋体
\newcommand*{\heiti}{\CJKfamily{zhhei}}   % 黑体
\newcommand*{\kaishu}{\CJKfamily{zhkai}}  % 楷书
\newcommand*{\fangsong}{\CJKfamily{zhfs}} % 仿宋
\newcommand*{\lishu}{\CJKfamily{zhli}}    % 隶书
\newcommand*{\youyuan}{\CJKfamily{zhyou}} % 幼圆

\endinput

对于adobe字体:以管理者的身份修改这个文件:

/usr/local/texlive/2011/texmf-dist/tex/latex/ctex/fontset/ctex-xecjk-adobefonts.def

更新内容如下:

\setCJKmainfont[BoldFont=Adobe Heiti Std,ItalicFont=Adobe Kaiti Std]
  {Adobe Song Std}
\setCJKsansfont{Adobe Heiti Std}
\setCJKmonofont{Adobe Fangsong Std}

\setCJKfamilyfont{zhsong}{Adobe Song Std}
\setCJKfamilyfont{zhhei}{Adobe Heiti Std}
\setCJKfamilyfont{zhfs}{Adobe Fangsong Std}
\setCJKfamilyfont{zhkai}{Adobe Kaiti Std}
\setCJKfamilyfont{zhli}{LiSu}
\setCJKfamilyfont{zhyou}{YouYuan}

\newcommand*{\songti}{\CJKfamily{zhsong}} % 宋体
\newcommand*{\heiti}{\CJKfamily{zhhei}}   % 黑体
\newcommand*{\kaishu}{\CJKfamily{zhkai}}  % 楷书
\newcommand*{\fangsong}{\CJKfamily{zhfs}} % 仿宋
\newcommand*{\lishu}{\CJKfamily{zhli}}    % 隶书
\newcommand*{\youyuan}{\CJKfamily{zhyou}} % 幼圆

\endinput

由上面的代码可知,两套字体的隶书和幼圆是公用的,其他的四类基本字体各有不同。这样修改过系统便会寻找到对应的字体,不再报错了。

最后打开一个Terminal窗口,用下面的命令刷新一下字体库:

sudo  mkfontscale
sudo mkfontdir
sudo fc-cache -fsv

然后通过fc-list命令就可以检测系统中所有的字体了。如果上面的步骤都操作正确的话,新字体会显示在列表中。使用XeLaTex编译的时候,如果按照前一篇文章介绍的方法,只要调用字体的时候使用正确的名称即可。如果直接用“ctexart”文件类的话,可以参考下面的例子:

\documentclass[UTF8]{ctexart} % 采用Mac字体
%\documentclass[UTF8,adobefonts]{ctexart} % 采用Adobe字体

%以上两行分别测试华文字体和Adobe字体,请交替使用(月下独酌 注)

\title{\LaTeX 中文设置之高层方案}
\author{xiaoyong}
\date{\today}

\begin{document}
\maketitle

\begin{center}
  1. 字体示例:\\
  \begin{tabular}{c|c}
    \hline
    \textbf{\TeX 命令} & \textbf{效果}\\
    \hline
    \verb|{\songti 宋体}| & {\songti 宋体}\\
    \hline
    \verb|{\heiti 黑体}| & {\heiti 黑体}\\
    \hline
    \verb|{\fangsong 仿宋}| & {\fangsong 仿宋}\\
    \hline
    \verb|{\kaishu 楷书}| & {\kaishu 楷书}\\
    \hline
    \verb|{\lishu 隶书}| & {\lishu 隶书}\\
    \hline
    \verb|{\youyuan 幼圆}| & {\youyuan 幼圆}\\
    \hline
  \end{tabular}
\end{center}

\begin{center}
  2. 字号示例:\\
  {\zihao{0}初号}
  {\zihao{1}一号}
  {\zihao{2}二号}
  {\zihao{3}三号}
  {\zihao{4}四号}
  {\zihao{5}五号}
  {\zihao{6}六号}
  {\zihao{7}七号}
  {\zihao{8}八号}
\end{center}

\end{document}

以上所有例子都在Mac下测试成功,使用的界面TexShop Version 2.29 (2.29)。



Reference:

http://bbs.stuhome.net/thread-1306061-1.html
http://kqwd.blog.163.com/blog/static/4122344820117725633776/

2013年10月27日星期日

Maybe You’re Just Making Excuses

Maybe it's my own fault.

Maybe I led you to believe it was easy when it wasn't.

Maybe I led you to believe that my highlights started from the free throw line and not in the gym.

Maybe I made you believe that every shot I took was a game-winner, that my game was built on flash and not fire.

Maybe it's my fault that you didn't see that failure gave me strength, that my pain was my motivation.

Maybe I led you to believe that basketball was a God-given gift and not something I worked for every single day of my life.

Maybe I destroyed the game or maybe you're just making excuses.




Counter Numbering Configuration in LaTeX

When using some template to write our documents, especially those classical and well-structured ones, such as PhD Thesis, we might run into some undesirable features bound to that template. For instance, the counter of footnote is reset for each page, namely if you have 10 footnotes created by the standard "\footnote" commands all over your document, but they are distributed on 10 different pages, then the numberings will all be "1", instead of "1", "2",...,"10". This also happens for linguistic examples created by the "linguex" package, whose counter will be reset for every chapter.

In LaTeX, the objects coming with an automatic numbering, such as tables, figures, footnotes, etc., contain an internal counter definition:

  1. \newcounter{counter_name}
  2. \newcounter{counter_name}[resetting_environment]
The second differs from the first by adding a resetting environment, which appears as once the environment changes, the counter will be reset to 0. That is why some counters are continuous across the whole document, while some others are reset at certain point.


Since the initialization of the counter is embedded in the definition of each object, which is really difficult to modify directly, so in order to be completely flexible on configuring the numbering, we can use the "chngcntr" package. There are basically a pair of commands that we can use:

  • \counterwithin{1}{2} or \counterwithin*{1}{2}
    • These commands help to add a resetting environment to a counter without it
    • The first compulsory argument denotes the counter name, for instance "footnote" for footnotes, "table" for tables, "ExNo" for linguistic examples, etc.
    • The second compulsory argument denotes the environment within which you want to reset the counter, for instance, "chapter", "section", etc.
  • \counterwithout{1}{2} or \counterwithout*{1}{2}
    • These commands help to eliminate a resetting environment for a counter under its influence
    • Same as above, the first compulsory argument denotes the counter name
    • Same as above, the second compulsory argument denotes the environment within which you want to eliminate the resetting effect for a counter

The starred version, "\counterwithin*" or "\counterwithout*" will suppress the redefinition of another counter-related command "\the{counter_name}", for more details, please look at the reference.




Reference:

http://mirrors.linsrv.net/tex-archive/macros/latex/contrib/chngcntr/chngcntr.pdf
http://en.wikibooks.org/wiki/LaTeX/Counters

Common Units for Spaces in LaTeX

There are many places where we need specify exact spaces in LaTeX, for instance, inserting an image, arranging the position of several tables, etc. Normally in the template codes on Internet, people will find units such as "cm" and "in", which stand for "centimeter" and "inch" respectively. Then are there other units? And if yes, what do they mean?
  1. in - inches 
  2. mm - millimeters 
  3. cm - centimeters 
  4. pt - points (about 1/72 inch) 
  5. em - approximately the width of an "M" in the current font 
  6. ex - approximately the height of an "x" in the current font



Conflict between The "ulem" Package and Bibliography

As we mentioned in a previous article, the "ulem" package provides various convenient ways to specify underlines.

However, after importing the "ulem", one might find the bibliography changes in an unexpected way: the strings that should actually be italicized are now underlined. That is because the italicized subparts of the bibliography is configured by the "\emph" command, and with the "ulem" package, "\emph" is redefined as underlined.

To avoid the annoying conflict, we just need to specify a normal emphasis option for the "ulem" package:

\usepackage[normalem]{ulem}

This is indicated at the beginning of the "ulem" documentation.





Reference:

http://mirror.ibcp.fr/pub/CTAN/macros/latex/contrib/ulem/ulem.pdf

2013年10月26日星期六

Change the Font for Your LaTeX Document

Sometimes, it is relatively easy and intuitive to determine which PDF file is created by LaTeX, due its distinct trademark font by default, which is quite different from other popular editors such as MS Office. However, LaTeX, as a professional editor, also supports various font setting. However, it is slightly more complicated than the WYSIWYG editors.

A detailed introduction to the LaTeX font system could refer to the first reference below. Basically, it is recommended that a single document should apply a uniform font, simply because that is more formal and beautiful. Thus, what we have to modify is actually the so-called "the font family".

The fonts could be systematically grouped into three main categories: roman (rm) or serif, sans serif (sf) and monospace (tt). For each category, LaTeX provides a default font, Computer Modern Roman, Computer Modern Sans Serif, and Computer Modern Typewritter, respectively. Further more, we can use the following three variables to refer to each of them:



  • \rmdefault
  • \sfdefault
  • \ttdefault
If not specification is made, the system will apply the serif or default roman font (rm), namely the Computer Modern. If we want to change it into the default sans serif (sf), namely Computer Modern Sans Serif, or the default monospace (tt), namely Computer Modern Sans Typewriter, we can use the following commands:

\renewcommand*{\familydefault}{\sfdefault}

\renewcommand*{\familydefault}{\ttdefault}

Besides the three default font families, LaTeX also provides some pre-installed font families, such as Palatino (ppl), Times (ptm), Helvetica (phv), Courier (pcr), etc. In principle, if the specific font belongs to the "rm" category, we just need to use the following command:

\renewcommand*\rmdefault{ppl}

Otherwise, we will have to first modify the system default font family category to "sf" or "tt" with the already mentioned commands respectively, then specify the particular font in a similar way:

\renewcommand*\sfdefault{pbk}

\renewcommand*\ttdefault{pcr} 


Or if you do not want to be so disciplined, you can just assign the particular font family to the system default roman font, namely:

\renewcommand*\rmdefault{pbk}


Please note that only abbreviations are accepted. For a complete list of pre-installed font families and their abbreviations, refer to the last reference.




Reference:

http://en.wikibooks.org/wiki/LaTeX/Fonts
http://www.tug.dk/FontCatalogue/
http://www.macfreek.nl/memory/Fonts_in_LaTeX

Some Useful Commands in The "tabular" Environment

When you are making tables in LaTeX, if you want to give interpolate some fancier features besides the standard and rigid outlook, there are some commands you are most like to encounter:

  • multirow: for creating a single row spanning over multiple rows
\multirow{Number of Rows}{*}{Content}
  • multicolum: for creating a single colum spanning over multiple columns
\multicolumn{Number of Rows}{Position of Alignment}{Content}
  • cline: for creating a horizontal line break with arbitrary width
\cline{Beginning Column-Ending Column}



Reference:

http://en.wikibooks.org/wiki/LaTeX/Tables

LaTeX中文支持-CJKutf8

之前的一篇文章中介绍过一种使用LaTeX处理中文文档的方法,即在XeLaTeX编译环境下调用fontspec多语言包。但是我们会发现XeLaTeX编译常常花费比一般LaTex编译更多的时间,而且有时候我们并不是要整篇文章都使用中文,而只是在大段英文中插入少量的中文。这里将介绍另外一种常见的在LaTeX中解决中文的方法:CJKutf8。

CJK 有两个基本宏包:
CJKCJKutf8,后者针对于UTF-8编码。由于多语言并行情况下,UTF-8编码更加通用,所以我们倾向于使用后者(中文的另一种常见编码是GBK):

\usepackage{CJKutf8}

如果要在文中插入中文,使用以下命令即可:

\begin{CJK}{UTF8}{gkai}

约翰有一辆车。
\end{CJK}

\begin{CJK}后的第一个参数是编码,由于之前我们已经选定了面向UTF-8的
CJKutf8,所以这里最好就使用UTF8。

第二个参数是中文字体,可以从以下四种字体中选择:bkai,bsmi,gkai,gbsn。前两者是Big5编码的明体(其实就是宋体和楷体字),他们的字库都非常小,许多汉字都无法正常显示。后两者分别是简体中文楷体字和宋体字,大部分汉字的显示都没有问题,可以根据个人喜好加以选择。


Reference:

http://tumutanzi.com/archives/tag/cjk
http://hi.baidu.com/jerryyou517/item/b3f2f52c74bd2f9ab7326344

2013年9月4日星期三

Footnote in Tables

With the standard configuration, one might not be able to insert a footnote with the plain command:

\footnote{...}

The result is that we can see the footnote icon, but the content is nowhere in the whole article. This is because the footnote command is sheltered by the tabular environment.

To achieve the possibility of inserting footnote in tables, we can actually cover the tabular environment with another handy environment, which is provided in the "footnote" package:

\usepackage{footnote}

\begin{savenotes}
\begin{table}
\begin{tabular}{|c|c|}
...
\end{tabular}
\end{table}
\end{savenotes}

Then within the table, the footnote command can be used freely like in other parts of the text.



Reference:
http://texblog.org/2012/02/03/using-footnote-in-a-table/

2013年7月31日星期三

Underlining in LaTex

The package "ulem" provides a nice facility for employing various types of underlines, both in the text and in mathematical formulas.

Some commonly applied commands:
  • \uline{a single underline}:单下划线
  • \uuline{a double underline}:双下划线
  • \uwave{a single under wavy line}:单波浪线
  • \sout{a single deleting line}:单删除线
  • \xout{a complex deleting block}:删除区域
  • \dashuline{a single under dotted line}:单下滑虚线
  • \dotuline{a single series of under dots}:单下滑点



Reference:

2013年3月20日星期三

LaTeX中文支持-fontspec

由于LaTeX设计之初,仅仅是考虑到了拉丁字母符号的输出。当它成为流行全球的编辑工具以后,避免不了针对其他语言符号的支持,中文就是其中之一。

网上有许多LaTeX在中文环境下的编译方法,最常用的即是使用CJK宏包。近期突然发现在XeLaTeX编译下,如果使用“fontspec”包也能顺利支持中文环境。“fontspec”的使用非常方便,很重要一个原因是它能够直接调用已经转载在电脑系统里面的各种字体,而不需要另外导入。一般电脑中标配的中文字体包括黑体(Hei),楷书(Kai),仿宋(STFangsong)等,具体的名称每个系统都不相同,需要自己单个查找。如果想查看自己电脑中的所有字体,可以使用以下命令:

fc-list

此外,如果希望使用系统中不存在的字库,“fontspec”也提供了导入方法:

\fontspec[ExternalLocation=dir]{字体.ttf}
  • 基本配置
那么,如何在tex文档中使用“fontspec”呢?具体方法如下:

\usepackage{fontspec}
\setromanfont{Kai}
\setmonofont[Scale=0.8]{AppleGothic Regular}

上面的第二行设置使用的是楷书,只需要在其中把“Kai”替换为自己喜欢的自己即可。第三行的设置目的是为了获得等宽字形。

如果需要在文中使用不同于默认设置的字体,也可以使用“fontspec”命令,只需要把更改的内容放在一个大括号范围内即可:

{
\fontspec{Hei}
黑体字的内容。。。
}
  • 自动换行问题
由此可知,使用“fontspec”来编辑中文环境非常方便,但是这种方法的一个常见问题就是不能正确断行,文字内容经常突兀地跑出页面边界。因此,人们通常同时使用下面两条命令:


\XeTeXlinebreaklocale "zh"
\XeTeXlinebreakskip = 0pt plus 1pt minus 0.1pt


这样,你就可以放心地使用LaTex来编辑自己的中文文档了。但是不要忘记编译的时候选用XeLaTeX,如果还是照常实用LaTex系统会报错。以上方法已经在Mac OS的TeXShop环境下测试成功。




Reference:
http://bbs.ctex.org/forum.php?mod=viewthread&tid=41552
http://forum.ubuntu.org.cn/viewtopic.php?t=61206

2013年1月25日星期五

Suppress Page Number in LaTex

Most LaTex document classes automatically import the page numbering system, which frees the user from the boring and tiring manual numbering process.

However, we might not always hope to include the page numbers, for instance, in some short abstracts, posters, or planning including only formulas. As a result, we would then need to suppress/remove/hide page numbers. The following codes could be handful:


\maketitle
\thispagestyle{empty}


This will simply suppress the page number on the exact page where you put the code. If you already use some external templates which define the numbering mechanism, you should add one line more in order to suppress the numbers at all pages:


\maketitle
\thispagestyle{empty}
\pagestyle{empty}

LaTeX Spacing

Normally when we are using some predefined LaTex template, the margin/space information is already well-specified. Sometimes, we might want to modify them in order to better fit our constraints, e.g., restricted page numbers.

The package "geometry" does exact this job, it is often included as:

\usepackage{geometry}

To assign more flexibility on it, the following code could be applied:

\usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}

Values for each space (top, bottom, left, right) could be configured by the user himself.

There are also other packages for spacing setting, for instance, "sectsty", "mdwlist" and etc, more reference could be found here.