标签:
杂谈 |
分类: 计算机与 Internet |
原文链接:LaTex Math tourial for mathTex
版权均归作者greensky个人所有,任何引用或转载请注明原文地址。
说明:本文对极少部分内容进行了适当的删减。
一、预备知识
LaTex是一个字处理程序,可以使你书写包含复杂数学公式的文档。这里简要说明的只是LaTeX的标记语法中的冰山一角——数学部分。为了使这篇文章简短,这里只讨论最基础的特性。
这篇教程是针对mathTex编写的,mathTex是使有LaTex实现在网页上书定公式的程序。你可以使用你所熟悉的html标记语言来格式化你的文字,你可以书写包含LaTex的字串的<img>标签,来格式化你的数学公式。例如,<img src="/cgi-bin/mathtex.cgi?\sqrt{a^2+b^2}">会在你放置标签的任何位置显示http://www.forkosh.dreamhost.com/mathtex.cgi?/small%20/sqrt{a^2+b^2}。
译者注:当在当前的机器上配置好mathTex环境后,才能使用上述的代码。对于机器上没有安装mathTex环境的用户可以使用http://www.forkosh.dreamhost.com/mimetex.cgi?的在线mathTex服务,方法可以参考文章《在网页中插入数学公式》。
LaTex字符集
很多键盘字符被渲染成和你期望的相同,例如在LaTex中键入
a...z,0...9,+-*/=()[]仅会被渲染为
特殊的,反斜线 \ 总是引入LaTex的特殊符号,如 \alpha,\beta,\gamma...\omega(渲染为 http://www.forkosh.dreamhost.com/mathtex.cgi?/alpha,/beta,/gamma.../omega),或是引会LaTex的特殊命令,如 \sqrt \pi (渲染为 http://www.forkosh.dreamhost.com/mathtex.cgi?/sqrt%20/pi)。当你想显示反斜线时,输入\backslash。
符号和命令
LaTex的特殊符号和命令都是以反斜线\开头,后边跟一个或多个字母字符 a-z, A-Z。符号或命令通常以一个空格或任何不为字母的字符结束。例如,\frac2x可以在\frac 和 2 之间不用加空格,正确的渲染为 http://www.forkosh.dreamhost.com/mathtex.cgi?/frac/pi2。但是\fracx2不能正确的渲染,因为不存在\fracx的命令。因此需要强制加入一个空格,\frac x2 可以渲染为 http://www.forkosh.dreamhost.com/mathtex.cgi?/frac%20x2。
命令参数
如上演示的,\frac需要两个参数,而 \sqrt
只需要一个。相似的,上标 b_i
二、基本名法结构(Basic constructions)
下标、上标和范围(Sub/superscripts and Limits)
除了我们前边的例子,符号可能同时存在上标和下标。例如 A_{u,v}^k渲染为 http://www.forkosh.dreamhost.com/mathtex.cgi?%20A_{u,v}^k。另外,上下标可能包含任意层级的上下标。例如,A_{u_i,v_j}^{k_m^n}会渲染为 http://www.forkosh.dreamhost.com/mathtex.cgi?A_{u_i,v_j}^{k_m^n}。
范围(即上下边界)的写法和上下标类似,例如,
\sum_{i=1}^ni=\frac{n(n+1)}2可能会渲染为 http://www.forkosh.dreamhost.com/mathtex.cgi?/sum/nolimits_{i=1}^ni=/frac{n(n+1)}2。
表达式以 \displaystyle开头可以确保范围直接在运算符的上下。例如
\displaystyle\sum_{i=1}^n i = \frac{n(n+1)}2 渲染为 http://www.forkosh.dreamhost.com/mathtex.cgi?/displaystyle/sum_{i=1}^n%20i%20=%20/frac{n(n+1)}2。
分隔符(Delimiters)
通常的圆括号形式有时候会不美观,例如,
(\frac1{\sqrt2}x+y)(\frac1{\sqrt2}x-y)渲染为 http://www.forkosh.dreamhost.com/mathtex.cgi?(/frac1{/sqrt2}x+y)(/frac1{/sqrt2}x-y)。
而LaTex的命令 \left(...\right) 会处动调整圆括号的大小和其它分隔符,以适应被括起来的内容。例如,
\left(\frac1{\sqrt2}x+y\right)\left(\frac1{\sqrt2}x-y\right)渲染为 http://www.forkosh.dreamhost.com/mathtex.cgi?/left(/frac1{/sqrt2}x+y/right)/left(/frac1{/sqrt2}x-y/right)。
通常的圆括号 (...) 不一定对称,但是 \left(...\right)必须成对出现。任何数量的 \middle|可能会出现在一个\left(...\right) 对之间,以自动调整竖线的尺寸(参见下表第三四个例子)。除了 \left(...\middle|...\right)下面的分隔符也可以自动调整尺寸:
注意最后两个例子,任何左分隔符都可以和 \right. 搭配平衡。类似的,任何右分隔符都可以和之前的 \left. 搭配平衡。 分隔符.可以和其它分隔符搭配平衡,但又不显示任何内容,这可以使你格式化如上最后两个的数学表达式。在这些例子中, \text{}和 {\atop}命令接下来会讨论。
Accents上标(如何翻译accent啊?暂时译为上标吧)
使用LaTex的数学上标 \vec{}可以书写向量。例如, \vec{v}渲染为 http://www.forkosh.dreamhost.com/mathtex.cgi?/vec{v}。有些上标可以应用在多于一个字符的参数。这其中LaTex可以识别的有:
Accent | 例子 | 渲染为 |
\vec{ } | \vec{x} |
http://www.forkosh.dreamhost.com/mathtex.cgi?/large/vec{x} |
\hat{ } | \widehat{ABC} | http://www.forkosh.dreamhost.com/mathtex.cgi?/widehat{ABC} |
\tilde{ } | \widetilde{ABC} |
http://www.forkosh.dreamhost.com/mathtex.cgi?/widetilde{ABC} |
\dot{ } | \dot{\omega} | http://www.forkosh.dreamhost.com/mathtex.cgi?/large/dot{/omega} |
\ddot{ } | \ddot{\omega} | http://www.forkosh.dreamhost.com/mathtex.cgi?/large/ddot{/omega} |
注意,上标可以复合。如
\dot{\vec{v}}渲染为
http://www.forkosh.dreamhost.com/mathtex.cgi?/dot{/vec{v}}。
虽然不是严格意义上的上标(accent),以下单参数的命令非常有用。\not 接受一个单字符参数,而\cancel ,\sout,\overline 和 \underline 都可以接受一个或多个字符作为参数。
Accent | 例子 | 渲染为 |
\not{ } | a \not= b | http://www.forkosh.dreamhost.com/mathtex.cgi?a/not=b |
|
a \not\in \mathbb{Q} | http://www.forkosh.dreamhost.com/mathtex.cgi?a/not/in/mathbb{Q} |
\cancel{ } | \cancel{ABC} | http://www.forkosh.dreamhost.com/mathtex.cgi?/usepackage{cancel}/cancel{ABC} |
\sout{ } | \sout{$ABC$} | http://www.forkosh.dreamhost.com/mathtex.cgi?/parstyle/usepackage{ulem}/sout{$ABC$} |
\overline{ } | \overline{ABC} | http://www.forkosh.dreamhost.com/mathtex.cgi?/overline{ABC} |
\underline{ } | \underline{ABC} | http://www.forkosh.dreamhost.com/mathtex.cgi?/underline{ABC} |
同样也不是上标,以下两个参数的命令同样很有用。\overbrace{
}^{
}两个参数之间需要 ^ ,而
\overset{}{}不需要。类似的,\underbrace{
}_{ }需要下划线 _
,而 \underset{
}{ }不需要。
命令 | 例子 | 渲染结果 |
\overbrace{ }^{ } | \overbrace{a,...,a}^{\text{k a's}} |
http://www.forkosh.dreamhost.com/mathtex.cgi?/overbrace{a,...,a}^{/text{k%20a |
\underbrace{ }_{ } | \underbrace{b,...,b}_{\text{l b's}} |
http://www.forkosh.dreamhost.com/mathtex.cgi?/underbrace{b,...,b}_{/text{l%20b |
\overset{ }{ } | a \overset{\text{def}}{=} b |
http://www.forkosh.dreamhost.com/mathtex.cgi?a%20/overset{/text{def}}{=}%20b |
\underset{ }{ } | c \underset{\text{def}}{=} d |
http://www.forkosh.dreamhost.com/mathtex.cgi?c%20/underset{/text{def}}{=}%20d |
你可以使用 \overbrace{
}^{ }和
\underbrace{
}_{ }
的组合。例如,\underbrace{\overbrace{a...a}^{\text{k
a's}},
\overbrace{b...b}^{\text{l
b's}}}_{\text{k+l
elements}}渲染为 http://www.forkosh.dreamhost.com/mathtex.cgi?/underbrace{/overbrace{a...a}^{/text{k%20a
。
函数名称
写入 sin^2\theta+cos^2\theta渲染为 http://www.forkosh.dreamhost.com/mathtex.cgi?sin^2/theta+cos^2/theta,而 \sin^2\theta+\cos^2\theta渲染为 http://www.forkosh.dreamhost.com/mathtex.cgi?/sin^2/theta+/cos^2/theta。许多普通的函数名都可以被LaTex识别为反斜线命令,并用罗马字体渲染,这些看起来更具有典型数学公式的风格。像 \lim这样一些函数名命令可以典型的渲染下标,如 \lim_{x\to\infty}\frac1x=0渲染为 http://www.forkosh.dreamhost.com/mathtex.cgi?/lim_{x/to/infty}/frac1x=0。最常用的LaTex函数名命令被列在这里。
\arccos | \arcsin | \arctan | \arg | \cos |
\cosh | \cot | \coth | \csc | \deg |
\det_ | \dim | \exp | \gcd_ | \hom |
\inf_ | \ker | \lg | \lim_ | \liminf_ |
\limsup_ | \ln | \log | \max_ | \min_ |
\Pr_ | \sec | \sin | \sinh | \sup_ |
\tan | \tanh |
矩阵
在LaTex中输入矩阵的最简单的方法是 \begin{matrix}a&b\\c&d\end{matrix},它可以渲染为 http://www.forkosh.dreamhost.com/mathtex.cgi?/begin{matrix}a&b//c&d/end{matrix} ; 。用\left(...\right)括起来,可以获得 http://www.forkosh.dreamhost.com/mathtex.cgi?/left(/begin{matrix}a&b//c&d/end{matrix}/right)。另外,\begin{pmatrix} ... \end{pmatrix}可以自动把矩阵用圆括号括起来。矩阵在每一行的最后用 \\换行,在一行中用 &分列。一个通用的 mxn的矩阵可以写为以下的形式:
\begin{matrix} | |||||||
a1,1 | & | a1,2 | & | . . . | & | a1,n | |
a2,1 | & | a2,2 | & | . . . | & | a2,n | |
. . . | . . . | . . . | . . . | . . . | . . . | . . . | |
am,1 | & | am,2 | & | . . . | & | am,n | |
|
|
|
|
|
|
|
\end{matrix} |
每一个元素 ai,j 可以包含任何有效的LaTex表达式,即使是另一个矩阵也可以。写入 &则相应的位置会留白。例如,\begin{pmatrix} a&\\&b \end{pmatrix}可以渲染2x2的对角线矩阵 http://www.forkosh.dreamhost.com/mathtex.cgi?/begin{pmatrix}%20a&//&b%20/end{pmatrix}。你可以在任何时候用 \\结束一行。所以之前的 a&\\&b可以写为 a\\&b。
比\begin{matrix}......\end{matrix}更一般的是 \begin{array}{lcr}......\end{array}表达式,它需要额外的{lcr} 式的参数用以说明每列元素的对齐方式。这三个字母分别代表left center 和 right,你要为阵列中的每一列提供一个字母。所以 \begin{array}{lcr}......\end{array} 指定一个三列的阵列,对于每一行,第一个元素左对齐,第二个元素居中对齐,第三个元素右对齐。注意,\begin{matrix}......\end{matrix}等效为 \begin{array}{ccc...}......\end{array},所以矩阵是居中对齐的。
假设你想要显示一个四个方程的方程组,你可以使用一个四行三列(rcl)的阵列,阵列的第二个元素为一个 = 号,第一个和第三个元素分别是方程的左边和右边的表达式。例如,
\begin{array}{rcl}
a+b+c+d&=&4\\
2a-d&=&b+c\\
2b&=&c+d-a\\
c-d&=&b-2a\end{array}
渲染为:
http://www.forkosh.dreamhost.com/mathtex.cgi?/begin{array}{rcl}a+b+c+d&=&4//2a-d&=&b+c//2b&=&c+d-a//c-d&=&b-2a/end{array}或者,你可以用来演示一些简单的代数运算,你可以在第一行后的第一个元素留空。如,
http://www.forkosh.dreamhost.com/mathtex.cgi?/begin{array}{rcl}(a+b)^3&=&(a+b)^2(a+b)//&=&(a^2+2ab+b^2)(a+b)//&=&(a^3+2a^2b+ab^2)+(a^2b+2ab^2+b^3)//&=&a^3+3a^2b+3ab^2+b^3/end{array}LaTex提供了多种额外的方法用以显示阵列类的数据,你会发现他们特别有用。但这些讨论超出了这篇教程的范围。
三、另外的技巧
间隔
LaTex有自已的间隔方式,用户输入的空格永远不会被显示。例如,(abcd), (a bcd),
(a b
对于嵌入任意间距,命令\hspace{} 接受一个数值参数来指定间距的宽度(单位为点)。例如,(ab\hspace{9pt}cd\hspace{25pt}ef)渲染为 http://www.forkosh.dreamhost.com/mathtex.cgi?(ab/hspace{9pt}cd/hspace{25pt}ef)。
文本
键入 abcd渲染为 http://www.forkosh.dreamhost.com/mathtex.cgi?abcd,使用的是LaTex默认的数学字体。如果要插入说明性的文字,\text{abc
def}和 \mbox{abc
def}渲染为 http://www.forkosh.dreamhost.com/mathtex.cgi?/text{abc%20def}
y=\left\{ {x/2\mbox{ if $x$ even} \atop (x+1)/2\text{ if odd}} \right
渲染为 http://www.forkosh.dreamhost.com/mathtex.cgi?y=/left/{{x/2/mbox{%20if%20$x$%20even}%20/atop%20(x+1)/2/text{%20if%20odd}}
字体大小
用以选择字体尺寸的LaTex命令有 \tiny, \small, \normalsize(默认值), \large, \LARGE , \huge 和 \Huge。 但是这些命令会在数学标记中被禁止,MathTex把这些命令从数学标记中移出来,充许使用这些字体大小的命令。例如,
\tiny\sqrt{a^2+b^2}
\small\sqrt{a^2+b^2}
\nomalsize\sqrt{a^2+b^2}
\large\sqrt{a^2+b^2}
\LARGE\sqrt{a^2+b^2}
\huge\sqrt{a^2+b^2}
\Huge\sqrt{a^2+b^2}
这些尺寸直接影响整个表达式。没有简单的方法去渲染在一个具有不同尺寸的表达式中,例如,\frac {\small a}{\large b}不起作用。
四、例子
这里有更的例子进一步演示LaTex的特性的用法。部分特性在上边的教程中没有讨论过,你可以参考附录去理解。
译者注:这里我们只讨论颜色的使用。
\usepackage{color}\color{red}\small e^x=\sum_{n=0}^\infty\frac{x^n}{n!} 渲染为 http://www.forkosh.dreamhost.com/mathtex.cgi?/usepackage{color}/color{red}/small%20e^x=/sum_{n=0}^/infty/frac{x^n}{n!}。
其它的颜色可以为 blue 、green。
五、LaTex的符号集
你已经看到一些LaTex的符号,如\theta, \beta 和\omega,以及一些LaTex命令,如\frac, \sqrt 和 \sum。在LaTex综合符号表中包含了LaTex支持的超过4900个符号。一些基本的数学符号和运算符号被放在下表中。
数学、罗马、美术字体、脚本、黒板体、黑体
简单的输入 a-z,A-Z渲染为 http://www.forkosh.dreamhost.com/mathtex.cgi?a-z,A-Z,使用的LaTex默认的数学字体。输入\mathbf{a-z,A-Z}渲染为 http://www.forkosh.dreamhost.com/mathtex.cgi?/mathbf{a-z,A-Z},使用的是数学黑体。而输入 \text{a-z,A-Z}渲染为 http://www.forkosh.dreamhost.com/mathtex.cgi?/text{a-z,A-Z},使用的是罗马字体。
除了数学体和罗马体,LaTeX还支持多种有用的字体,这其中有:
- 黑板黑体(Blackboard bold) \mathbb{A-Z} 渲染为http://www.forkosh.dreamhost.com/mathtex.cgi?/mathbb{A-Z}。
- 美术字体(Calligraphic) \mathcal{A-Z} 渲染为 http://www.forkosh.dreamhost.com/mathtex.cgi?/mathcal{A-Z}。
- 脚本字体(script) \usepackage{mathrsfs}\mathscr{A-Z}渲染为 http://www.forkosh.dreamhost.com/mathtex.cgi?/usepackage{mathrsfs}/mathscr{A-Z}。
你通常一次只键入一个这种字体的字符,如 x\in\mathbb{R}_+渲染为 http://www.forkosh.dreamhost.com/mathtex.cgi?x/in/mathbb{R}_+。但是,和\text{}类似,在{}之间允许任何数量的字符。
http://www.forkosh.dreamhost.com/mathtex.cgi?/usepackage{mathrsfs}/begin{array}{希腊字符
LaTeX支持的希腊字符表。
http://www.forkosh.dreamhost.com/mathtex.cgi?/begin{array}{
数学符号和关系符号
在LaTeX的\display模式中,一些运算符会自动提升为较大的尺寸。例如,f(x)=\int_{-\infty}^xe^{-t^2}dt
渲染为
译者注:上面三个图表是直接用LaTex排版在线生成的,查看网页的源文件,就可以看到它们的LaTeX源码。http://www.forkosh.dreamhost.com/mathtex.cgi?/small/begin{array}{
六、LaTeX参考书目
这篇教程讨论了LaTeX的数学排版能力,而省略了对文体格式化的内容。大部分参考文章介绍的是LaTeX的数学排版,但同时也注重对LaTeX文档排版的整体介绍。TeX用户组提供了在线资源和图书资源,你会发现它们很有用。
在线LaTeX资源
在在线资源中,有两篇简短的介绍LaTex数学排版的教程,它们分别是 LaTeX Math和 LaTeX Math II。《The not so Short Introduction to LaTeX》是一篇非常棒的全面介绍LaTeX的文章。译者注:以前拜读过这篇文章,很不错,入门必读!
LaTeX书籍
如果你只是对LaTeX在线数学排版感兴趣(而不是对文档排版),你或许不想在这些书籍上花费时间和金钱。但是万一你的确想买一本书,在所有的书籍中,下边这些书或许是你想要的:
- 中级:《A Guide to LaTeX2e, 4th ed》是一本权威易读的书,对数学部分讨论很不错。
- 高级:多达1100页,不适合作为教程和入门书籍,但提供了LaTeX最详尽的参考,这本书就是《The LaTeX Companion, 2nd ed》Frank Mittelbach and Michel Goossens, Addison-Wesley 2004, ISBN 0-201-36299-6 ($59.99)。
- 数学:Math into LaTeX, 3rd ed., George Gratzer, Birkhauser Boston 2000, ISBN: 0-8176-4131-9 注重数学部分。第一个版本的部分内容在线提供。
—全文完—