感谢支持
我们一直在努力

Ubuntu下配置简易的Tex UTF-8中文环境

在Ubuntu下配置使用Tex中文环境有好几种选择和相关方法,有些比较简单(比如直接用XeTeX),有些则相对麻烦一点(比如手动去生成相关字体)。


现在应用最广的一般是TexLive+CJK,如果想尽量使你的Tex文档在Windows/Linux下互相通用,一般都选择这种组合,而且这也是目前最成熟的(相对于XeTeX)。


今天这篇文章中的方法,是利用目前已经成功的方法,再结合Ubuntu的特定环境所作的。希望能给想在Ubuntu下配置Tex中文环境的朋友一个参考。


先介绍一下之前最流行的在Linux下配置Tex的方法,一般是参考这篇文章:


http://www.linuxidc.com/Linux/2008-02/11351p3.htm


下载1GB左右的ISO档,挂载ISO并安装其中的TexLive,再自己生动生成相关字体文件。一般都能顺利的配置好Tex+UTF-8的中文环境。


但是对于习惯了apt-get的用户,可能不愿意从外部安装Tex套件。因此,我参考了一些文章,琢磨出了这个利用Ubuntu源里的TexLive,再手动生成字体包的方法,优点是:


不需要下载1GB的ISO档,只需要下载我准备的几百KB的字体生成要用的相关文件。
只从Ubuntu源里安装相关Tex套件,不往系统目录写入其他文件,方便删除。
仍需要手动生成字体文件,不过是安装在自己的主目录下。
好。请看详细方法:


一、安装TexLive+Latex+CJK:


打开终端,执行下述命令安装TexLive和常用的一些Latex宏包(可以根据自己的需要增改):


sudo apt-get install texlive texlive-math-extra texlive-latex-base texlive-latex-extra texlive-latex-recommended texlive-pictures texlive-science texlive-bibtex-extra texlive-common latex-beamer


如果硬盘充裕的话,直接完整安装也可以:


sudo apt-get install texlive-full latex-beamer


安装完后,就可以安装CJK的相关软件包了,如果只需要获得中文支持,那么执行:


sudo apt-get install latex-cjk-chinese ttf-arphic-* hbf-*


否则,建议安装latex-cjk-all以获取完整支持。

二、生成中文字体包


安装好TexLive+CJK以后,还需要安装一个软件──fontforge用于生成字体:


sudo apt-get install fontforge


好了,前面所做的,都是标准的Debian式安装,假如哪天你不需要了,直接remove安装即可。


生成字体前,请自己准备你需要生成的字体文件:simsun.ttc,simhei.ttf等,这里以simsun.ttc(宋体)为例。


准备好后下载下面这个包,解压到一个地方,如自己的主目录~/font:

font.tar.bz2


然后把simsun.ttc也复制到~/font里去,执行下面的命令生成字体地图:


cd ~/font
time fontforge -script subfonts.pe simsun.ttc song Unicode.sfd


加time是为了计算时间,因为比较耗时,在我的Core 2 Duo T5500下,生成song花了40分钟,生成hei花了24分钟,仅供参考。


字体生成好了,再建立一个描述文件吧。


在~/font下,建立一个makemap文件,内容如下:


for i in *.tfm
do
cat >> song.map << EOF
${i%.tfm} ${i%.tfm} < ${i%.tfm}.pfb
EOF
done


然后在终端下执行:chmod +x makemap让文件加上执行权限,最后执行:


./makemap


再建立一个一个c70song.fd文件:


% This is c70song.fd for CJK package.
% created by Edward G.J. Lee
% modify by Yue Wang
\ProvidesFile{c70song.fd}
\DeclareFontFamily{C70}{song}{\hyphenchar \font\m@ne}
\DeclareFontShape{C70}{song}{m}{n}{<-> CJK * song}{}
\DeclareFontShape{C70}{song}{bx}{n}{<-> CJKb * song}{\CJKbold}
\endinput


好的,相关文件都已生成,开始复制字体使其生效。


执行下面的命令,在你的主目录下生成隐藏的个人Tex配置,如果你哪天不需要了,也可以删除:


