Download - Viettug vietex-doc-math-tools-help-1
mathtools.sty - công cụ tinh chỉnh công thức
Nguyễn Hữu ĐiểnKhoa Toán - Cơ - Tin học
ĐHKHTN Hà Nội, ĐHQGHN
1 Giới thiệu gói lệnh
Gói lệnh mathtools.sty công cụ tinh chỉnh công thức dùng với amsmath.sty có tại địa chỉ
http://tug.ctan.org/tex-archive/macros/latex/contrib/mh/
Trong bó lệnh ngoài mathtools.sty còn có các gói lệnh khác như1. breqn.sty Tự động ngắt dòng trong môi trường công thức equation.2. empheq.sty co dãn công thức (Đã có bài riêng về gói này)3. flexisym Thay đổi ký hiệu thích hợp với breqn.sty4. mathstyle Quản lý định dang ký tự công thức toán.5. xfrac.sty Chia các phân thức theo thứ bậc to nhỏ trong latex2e.
Trong bài này chỉ giới thiệu mathtools.sty.
2 Sử dụng lệnh
1. Tùy chọn gói lệnh\usepackage[fleqn,tbtags]{mathtools} tương đương với
\usepackage[fleqn,tbtags]{amsmath}
\usepackage{mathtools}
Ngoài ra còn tùy chọnfixamsmath sử lại một lỗi trong gói lệnh amsmath.stydonotfixamsmathbugs không sửa lại lỗi.allowspaces cho phép chữa lại khoảng trống quanh công thứcdisallowspaces không cho phép sửa công thức.
2. Nhiều khi trong gói lệnh amsmath.sty mất chữ khi dùng
\[\begin{gathered}
[p] = 100 \\
[v] = 200\end{gathered}
\]
= 100
[v] = 200
Nhưng với gói lệnh mathtools.sty thì[p] = 100
[v] = 200
1
http://nhdien.wordpress.com - Nguyễn Hữu Điển 2
3. Tinh chỉnh bằng lệnh trong mathtools.sty bằng \mathclap{}
\begin{align*}
X &= \sum_{1\le i\le j\le n} X_{ij} \\X &= \sum_{\mathclap{1\le i\le j\le n}} X_{ij}
\end{align*}
X =∑
1≤i≤j≤n
Xij ←− amsmath.sty
X =∑
1≤i≤j≤n
Xij ←− mathtools.sty
Tương tự cho dong công thức
\begin{align*}
a+b &= \sum_{i+j=10} f(i) g(j) \\&= \sum_{k\geq0} h(k)\\
a+b &= \sum_{\mathclap{i+j=10}} f(i) g(j) \\&= \sum_{\mathclap{k\geq0}} h(k)
\end{align*}
a + b =∑
i+j=10
f(i)g(j)
=∑
k≥0
h(k)
a + b =∑
i+j=10
f(i)g(j)
=∑
k≥0
h(k)
4. Tinh chỉnh bằng lệnh \smashoperator{} mặc định căn vào giữa, với tùy chọn\smashoperator[r]{} căn bên phải, \smashoperator[l]{} căn trái
\begin{align*}V &= \sum_{i\le j \le \le k \le \ell}^{\infty} V_{ijk\ell} \\
V &= \smashoperator{\sum_{i\le j \le \le k \le \ell}^{\infty}} V_{ijk\ell} \\V &= \smashoperator[r]{\sum_{i\le j \le \le k \le \ell}^{\infty}} V_{ijk\ell} \\
V &= \smashoperator[l]{\sum_{i\le j \le \le k \le \ell}^{\infty}} V_{ijk\ell}
\end{align*}
V =∞∑
i≤j≤≤k≤`
Vijk`
V =
∞∑
i≤j≤≤k≤`
Vijk`
V =
∞∑
i≤j≤≤k≤`
Vijk`
V =
∞∑
i≤j≤≤k≤`
Vijk`
http://nhdien.wordpress.com - Nguyễn Hữu Điển 3
5. Dùng lệnh \adjustlimits để chỉnh dòng dưới ngang hàng nhau
{\Huge
\begin{alignat*}{2}& \lim_{n\to\infty} \max_{p\ge n},
& \adjustlimits\lim_{n\to\infty} \max_{p\ge n} \\
& \lim_{n\to\infty} \max_{p^2\ge n},& \adjustlimits\lim_{n\to\infty} \max_{p^2\ge n} \\
& \lim_{n\to\infty} \sup_{p^2\ge nK},& \adjustlimits\lim_{n\to\infty} \sup_{p^2\ge nK} \\
& \limsup_{n\to\infty} \max_{p\ge n},
& \adjustlimits\limsup_{n\to\infty} \max_{p\ge n}\end{alignat*}}
Bên trái là chưa tinh chỉnh, bên phải dùng lệnh tinh chỉnh rồi
limn→∞
maxp≥n
, limn→∞
maxp≥n
limn→∞
maxp2≥n
, limn→∞
maxp2≥n
limn→∞
supp2≥nK
, limn→∞
supp2≥nK
lim supn→∞
maxp≥n
, lim supn→∞
maxp≥n
6. Định dạng lại kiểu bao quanh đánh nhãn
\newtagform{brackets2}[\textbf]{[}{]}
\usetagform{brackets2}
\begin{align}X &= \sum_{1\le i\le j\le n} X_{ij} \\
X &= \sum_{\mathclap{1\le i\le j\le n}} X_{ij}\end{align}
X =∑
1≤i≤j≤n
Xij [1]
X =∑
1≤i≤j≤n
Xij [2]
Trở lại mặc định dùng \usetagform{default}
http://nhdien.wordpress.com - Nguyễn Hữu Điển 4
\begin{align}
X &= \sum_{1\le i\le j\le n} X_{ij} \\X &= \sum_{\mathclap{1\le i\le j\le n}} X_{ij}
\end{align}
X =∑
1≤i≤j≤n
Xij (3)
X =∑
1≤i≤j≤n
Xij (4)
7. Chỉ hiện ra các lệnh có tham chiếu \mathtoolsset{showonlyrefs=true}
\begin{align}
X &= \sum_{1\le i\le j\le n} X_{ij} \label{eq:eq01}\\
X &= \sum_{\mathclap{1\le i\le j\le n}} X_{ij} \label{eq:eq02}\end{align}
X =∑
1≤i≤j≤n
Xij (5)
X =∑
1≤i≤j≤n
Xij
Lệnh nhãn \label{eq:eq01} tham chiếu \refeq{eq:eq01} sẽ có (5) còn \label{eq:eq02}không hiện ra cả trong công thức.
Khi dùng trở lại \mathtoolsset{showonlyrefs=false}
\begin{align}
X &= \sum_{1\le i\le j\le n} X_{ij} \label{eq:eq03}\\X &= \sum_{\mathclap{1\le i\le j\le n}} X_{ij} \label{eq:eq04}
\end{align}
X =∑
1≤i≤j≤n
Xij (6)
X =∑
1≤i≤j≤n
Xij (7)
Lệnh nhãn \label{eq:eq03} tham chiếu \refeq{eq:eq03} sẽ có (6) còn \label{eq:eq04}
vẫn hiện ra cả trong công thức.
http://nhdien.wordpress.com - Nguyễn Hữu Điển 5
8. Trong gói lệnh \usepackage{amsmath} có các môi trường ma trận\begin{align*}
&\begin{matrix}
a_{11} &a_{12} \\a_{21} &a_{22}+b_{12}
\end{matrix} \\
&\begin{pmatrix}a_{11} &a_{12} \\
a_{21} &a_{22}+b_{12}
\end{pmatrix}\\&\begin{bmatrix}
a_{11} &a_{12} \\a_{21} &a_{22}+b_{12}
\end{bmatrix}\\
&\begin{Bmatrix}a_{11} &a_{12} \\
a_{21} &a_{22}+b_{12}
\end{Bmatrix}\\&\begin{vmatrix}
a_{11} &a_{12} \\a_{21} &a_{22}+b_{12}
\end{vmatrix}\\
&\begin{Vmatrix}a_{11} &a_{12} \\
a_{21} &a_{22}+b_{12}
\end{Vmatrix}\end{align*}
a11 a12
a21 a22 + b12(
a11 a12
a21 a22 + b12
)
[
a11 a12
a21 a22 + b12
]
{
a11 a12
a21 a22 + b12
}
∣
∣
∣
∣
a11 a12
a21 a22 + b12
∣
∣
∣
∣
∥
∥
∥
∥
a11 a12
a21 a22 + b12
∥
∥
∥
∥
Trong \usepackage{mathtools} môi trường có \begin{matrix*}[r] và căn các phần tử\begin{align*}
&\begin{matrix*}[r]
a_{11} &a_{12} \\a_{21} &a_{22}+b_{12}
\end{matrix*} \\&\begin{pmatrix*}[r]
a_{11} &a_{12} \\
a_{21} &a_{22}+b_{12}\end{pmatrix*}\\
&\begin{bmatrix*}[r]
a_{11} &a_{12} \\a_{21} &a_{22}+b_{12}
\end{bmatrix*}\\
&\begin{Bmatrix*}[r]a_{11} &a_{12} \\
a_{21} &a_{22}+b_{12}\end{Bmatrix*}\\
&\begin{vmatrix*}[r]
a_{11} &a_{12} \\a_{21} &a_{22}+b_{12}
\end{vmatrix*}\\
&\begin{Vmatrix*}[r]a_{11} &a_{12} \\
a_{21} &a_{22}+b_{12}\end{Vmatrix*}
\end{align*}
a11 a12
a21 a22 + b12(
a11 a12
a21 a22 + b12
)
[
a11 a12
a21 a22 + b12
]
{
a11 a12
a21 a22 + b12
}
∣
∣
∣
∣
a11 a12
a21 a22 + b12
∣
∣
∣
∣
∥
∥
∥
∥
a11 a12
a21 a22 + b12
∥
∥
∥
∥
http://nhdien.wordpress.com - Nguyễn Hữu Điển 6
\usepackage{mathtools}
\begin{matrix*}[l]
\begin{align*}
&\begin{matrix*}[l]a_{11} &a_{12} \\
a_{21} &a_{22}+b_{12}\end{matrix*} \\
&\begin{pmatrix*}[l]
a_{11} &a_{12} \\a_{21} &a_{22}+b_{12}
\end{pmatrix*}\\
&\begin{bmatrix*}[l]a_{11} &a_{12} \\
a_{21} &a_{22}+b_{12}\end{bmatrix*}\\
&\begin{Bmatrix*}[l]
a_{11} &a_{12} \\a_{21} &a_{22}+b_{12}
\end{Bmatrix*}\\
&\begin{vmatrix*}[l]a_{11} &a_{12} \\
a_{21} &a_{22}+b_{12}\end{vmatrix*}\\
&\begin{Vmatrix*}[l]
a_{11} &a_{12} \\a_{21} &a_{22}+b_{12}
\end{Vmatrix*}
\end{align*}
a11 a12
a21 a22 + b12(
a11 a12
a21 a22 + b12
)
[
a11 a12
a21 a22 + b12
]
{
a11 a12
a21 a22 + b12
}
∣
∣
∣
∣
a11 a12
a21 a22 + b12
∣
∣
∣
∣
∥
∥
∥
∥
a11 a12
a21 a22 + b12
∥
∥
∥
∥
9. Thêm môi trường dcases và dcases* để tinh chỉnh, ta so sánh
\[
a=\begin{cases}
E = m c^2 & \text{Nothing to see here} \\\int x-3\, dx & \text{Integral is text style}
\end{cases}\]
a =
{
E = mc2 Nothing to see here∫
x− 3 dx Integral is text style
\[a=\begin{dcases}
E = m c^2 & \text{Nothing to see here} \\
\int x-3\, dx & \text{Integral is text style}\end{dcases}
\]
a =
E = mc2 Nothing to see here∫
x− 3 dx Integral is text style
\[
http://nhdien.wordpress.com - Nguyễn Hữu Điển 7
a=\begin{dcases*}
E = m c^2 & Nothing to see here \\\int x-3\, dx & Integral is text style
\end{dcases*}
\]
a =
E = mc2 Nothing to see here∫
x− 3 dx Integral is text style
10. Khoảng trống trong công thức được chỉnh lại
\usepackage{amsmath}
Trước công thức
a = b (8)
Bên trong công thức
c = d (9)
Sau công thức
\usepackage{mathtools}
Trước công thức
a = b (10)
Bên trong công thức
c = d (11)
Sau công thức
11. dùng lệnh tinh chỉnh chỉ số \prescript
\[
{}^{4}_{12}\mathbf{C}^{5+}_{2} \quad\prescript{14}{2}{\mathbf{C}}^{5+}_{2} \quad
\prescript{4}{12}{\mathbf{C}}^{5+}_{2} \quad
\prescript{14}{}{\mathbf{C}}^{5+}_{2} \quad\prescript{}{2}{\mathbf{C}}^{5+}_{2}
\]
412C
5+
2
142C
5+
2
412C
5+
2
14C
5+
2 2C5+
2
\newcommand*\myisotope[3]{%\begingroup
\mathtoolsset{prescript-sup-format=\mathit,
prescript-sub-format=\mathbf,
prescript-arg-format=\mathrm,}%
\prescript{#1}{#2}{#3}%
\endgroup}
http://nhdien.wordpress.com - Nguyễn Hữu Điển 8
\[\myisotope{A}{Z}{X}\to \myisotope{A-4}{Z-2}{Y}+
\myisotope{4}{2}{\alpha}
\]
AZX→ A−4
Z−2Y + 4
2α
12. Mũi tên có chú thích trên và dưới
\xleftrightarrow[Chữ ở dưới]{chữ ở trên}
\xRightarrow[Chữ ở dưới]{chữ ở trên}
\xLeftarrow[Chữ ở dưới]{chữ ở trên}\xLeftrightarrow[Chữ ở dưới]{chữ ở trên}
\xhookleftarrow[Chữ ở dưới]{chữ ở trên}\xhookrightarrow[Chữ ở dưới]{chữ ở trên}
\xmapsto[Chữ ở dưới]{chữ ở trên}
Đặc biệt hay dùng
\xrightarrow[Chữ ở dưới]{chữ ở trên}\xleftarrow[Chữ ở dưới]{chữ ở trên}
Ví dụ
\[A \xLeftarrow[under]{over} B
\]
Aover⇐===under
B
Tương tự có
\xrightharpoondown[Chữ ở dưới]{chữ ở trên}
\xrightharpoonup[Chữ ở dưới]{chữ ở trên}\xleftharpoondown[Chữ ở dưới]{chữ ở trên}
\xleftharpoonup[Chữ ở dưới]{chữ ở trên}
\xrightleftharpoons[Chữ ở dưới]{chữ ở trên}\xleftrightharpoons[Chữ ở dưới]{chữ ở trên}
13. Ngoặc ở trên và ở dưới
\underbracket[hrule thicknessi] [hbracket heighti]{hargi}\overbracket[hrule thicknessi] [hbracket heighti]{hargi}
$\underbracket {foo\ bar}_{baz}$ foo bar
baz
$\overbracket {foo\ bar}^{baz}$
baz
foo bar
\[
\underbracket[3pt]{xxx\ yyy}_{zzz} \quad \text{and} \quad
\underbracket[1pt][7pt]{xxx\ yyy}_{zzz}\]
http://nhdien.wordpress.com - Nguyễn Hữu Điển 9
xxx yyy
zzz
and xxx yyy
zzz
14. Ngắt tử số hoặc mẫu số trong phân số
\[
a=\frac{
\splitfrac{xy + xy + xy + xy + xy}{+ xy + xy + xy + xy}
}
{z}=\frac{
\splitdfrac{xy + xy + xy + xy + xy}{+ xy + xy + xy + xy}
}
{z}\]
a =
xy + xy + xy + xy + xy+ xy + xy + xy + xy
z=
xy + xy + xy + xy + xy
+ xy + xy + xy + xy
z
15. Môi trường gathered được cải tiến nhiều
\begin{lgathered}[hposi] hcontentsi \end{lgathered}
\begin{rgathered}[hposi] hcontentsi \end{rgathered}\newgathered{hnamei}{hpre_linei}{hpost_linei}{hafteri}
\renewgathered{hnamei}{hpre_linei}{hpost_linei}{hafteri}
Ví dụ
\begin{equation}\begin{lgathered}
x=1,\quad x+1=2 \\
y=2\end{lgathered}
\end{equation}
x = 1, x + 1 = 2
y = 2(12)
Cải tiến môi trường này
\newcounter{steplinecnt}
\newcommand\stepline{\stepcounter{steplinecnt}\thesteplinecnt}
\newgathered{stargathered}{\llap{\stepline}$*$\quad\hfil}% \hfil for centering
{\hfil}% \hfil for centering{\setcounter{steplinecnt}{0}}% reset counter
Áp dụng
http://nhdien.wordpress.com - Nguyễn Hữu Điển 10
\begin{gather}
\begin{stargathered}x=1,\quad x+1=2 \\
y=2
\end{stargathered}\end{gather}
1∗ x = 1, x + 1 = 2
2∗ y = 2(13)
16. Chủ động cách dòng công thức
\begin{spreadlines}{hdimeni} hcontentsi \end{spreadlines}
\begin{spreadlines}{20pt}Cách ra 20pt
\begin{gather}
a=b\\c=d
\end{gather}\end{spreadlines}
Cách ra 20pt
a = b (14)
c = d (15)
Không cách ra.
\begin{gather}
a=b\\c=d
\end{gather}
Không cách ra.
a = b (16)
c = d (17)