显示标签为“Latex”的博文。显示所有博文
显示标签为“Latex”的博文。显示所有博文

星期三, 一月 09, 2013

LaTeX/Special Characters



LaTeX logo.svg
In this chapter we will tackles everything related to input encoding, typesetting diacritics and special characters.
In the following document, we will refer to special characters for all symbols other than A-Za-z0-9 and english punctuation marks.
This chapter is tightly linked with the font encoding issue. You should have a look at Fonts on the topic.
Some languages usually need a dedicated input system to ease document writing. This is the case of Arab, Chinese, Japanese, Korean and others. This specific matter will be tackled in Internationalization.
The rules for producing characters with diacritical marks, such as accents, differ somewhat depending whether you are in text mode, math mode, or the tabbing environment.

Contents

  [hide] 

[edit]Input encoding

[edit]A technical matter

Most of the modern computer systems allow you to input letters of national alphabets directly from the keyboard. If you tried to input these special characters in your LaTeX source file and compiled it, you may have noticed that they do not get printed at all.
A LaTeX source document is a plain text file. A computer stores data in a binary format, that is a sequence of bits (0 and 1). To display a plain text file, we need a code which tells which sequence of bits corresponds to which sequence of characters. This association is called input encoding, character encoding, or more informally charset.
For historical reasons, there are many different input encodings. There is an attempt to unify all the encoding with a specification that contains all existent symbols that are known from human history. This specification is Unicode. It only defines code points, which is a number for a symbol, but not the way symbols are reprensented in binary value. For that, unicode encodings are in charge. There is also several unicode encodings available, UTF-8 being one of them.
The ASCII encoding is an encoding which defines 128 characters on 7 bits. Its widespread use has lead the vast majority of encodings to have backward compatibility with ASCII, by defining the first 128 characters the same way. The other characters are added using more bits (8 or more).
This is actually a big issue, since if you do not use the right encoding to display a file, it will show weird characters. What most programs try to do is guess statistically the encoding by analyzing the frequent sequences of bits. Sadly, it is not 100% safe. Some text editors may not bother guessing the encoding and will just use the OS default encoding. You should consider that other people might not be able to display directly your input files on their computer, because the default encoding for text file is different. It does not mean that the user cannot use another encoding, besides the default one, only that it has to be configured. For example, the German umlaut ä on OS/2 is encoded as 132, with Latin1 it is encoded as 228, while in Cyrillic encoding cp1251 this letter does not exist at all. Therefore you should consider encoding with care.
The following table shows the default encodings for some operating systems.
Operating systemDefault Encodings
Western LatinCyrillic
Modern Unices (*BSD, Mac OS X, GNU/Linux)utf-8utf-8
Mac (before OS X)applemacmaccyr
Unix (Old)latin1koi8-ru
Windowsansinew, cp1252cp1251
DOS, OS/2cp850cp866nav
UTF-8 and Latin1 are not compatible. It means that if you try to open a Latin1-encoded file using a UTF-8 decoding, it will display odd symbols only if you used accents in it, since both encoding are ASCII superset they encode the classic letters the same way. There aren't many advantages in using Latin1 over UTF-8, which is technically superior. UTF-8 is also becoming the most widely used encoding (on the Web, in modern Unices, etc.).

[edit]Dealing with LaTeX

TeX uses ASCII by default. But 128 characters is not enough to support non-english languages. TeX has its own way to do that with commands for every diacritical marking (see Escaped codes). But if we want accents and other special characters to appear directly in the source file, we have to tell TeX that we want to use a different encoding.
There are several encodings available to LaTeX:
  • ASCII: the default. Only bare english characters are supported in the source file.
  • ISO-8859-1 (a.k.a. Latin 1): 8-bits encoding. It supports most characters for latin languages, but that's it.
  • UTF-8: a Unicode multi-byte encoding. Supports the complete Unicode specification.
  • Others...
In the following we will assume you want to use UTF-8.
There are some important steps to specify encoding.
  • Make sure your text editor decodes the file in UTF-8.
  • Make sure it save your file in UTF-8. Most text editors do not make the distinction, but some do, such as Notepad++.
  • If you are working in a terminal, make sure it is set to support UTF-8 input and output. Some old Unix terminals may not support UTF-8. PuTTY is not set to use UTF-8 by default, you have to configure it.
  • Tell LaTeX that the source file is UTF-8 encoded.
