I have a large block matrix in my document, which violates the page margin slightly. I would like to reduce the horizontal spacing between the matrix elements of a row. Additionally, I want to align multiple equations. I tried with array and matrix but the arraycolsep
command does not work as I expect.
\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}
Edit 1: I forgot to mention that I used a very short equation in the second line. However, the second line is also very long and both will not fit a single line.
Best Answer
I'd define a
varmatrix
environment where you can set both the\arraycolsep
and the font size.This is much more flexible, because you can decide for
\normalsize
and reduce the separation, but also to use smaller size.The default is
\small
and0.7\arraycolsep
.Some explanations.
The
\keys_define:nn
part defines a key-value syntax for the options tovarmatrix
.sep
key sets the\arraycolsep
delim
key sets the fences, use the standard letters ofamsmath
,b
,p
,v
,V
or even nothing for no fencesize
key should receve a font size declarationthe
varmatrix
environment typesets the matrix in a box, so we need to restart math mode after setting the size and the\arraycolsep
after the box is finished, it is used inside
\vcenter
for vertical centering like usual matrices