1. 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
2. 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_{1le ile jle n} X_{ij}
X &= sum_{mathclap{1le ile jle 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_{kgeq0} h(k)
a+b &= sum_{mathclap{i+j=10}} f(i) g(j)
&= sum_{mathclap{kgeq0}} 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_{ile j le le k le ell}^{infty} V_{ijkell}
V &= smashoperator{sum_{ile j le le k le ell}^{infty}} V_{ijkell}
V &= smashoperator[r]{sum_{ile j le le k le ell}^{infty}} V_{ijkell}
V &= smashoperator[l]{sum_{ile j le le k le ell}^{infty}} V_{ijkell}
end{align*}
V =
∞
i≤j≤≤k≤
Vijk
V =
∞
i≤j≤≤k≤
Vijk
V =
∞
i≤j≤≤k≤
Vijk
V =
∞
i≤j≤≤k≤
Vijk
3. 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_{ntoinfty} max_{pge n},
& adjustlimitslim_{ntoinfty} max_{pge n}
& lim_{ntoinfty} max_{p^2ge n},
& adjustlimitslim_{ntoinfty} max_{p^2ge n}
& lim_{ntoinfty} sup_{p^2ge nK},
& adjustlimitslim_{ntoinfty} sup_{p^2ge nK}
& limsup_{ntoinfty} max_{pge n},
& adjustlimitslimsup_{ntoinfty} max_{pge 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
n→∞
max
p≥n
, lim
n→∞
max
p≥n
lim
n→∞
max
p2≥n
, lim
n→∞
max
p2≥n
lim
n→∞
sup
p2≥nK
, lim
n→∞
sup
p2≥nK
lim sup
n→∞
max
p≥n
, lim sup
n→∞
max
p≥n
6. Định dạng lại kiểu bao quanh đánh nhãn
newtagform{brackets2}[textbf]{[}{]}
usetagform{brackets2}
begin{align}
X &= sum_{1le ile jle n} X_{ij}
X &= sum_{mathclap{1le ile jle 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}
4. http://nhdien.wordpress.com - Nguyễn Hữu Điển 4
begin{align}
X &= sum_{1le ile jle n} X_{ij}
X &= sum_{mathclap{1le ile jle 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_{1le ile jle n} X_{ij} label{eq:eq01}
X &= sum_{mathclap{1le ile jle 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_{1le ile jle n} X_{ij} label{eq:eq03}
X &= sum_{mathclap{1le ile jle 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.
6. 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
[
7. 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}
]
4
12C5+
2
14
2C5+
2
4
12C5+
2
14
C5+
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
}
8. 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
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
]
A
over
⇐===
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}
]
9. 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}
newcommandstepline{stepcounter{steplinecnt}thesteplinecnt}
newgathered{stargathered}
{llap{stepline}$*$quadhfil}% hfil for centering
{hfil}% hfil for centering
{setcounter{steplinecnt}{0}}% reset counter
Áp dụng
10. 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)