我的文档中有一个大块矩阵,这稍微违反了页边距。我想减少行中矩阵元素之间的水平间距。此外,我想对齐多个方程。我尝试使用数组和矩阵,但命令arraycolsep
没有按预期工作。
\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{array}
\begin{document}
The following very large matrix could fit the text width if the spacing could be reduced slightly.
\setcounter{MaxMatrixCols}{30}
\begin{align}
x =&
\begin{bmatrix}
1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0
\end{bmatrix}\\
=& 3
\end{align}
This could be done defining the arraycolsep:
\begin{align}
\setlength\arraycolsep{4pt}
x =
\begin{bmatrix}
1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0
\end{bmatrix}\\
\end{align}
Unfortunately, as soon as I try to align multiple lines the arracolsep command stops doing what I expect.
\begin{align}
\setlength\arraycolsep{4pt}
x =&
\begin{bmatrix}
1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0
\end{bmatrix}\\
=& 3
\end{align}
As is the same for the array env
\begin{align}
\setlength\arraycolsep{4pt}
x =&
\left[\begin{array}{cccccccccccccccccccccccc}
1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0
\end{array}\right]\\
=& 3
\end{align}
Can anyone help out? Thanks!
\end{document}
修改 1:我忘了说我在第二行使用了一个非常短的方程。但是,第二行也非常长,一行写不下两个方程。
答案1
我会定义一个varmatrix
环境,您可以在其中设置\arraycolsep
和字体大小。
这更加灵活,因为您可以决定\normalsize
减少分离,而且还可以使用更小的尺寸。
默认值为\small
和0.7\arraycolsep
。
\documentclass{article}
\usepackage{amsmath}
\newsavebox{\varmatrixbox}
\ExplSyntaxOn
\keys_define:nn {martin/varmatrix}
{
sep .dim_set:N = \l_martin_varmatrix_sep_dim,
delim .tl_set:N = \l_martin_varmatrix_delim_tl,
size .tl_set:N = \l_martin_varmatrix_size_tl,
sep .initial:n = 0.7\arraycolsep,
size .initial:n = \small,
}
\NewDocumentEnvironment{varmatrix}{O{}}
{
\keys_set:nn {martin/varmatrix} { #1 }
\begin{lrbox}{\varmatrixbox}
% font size
\l_martin_varmatrix_size_tl
% separation
\setlength{\arraycolsep}{\l_martin_varmatrix_sep_dim}
$\begin{\l_martin_varmatrix_delim_tl matrix}
}
{
\end{\l_martin_varmatrix_delim_tl matrix}$
\end{lrbox}
\vcenter{\box\varmatrixbox}
}
\ExplSyntaxOff
\setcounter{MaxMatrixCols}{50}
\begin{document}
\begin{align}
x &=
\setlength{\arraycolsep}{3pt}
\begin{bmatrix}
1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0
\end{bmatrix}\\
&=
\begin{varmatrix}[delim=b,size=\normalsize,sep=3pt]
1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0
\end{varmatrix}\\
&=
\begin{varmatrix}[delim=b,size=\small]
1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0
\end{varmatrix}\\
&=
\begin{varmatrix}[delim=p,size=\footnotesize,sep=0.5\arraycolsep]
1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0
\end{varmatrix}
\end{align}
\end{document}
一些解释。
该
\keys_define:nn
部分为选项定义了键值语法varmatrix
。- 键
sep
设置\arraycolsep
- 键
delim
设置围栏,使用标准字母amsmath
、b
、p
、v
,V
甚至可以不使用任何字母来表示没有围栏 - 键
size
应该接收字体大小声明
- 键
环境
varmatrix
将矩阵排版在一个框中,因此我们需要在设置大小和\arraycolsep
盒子完成后,
\vcenter
像通常的矩阵一样在内部用于垂直居中
答案2
我建议结合使用\arraycolsep
中等大小的矩阵(显示样式大小的 80%),如nccmath
包中所定义:
\documentclass[12pt]{article}
\usepackage{amsmath, nccmath}
\usepackage{etoolbox}
\AtBeginEnvironment{bmatrix}{\setlength{\arraycolsep}{4pt}}
\setcounter{MaxMatrixCols}{30}
\begin{document}
The following very large matrix fits the text width using medsize matrices
\begin{align}
x &=
\medmath{\begin{bmatrix}
1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0
\end{bmatrix}}\\
&= 3
\end{align}
\end{document}
答案3
@{}
您可以使用语法在列之间插入 4pt 空格matrix
。此外,使用&=
not=&
可获得适当的间距。
\begin{align}
x &=
\left[\begin{array}{*{24}{c@{\hspace{4pt}}}}
1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0
\end{array}\right]\\
&= 3
\end{align}