\usepackage[utf8]{inputenc}
inputenc package tells LaTeX what the text encoding format of your .tex files is.

[edit]Extending the support

The LaTeX support of UTF-8 is fairly specific: it includes only a limited range of unicode input characters. It only defines those symbols that are known to be available with the current font encoding. You might encounter a situation where using UTF-8 might result in error:
! Package inputenc Error: Unicode char \u8:ũ not set up for use with LaTeX.
This is due to the utf8 definition not necessarily having a mapping of all the character glyphs you are able to enter on your keyboard. Such characters are for example
ŷ Ŷ ũ Ũ ẽ Ẽ ĩ Ĩ
In such case, you may try need to use the utf8x option to define more character combinations. utf8x is not officially supported, but can be viable in some cases. However it might break up compatibility with some packages like csquotes.
Another possiblity is to stick with utf8 and to define the characters yourself. It is very easy indeed:
\DeclareUnicodeCharacter{'codepoint'}{'TeX sequence'}
where codepoint is the unicode codepoint of the desired character. TeX sequence is what to print when the character matching the codepoint is met. You may find codepoints on this site. Codepoints are easy to find on the web. Example:
\DeclareUnicodeCharacter{0177}{\^y}
Now inputting 'ŷ' will effectively print 'ŷ'.
With XeTeX and LuaTeX the inputenc package is no longer needed. Both engines support UTF-8 directly and allow the use of TTF and OpenType fonts to support Unicode characters. See the Fonts section for more information.

[edit]Escaped codes

