You are on page 1of 10

mathtools.

sty - công cụ tinh chỉnh công thức

Nguyễn Hữu Điển


Khoa 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.sty
4. 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ọn
fixamsmath sử lại một lỗi trong gói lệnh amsmath.sty
donotfixamsmathbugs không sửa lại lỗi.
allowspaces cho phép chữa lại khoảng trống quanh công thức
disallowspaces 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
X= Xij ←− amsmath.sty
1≤i≤j≤n
X
X= Xij ←− mathtools.sty
1≤i≤j≤n

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*}

X
a+b= f (i)g(j)
i+j=10
X
= h(k)
k≥0
X
a+b= f (i)g(j)
i+j=10
X
= h(k)
k≥0

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*}


X
V = Vijk`
i≤j≤≤k≤`
X∞
V = Vijk`
i≤j≤≤k≤`
X∞
V = Vijk`
i≤j≤≤k≤`
X∞
V = Vijk`
i≤j≤≤k≤`
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

lim max, lim max


n→∞ p≥n n→∞ p≥n
lim max , lim max
n→∞ p2≥n n→∞ p2≥n
lim sup , lim sup
n→∞ 2
p ≥nK n→∞ p2≥nK
lim sup max, lim sup max
n→∞ p≥n n→∞ p≥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
X= Xij [1]
1≤i≤j≤n
X
X= Xij [2]
1≤i≤j≤n

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
X= Xij (3)
1≤i≤j≤n
X
X= Xij (4)
1≤i≤j≤n

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
X= Xij (5)
1≤i≤j≤n
X
X= Xij
1≤i≤j≤n

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
X= Xij (6)
1≤i≤j≤n
X
X= Xij (7)
1≤i≤j≤n

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} a11 a12
\end{pmatrix}\\ a21 a22 + b12
 
&\begin{bmatrix} a11 a12
a_{11} &a_{12} \\ a21 a22 + b12
a_{21} &a_{22}+b_{12} 
a11 a12

\end{bmatrix}\\ a21 a22 + b12
&\begin{Bmatrix}  
a_{11} &a_{12} \\ a11 a12
a_{21} &a_{22}+b_{12} a21 a22 + b12

\end{Bmatrix}\\ a11 a12

&\begin{vmatrix} a21 a22 + b12
a_{11} &a_{12} \\
a11 a12

a_{21} &a_{22}+b_{12}
a21 a22 + b12

\end{vmatrix}\\
&\begin{Vmatrix}
a_{11} &a_{12} \\
a_{21} &a_{22}+b_{12}
\end{Vmatrix}
\end{align*}
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} a11 a12
\end{pmatrix*}\\ a 21 a 22 + b12
 
&\begin{bmatrix*}[r] a11 a12
a_{11} &a_{12} \\ a21 a22 + b12
a_{21} &a_{22}+b_{12} 
a11 a12

\end{bmatrix*}\\ a21 a22 + b12
&\begin{Bmatrix*}[r]  
a_{11} &a_{12} \\ a11 a12
a_{21} &a_{22}+b_{12} a21 a22 + b12

\end{Bmatrix*}\\ a11 a12

&\begin{vmatrix*}[r] a21 a22 + b12
a_{11} &a_{12} \\
a11 a12

a_{21} &a_{22}+b_{12}
a21 a22 + b12

\end{vmatrix*}\\
&\begin{Vmatrix*}[r]
a_{11} &a_{12} \\
a_{21} &a_{22}+b_{12}
\end{Vmatrix*}
\end{align*}
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} a11 a12
\end{pmatrix*}\\ a21 a22 + b12
 
&\begin{bmatrix*}[l] a11 a12
a_{11} &a_{12} \\ a21 a22 + b12
a_{21} &a_{22}+b_{12} 
a11 a12

\end{bmatrix*}\\ a21 a22 + b12
&\begin{Bmatrix*}[l]  
a_{11} &a_{12} \\ a11 a12
a_{21} &a_{22}+b_{12} a21 a22 + b12

\end{Bmatrix*}\\ a11 a12

&\begin{vmatrix*}[l] a21 a22 + b12
a_{11} &a_{12} \\
a11 a12


a_{21} &a_{22}+b_{12}
a21 a22 + b12

\end{vmatrix*}\\
&\begin{Vmatrix*}[l]
a_{11} &a_{12} \\
a_{21} &a_{22}+b_{12}
\end{Vmatrix*}
\end{align*}
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}
\]

(
E = mc2 Nothing to see here
a=
x − 3 dx Integral is text style
R

\[
a=\begin{dcases}
E = m c^2 & \text{Nothing to see here} \\
\int x-3\, dx & \text{Integral is text style}
\end{dcases}
\]

2
Nothing to see here

E = mc
a=
Z
 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*}
\]

2
Nothing to see here

E = mc
a=
Z
 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}
\]

5+ 14 5+ 4 5+ 14
4
12 C2 2 C2 12 C2 C5+
2
5+
2 C2

\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}
\]

A
ZX → A−4 4
Z−2 Y + 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
\]

over
A ⇐=== B
under

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}$ f oo bar


baz
baz

$\overbracket {foo\ bar}^{baz}$ f oo 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 and xxx yyy


zzz 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}
\]

xy + xy + xy + xy + xy xy + xy + xy + xy + xy
+ xy + xy + xy + xy + xy + xy + xy + xy
a= =
z 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
(12)
y=2
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
(13)
2∗ y=2

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)

You might also like