mkdir -p ~/.texmf-var/fonts/map/dvips/CJK
mkdir -p ~/.texmf-var/fonts/tfm/CJK/song
mkdir -p ~/.texmf-var/fonts/type1/CJK/song
mkdir -p ~/.texmf-var/tex/latex/CJK/UTF8


建立完这层层叠叠的目录以后,就把刚刚生成的字体复制进去吧。


cp ~/font/song.map ~/.texmf-var/fonts/map/dvips/CJK
cp ~/font/*.tfm ~/.texmf-var/fonts/tfm/CJK/song
cp ~/font/*.pfb ~/.texmf-var/fonts/type1/CJK/song
cp ~/font/c70song.fd ~/.texmf-var/tex/latex/CJK/UTF8


复制完后就执行命令刷新缓存,让它生效:


sudo texhash
updmap –enable Map song.map


假如一切顺序的话, 就测试一下我们安装的song体是否能用吧。


在任意位置编辑这个文件,然后保存为test.tex,支持UTF-8格式:


\documentclass{article}
\usepackage{CJKutf8}
\begin{document}
\begin{CJK}{UTF8}{song}
你好!这里是Ubuntu下的TexLive+CJK环境!
\end{CJK}
\end{document}


执行分别生成pdf文档和用evince来查看文档:


pdflatex test.tex
evince test.pdf


看看你的过程顺不顺利,生成的PDF档也是下面这样吗?


texlive.png 


用同样的方法搞定hei和kai等常用中文标准字体,开始享受你的Tex吧!


texlive-2.png

TeXLive 2007 CJK Chinese Howto


my email address is yulewang \at gmail \dot com



TeXLive 2007 CJK Chinese HowTO


Yue Wang



 
Attention:
 
In TeXLive 2007, there is no need to use CJK package anymore, XeTeX is good enough to handle Chinese and Open/TrueType fonts. and the key problem of XeTeX, i.e. the font switching problem, is tend to be fixed in the next release of XeTeX. But many old documents are written using macropackages/styles based on CJK or CCT, some of them are still widely used today, so, in some cases it is essential to install these fonts.


 
 
Install TeXLive
_________________
First, install TeXLive.
 
wget ftp://ftp.tsinghua.edu.cn/mirror/CTAN/systems/texlive/Images/texlive2007-live-20070212.iso.zip
unzip texlive2007-live-20070212.iso.zip
mount -o loop texlive2007-live-20070212.iso /mnt
cd /mnt
sudo ./install-tl
 
press I to install.


Setup System Path
_________________
Set the path. Take debian GNU/Linux as example, modify your /etc/environment like this and make a reboot:


PATH=”/usr/local/texlive/2007/bin/i386-linux:/usr/local/matlab/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/usr/games”
LANG=”en_US.UTF-8″
LANGUAGE=”en_US.UTF-8″


Copy The Files Needed
_________________
make a dir called font, copy the font here.
mkdir ~/font
cd ~/font
cp /media/sda/windows/Fonts/simhei.ttf .


copy all the files needed.
sudo apt-get install fontforge
cp /usr/local/texlive/2007/texmf-dist/source/latex/CJK/utils/subfonts/* ~/font/
cp /usr/local/texlive/2007/texmf/fonts/sfd/*.sfd ~/font/


 
 
Generate Font
______________
Then, make the font. It is a good way to test how fast your computer is 🙂 it takes me 3 minutes to generate the fonts[Core Duo 2].
 
Some one complain that the procedure take them 1 hours to do so, this is because you use the new version of fontforge. so be sure to use fontforge 2005.


time fontforge -script subfonts.pe simhei.ttf hei Unicode.sfd


create a file name makemap like this:


for i in *.tfm
do
cat >> hei.map << EOF
${i%.tfm} ${i%.tfm} < ${i%.tfm}.pfb
EOF
done



Make map file:
chmod +x makemap
./makemap


create a file name c70hei.fd for CJK package:


% This is c70hei.fd for CJK package.
% created by Edward G.J. Lee
% modify by Yue Wang
\ProvidesFile{c70hei.fd}
\DeclareFontFamily{C70}{hei}{\hyphenchar \font\m@ne}
\DeclareFontShape{C70}{hei}{m}{n}{<-> CJK * hei}{}
\DeclareFontShape{C70}{hei}{bx}{n}{<-> CJKb * hei}{\CJKbold}
\endinput



Copy Fonts into  TEXMF
_________________
create the local directory to save the font
cd ~/.texlive2007
cd texmf-var
mkdir -p fonts/map/dvips/CJK
mkdir -p fonts/tfm/CJK/hei
mkdir -p fonts/type1/CJK/hei
mkdir -p tex/latex/CJK/UTF8
cp ~/font/hei.map fonts/map/dvips/CJK/
cp ~/font/*.tfm fonts/tfm/CJK/hei
cp ~/font/*.pfb fonts/type1/CJK/hei
cp ~/font/c70hei.fd tex/latex/CJK/UTF8



Update The System and Test
_____________________
Just run:
texhash
updmap –enable Map hei.map



create a test file to test your work.


\documentclass{article}
\usepackage{CJKutf8}
\begin{document}
\begin{CJK}{UTF8}{hei}
你好!
\end{CJK}
\end{document}



latex test.tex
xdvi test.dvi
dvipdfm test.dvi
xpdf test.pdf
pdflatex test.pdf
……


 


Virtual GBK Font(Not Essential)
________________________


The main goal of virtual fonts is to make one font for various encoding systems. We still take simhei as an example to tell the reader how to create one UTF8 font for UTF8 and GBK encodings.


first, Let’s suggest that you have finished the task of creating a utf8 heiti font for LaTeX and you are still in the ~/font directory with all the fonts and scripts undeleted. you also copy all the utf8 fonts into your ~/.texlive2007 directory and they are working with no problem.


run these commands. if you do not delete something , *.tfm and uni2sfd.pl are still in your ~/font dir.
perl uni2sfd.pl hei UGBK.sfd gbkhei gbk
mkdir ~/.texlive2007/texmf-var/fonts/tfm/CJK/gbkhei
mv gbkhei*.tfm ~/.texlive2007/texmf-var/fonts/tfm/CJK/gbkhei
mkdir ~/.texlive2007/texmf-var/fonts/vf
mv gbkhei*.vf ~/.texlive2007/texmf-var/fonts/vf
mkdir ~/.texlive2007/texmf-var/tex/latex/CJK/GBK


create a file named c19hei.fd just like the c70hei.fd in
~/.texlive2007/texmf-var/tex/latex/CJK/GBK


% This is c19hei.fd for CJK package.
% created by Edward G.J. Lee
\ProvidesFile{c19hei.fd}
\DeclareFontFamily{C19}{hei}{\hyphenchar \font\m@ne}
\DeclareFontShape{C19}{hei}{m}{n}{<-> CJK * gbkhei}{}
\DeclareFontShape{C19}{hei}{bx}{n}{<-> CJKb * gbkhei}{\CJKbold}
\endinput



make a test file in *GBK* encoding like this:


\documentclass{article}
\usepackage{CJK}
\begin{document}
\begin{CJK}{GBK}{hei}
你好
\end{CJK}
\end{document}



if you are lucky, you will get the good outputs without problem (no texhash or updmap needed)
Use ttf directedly in pdftex/dvipdfmx[not recommended]
—————————————————-
Well, our pdflatex/dvipdfmx can also use ttf directedly.
create ~/.texlive2007/texmf-var/fonts/map/dvipdfm/cid-x.map


gbkhei@UGBK@    UniGB-UCS2-H    :0:simhei.ttf
hei@Unicode@    unicode    :0:simhei.ttf


move the font simhei.ttf to the truetype directory:
mkdir ~/.texlive2007/texmf-var/fonts/truetype
cp simhei.tff ~/.texlive2007/texmf-var/fonts/truetype


 
create the map generating file makemap-enc


for i in hei*.tfm
do
cat >> hei-enc.map << EOF
${i%.tfm} < ${i%.tfm}.enc < simhei.ttf
EOF
done


generate the map:
chmod +x makemap-enc
./makemap-enc


copy all the file needed
cp hei-enc.map ~/.texlive2007/texmf-var/fonts/map/pdftex/CJK/
mkdir -p ~/.texlive2007/texmf-var/fonts/enc/CJK/hei
cp hei*.enc ~/.texlive2007/texmf-var/fonts/enc/CJK/hei


copy the adobe font:
sudo mkdir ~/.texlive2007/texmf-var/fonts/cmap
sudo apt-get install cmap-adobe-*
cp -a /usr/share/fonts/cmap/* ~/.texlive2007/texmf-var/fonts/cmap


 


test that it works:


The UTF8 encoding test file:


\pdfoutput=1
\pdfmapfile{=hei-enc.map}
\documentclass{article}
\usepackage{CJKutf8}
\begin{document}
\begin{CJK}{UTF8}{hei}
你好
\end{CJK}
\end{document}


The GBK encoding test file(save it in gbk first!):


\pdfoutput=1
\pdfmapfile{=hei-enc.map}
\documentclass{article}
\usepackage{CJK}
\begin{document}
\begin{CJK}{GBK}{hei}
你好
\end{CJK}
\end{document}


Install Ctex macro package
—————————————–


There are two versions of ctex macro package, one for  GBK and one for UTF-8.
First, be sure to install all the chinese fonts using simsun.ttf(If you use
simsun.ttc, break it) simhei.ttf, simli.ttf, simkai.ttf, simfang.ttf,
simyou.ttf just follow the example of simhei before.


then, copy all the macropackages in Liangzi’s CTeXLive-2005
sudo mount -o loop  CTeXLive2005  /mnt
cp -a /mnt/2005/texmf-local/tex/latex/ctex ~/.texlive2007/texmf-var/tex/latex
cp -a /mnt/2005/texmf-local/tex/latex/ctexutf8
~/.texlive2007/texmf-var/tex/latex
cp -a /mnt/2005/texmf-local/tex/latex/ccmap ~/.texlive2007/texmf-var/tex/latex


modify all the fd files there.
for those fd file in ctexutf8 directory, use song,fang … instead of unisong,
unifang, unisongsl,unifangsl….., for those fd files in ctex directory, use
gbksong instead of gbksongsl, gbkfang instead of gbkfangsl….
And dont use \CJKbold , use hei instead.



an example file should like:
in utf8 directory


\ProvidesFile{c70rm.fd}
  [2006/06/09 v0.8 ctex
   font definition file]


\DeclareFontFamily{C70}{rm}{\hyphenchar \font\m@ne}
\DeclareFontShape{C70}{rm}{m}{n}{<-> CJK * song}{}
\DeclareFontShape{C70}{rm}{bx}{n}{<-> CJK * hei}{}
\DeclareFontShape{C70}{rm}{m}{sl}{<-> CJK * song}{}



in ctex directory


\ProvidesFile{c19sf.fd}
  [2006/06/09 v0.8 ctex
   font definition file]
\DeclareFontFamily{C19}{sf}{\hyphenchar \font\m@ne}
\DeclareFontShape{C19}{sf}{m}{n}{<-> CJK * gbkyou}{}
\DeclareFontShape{C19}{sf}{bx}{n}{<-> CJKb * gbkhei}{}
\DeclareFontShape{C19}{sf}{m}{sl}{<-> CJK * gbkyou}{}
\DeclareFontShape{C19}{sf}{bx}{sl}{<-> CJKb * gbkhei}{}
\DeclareFontShape{C19}{sf}{m}{it}{<-> CJK * gbkyou}{}
\DeclareFontShape{C19}{sf}{bx}{it}{<-> CJKb * gbkhei}{}
\endinput



Last,run texhash and test the files.
Utf8 test file


\documentclass{ctexreputf8}
\begin{document}
你好
\end{document}


 GBK test file


\documentclass{ctexrep}
\begin{document}
你好
\end{document}

赞(0) 打赏
转载请注明出处:服务器评测 » Ubuntu下配置简易的Tex UTF-8中文环境
分享到: 更多 (0)

听说打赏我的人,都进福布斯排行榜啦!

支付宝扫一扫打赏

微信扫一扫打赏