In addition to direct UTF-8 input, LaTeX supports the composition of special characters. This is convenient if your keyboard lacks some desired accents and other diacritics.
The following accents may be placed on letters. Although 'o' letter is used in most of the examples, the accents may be placed on any letter. Accents may even be placed above a "missing" letter; for example, \~{} produces a tilde over a blank space.
The following commands may be used only in paragraph (default) or LR (left-right) mode.
LaTeX commandSampleDescription
\`{o}ògrave accent
\'{o}óacute accent
\^{o}ôcircumflex
\"{o}öumlaut, trema or dieresis
\H{o}őlong Hungarian umlaut (double acute)
\~{o}õtilde
\c{c}çcedilla
\k{a}ąogonek
\lłl with stroke
\={o}ōmacron accent (a bar over the letter)
\b{o}obar under the letter
\.{o}ȯdot over the letter
\d{u}ụdot under the letter
\r{a}åring over the letter (for å there is also the special command \aa)
\u{o}ŏbreve over the letter
\v{s}šcaron/hacek ("v") over the letter
\t{oo}o͡o"tie" (inverted u) over the two letters
To place a diacritic on top of an i or a j, its dot has to be removed. The dotless version of these letters is accomplished by typing \iand \j. For example:
  • \^\i should be used for i circumflex 'î';
  • \"\i should be used for i umlaut 'ï'.
If a document is to be written completely in a language that requires particular diacritics several times, then using the right configuration allows those characters to be written directly in the document. For example, to achieve easier coding of umlauts, the babel package can be configured as \usepackage[ngerman]{babel}. This provides the short hand "o for \"o. This is very useful if one needs to use some text accents in a label, since no backslash will be accepted otherwise.
More information regarding language configuration can be found in the Internationalization section.

[edit]Less than < and greater than >

The two symbols '<' and '>' are actually ASCII characters, but you may have noticed that they will print '¡' and '¿' respectively. This is a font encoding issue. If you want them to print their real symbol, you will have to use another font encoding such as T1, loaded with the inputenc package. See Fonts for more details on font encoding.
Alternatively, they can be printed with dedicated commands:
\textless
\textgreater

[edit]Euro € currency symbol

When writing about money these days, you need the euro sign. The textcomp package features a \texteuro command which is quite far from the official symbol. An official version of the euro symbol is provided by eurosym. Load it in the preamble (optionnaly with the officialoption):
\usepackage[official]{eurosym}
then you can insert it with the \euro{} command. Finally, if you want a euro symbol that matches with the current font style (e.g., bold, italics, etc.) you can use a different option:
\usepackage[gen]{eurosym}
again you can insert the euro symbol with \euro{}.
Alternatively you can use the marvosym package which also provides the official euro symbol.
\usepackage{marvosym}
% ...

\EUR{}
Now that you have succeeded in printing a euro sign, you may want the '€' on your keyboard to actually print the euro sign as above. There is a simple method to do that. You must make sure you are using UTF-8 encoding along with a working \euro{} or \EUR{}command.
\DeclareUnicodeCharacter{20AC}{\euro{}}
% or
\DeclareUnicodeCharacter{20AC}{\EUR{}}
Complete example:
\usepackage[utf8]{inputenc}
\usepackage{marvosym}
\DeclareUnicodeCharacter{20AC}{\EUR{}
}

[edit]Degree symbol for temperature and math

A common mistake is to use the \circ command. It will not print the correct character. Use the textcomp package instead, which provides a\textdegree command.
\usepackage{textcomp}
%...

A $45\textdegree$ angle.
For temperature, you can use the same command or opt for the gensymb package and write
\usepackage{gensymb}
%...

17\degree~C
or
17\celsius
Some keyboard layouts feature the degree symbol, you can use it directly by declaring the unicode character:
\usepackage[utf8]{inputenc}
\usepackage{textcomp}
\DeclareUnicodeCharacter{00B0}{\textdegree{}
}

% ...

17°~C

[edit]Other symbols

LaTeX has lots of symbols at its disposal. The majority of them are within the mathematical domain, and later chapters will cover how to get access to them. For the more common text symbols, use the following commands:
Symbols.png
Not mentioned in above table, tilde (~) is used in LaTeX code to produce non-breakable space. To get printed tilde sign, either write\~{} or \textasciitilde{}. And a visible space ␣ can be created with \textvisiblespace.
For some more interesting symbols, the Postscript ZipfDingbats font is available thanks to the pifont package. Add the declaration to your preamble: \usepackage{pifont}. Next, the command \ding{number}, will print the specified symbol. Here is a table of the available symbols:
ZapfDingbats symbols.

[edit]In special environments

[edit]Math mode

Several of the above and some similar accents can also be produced in math mode. The following commands may be used only in math mode.
LaTeX commandSampleDescriptionText-mode equivalence
\hat{o}\hat{o}circumflex\^
\widehat{oo}\widehat{oo}wide version of \hat over several letters
\check{o}\check{o}vee or check\v
\tilde{o}\tilde{o}tilde\~
\widetilde{oo}\widetilde{oo}wide version of \tilde over several letters
\acute{o}\acute{o}acute accent\'
\grave{o}\grave{o}grave accent\`
\dot{o}\dot{o}dot over the letter\.
\ddot{o}\ddot{o}two dots over the letter (umlaut in text-mode)\"
\breve{o}\breve{o}breve\u
\bar{o}\bar{o}macron\=
\vec{o}\vec{o}vector (arrow) over the letter
When applying accents to letters i and j, you can use \imath and \jmath to keep the dots from interfering with the accents:
LaTeX commandSampleDescriptionSample with upper dot
\hat{\imath}\hat{\imath}circumflex on letter i without upper dot\hat{i}
\vec{\jmath}\vec{\jmath}vector (arrow) on letter j without upper dot\vec{j}

[edit]Tabbing environment

Some of the accent marks used in running text have other uses in the tabbing environment. In that case they can be created with the following command:
  • \a' for an acute accent
  • \a` for a grave accent
  • \a= for a macron accent

[edit]Unicode keyboard input

Some operating systems provide a keyboard combination to input any Unicode code point, the so-called unicode compose key.
Many X applications (*BSD and GNU/Linux) support the Ctrl+Shift+u combination. A 'u' symbol should appear. Type the code point and pressenter or space to actually print the character. Example:
 20AC 
will print the euro character.
Desktop environments like GNOME and KDE may feature a customizable compose key for more memorizable sequences.
Xorg features advanced keyboard layouts with variants that let you enter a lot of characters easily with combination using the aprioriate modifier, like Alt Gr. It highly depends on the selected layout+variant, so we suggest you to play a bit with your keyboard, preceeding every key and dead key with the Alt Gr modifier.

星期三, 九月 16, 2009

LaTeX: Indexes and Glossaries

Indexes/Indices

LaTeX can automatically create indexes while you work on your document. To use LaTeX indexing, you must first include the makeidx command in the preamble. The full command is:
\usepackage{makeidx}
\makeindex

When you want to index something, use the command:
\index{Index Entry}

There are several types of index entries that LaTeX can create. These are:

  • Plain entry: uses the basic command \index{cheese} to make an entry for cheese with the current page number
  • Subindex Entry: uses the basic command with an exclamation point to separate the first main entry from the subentry. The command for a single level of subentry appears as such:
    \index{cheese!gouda}
    Subentries can be up to one additional level deep, and the command appears as such:
    \index{cheese!gouda!brie}
  • Cross-references: Entries which are simple "See" and another entry are created using the vertical bar with this command:
    \index{cheese|see{crackers}}
  • Font changes: To change the style of an index entry, use the @ sign followed by a font change command like this:
    \index{Kraft@\textit{Kraft}}
    This example indexes Kraft and italicizes it at the same time. Any of the standard font change commands which use \text... will work within this syntax.
  • Out of sequence: This can be used to make index entries appear out of sequence, so that they appear with the work they represent. For instance:
    \index{cheese@gouda}
    Would place gouda in the list right next to cheese, as if gouda was actually spelled cheese.

When the document has been processed through LaTeX with the index commands included, it will create a .idx file. You will then need to run the .idx file through the makeindex program by typing:
makeindex filename

The filename here is without a file extension. The program will look for the .idx file and will output a .ind file. This file is used by the command:
\printindex

The print index command goes at the end of your document files (the .tex file) and then takes the information from the .ind file to build and include the full index in your paper.

Glossaries

Glossaries are made in the same manner as indexes. Glossaries simply use the commands:
\makeglossary (this is used in the preamble, like the \makeindex command)
\glossary (this is used for the particular entries, like the \index command)

There are some minor differences in how glossaries and indexes are handled. Please see the books by Lamport or Samarin for more information.

星期五, 九月 11, 2009

让表格中的单元格换行

自定义命令:
让表格中的单元格换行
\newcommand{\tabincell}[2]{\begin{tabular}{@{}#1@{}}#2\end{tabular}}
%然后使用&\tabincell{c}{}&就可以在表格中自动换行

%比如这么用
\begin{tabular}{|c|c|}
\hline
1 & the first line \\
\hline
2 & \tabincell{c}{haha\\ heihei\\zeze} \\
\hline
\end{tabular}

Latex 经验

[经典书籍]
经典的参考书lshort和中文的latex_manual。别的书不推荐,并不等于别的书不好,侧重点不同。
另外常备数学符号手册,有时候需要查询。
bbs.ctex.org,好地方,论坛的气氛超好。

[系统及组合工具]
miktex+winedt是极好的组合,latex编译和dvi、pdf显示等各种操作点击一下即可,完全配置好了,而且对于参考文献的编辑书写很方便,特殊符号也如此。
实际操作当中,对于需要跟老板反复讨论修改的文章,可以使用word写正文的doc文件,只需告诉老板忽略带\的代码以及图片、表格、公式等段落的语法代码。这样,不但可以利用word的语法检查,更可以做到修改的功能,与普通word操作无二。文本内容差不多之后,将doc另存为tex纯文本,编译生成pdf即可。
通常含有交叉引用或者公式等编号的TEX文件,需要反复编译4遍左右,才能达到最终效果,反正winedt中也就是多点几下按钮,倒是省事。
对于表格,使用latable,支持列复制粘贴,很方便数据输入,最后拷贝出Tex代码即可。
对于公式,使用Texaide4(或Mathtype),此软件是把mathtype的内核整了出来的,所以输入公式是直接可视化,最后拷出tex代码即可。有人用SWP来方便修改文章里的公式,也可行。
对于作图:函数公式图、三维图用gnuplot;二维数据图、非线性拟合用origin,导出eps文件;位图用gimp或photoshop;矢量图用其他工具,如dia, smartdraw,制作后直接导出或先打印成pdf然后用adobe另存为eps文件。

[入手须知]
latex用命令行肯定是不爽,所以一定要用winedt。
yap的逆向返回,在yap里设置一下即可。
再次强调,下载一个好的模板做准备,论文或者文章或者实验报告。

[书写经验]
常用的符号输入,埃 (\AA)、百分号 (\%)、~ ($\sim$)
温度符号或者直体希腊字母,可以先定义一些宏,角度 \def\degree{$^{\circ}$} 和摄氏度符号\def\centigrade{$^{\circ}\mathrm{C}$},或者引用现成的符号包。
双倍行距 \renewcommand{\baselinestretch}{1.6} 或者\linespread{1.6}
~?小间距,多用于两个连着的标点之间或者单位之前
“ ”?引号的正确输入
$?数学模式。若需输入希腊字母,也需进此模式
\[ \]?文中公式
若嫌论文每次生成pdf很慢,可采用draft模式,则插图处以黑框显示,最后定稿时再以final模式编译。
编译出错的时候,从最初给出的错误信息看起,后面的可能没用。
简单的子图,可以不用其他包,直接多写几个\includegraphics;需要上下排的时候,\\换行即可。

[图像制作]
eps图像?万能但是愚蠢的办法,是虚拟彩色打印机,分辨率确实高,但是虚拟打印重新得到的eps图像文件的分辨率与打印机设置有关,因为生成的文件有可能比原始文件大很多很多,完全没有必要。因此强烈推荐jpeg2ps这个小软件,命令行转化一下即可。数据作图只需用origin导出即可;若用gnuplot,也很方便。具体建议:如果所写论文里面几乎不含实验数据、矢量形状图,则建议统一使用jpg图像,同时用pdflatex直接生成pdf。

位图制作,使用gimp或ps都可以,gimp可以直接导出eps文件,ps可以采取生成jpg文件然后转化或者生成pdf再用acrobat另存。

矢量作图,使用dia或Inkscape,或者powerpoint!这里提到的几个软件在win32下都有直接的安装文件,然后打印成pdf再另存。

如果所写论文里面含有大量数据图,以及少量AFM等位图,为了保证准确性,建议统一采用eps图像,经latex-dvi-pdf制作。因为origin支持此项输出;对于位图,采用jpeg2ps转化成ps图像,不必剪裁,直接可以作为eps图像文件引用。然后用latex->dvi->pdf来生成最终成品pdf。更可喜的是,这两种方式所生成的ps图像文件,在yap的dvi浏览器里,就完全支持,浏览起来非常方便。除了通过jpg格式转化为eps文件;更通行的办法是打印为pdf文件,然后用acrobat裁剪并另存为eps文件。但是,并不建议采用虚拟ps打印机打印的方式,因为,一,打印的ps图像,可能需要重新用gsview进行剪裁,再进一步生成eps文件;二,打印的ps图像文件与所用的虚拟ps打印机分辨率设置有关,相当于重新采样制作,完全没有必要,反倒有可能图像文件很大。

BTW:据说photoshop制作的ps文件,如果设置的boundbox的话,也是可以被引用的。本人相信这是对的,但是没有深入研究。

[编译生成]
pdflatex->pdf?可直接一步生成pdf,支持jpg文件,方便,但是不支持eps,因此origin的作图成了问题。但是pdflatex竟然支持pdf的图,而任何文档转成pdf格式,都是很方便的,所以这种方法现在也非常可行。

latex->dvi->pdf?先生成dvi,再生成pdf,不支持jpg的图,通常用图都是eps,需要虚拟打印或者转换,缺点是修改eps图片不方便。一般说来,没必要经过ps中转,dvi作中间调整观察,pdf直接看到最终效果即可。
?需要指出的是,dvi预览的时候,暂时不支持的伪eps图会留出空档。
?另外,有交叉引用的时候,可能要多编译几次,这是正常现象。

[常见问题]
有些输入必须在数学环境下,比如最常用的希腊字母的书写方式。
参考文献的引用是用\cite,而表格、图像用的是\ref。用错了的话,那个问号可是一直会在哦。
bibtex的style一般用unsrt,即按照文中出现的次序逐个显示。revtex的模板库默认已经是这个了。
bibtex里的作者项,每个作者用and连接,不要用显示结果的逗号或者分号,因为最终显示的处理效果会有你设定的样式自动决定。
bibtex里的单项最好用{ },而不要用” “,因为用大括号的话,里面可以书写一些德文的怪点点。
每章单独的参考文献,可以用库\usepackage{chapterbib}来实现。
需要[1-4]这样的参考文献引用风格,在使用了bibtex之后,加上\usepackage[sort&compress,sectionbib]{natbib}可以实现。