4

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}

enter image description here

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.

1
  • 1
    x =& and =&3 should be x&= and &=3 see the bad space you have in your image for =3 May 7 at 21:28

3 Answers 3

4

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

enter image description here

Some explanations.

  • The \keys_define:nn part defines a key-value syntax for the options to varmatrix.

    1. the sep key sets the \arraycolsep
    2. the delim key sets the fences, use the standard letters of amsmath, b, p, v, V or even nothing for no fence
    3. the size key should receve a font size declaration
  • the 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

2
  • This looks like a fantastic solution. However, there is much code in the beginning, which I do not fully understand, unfortunately. Would you mind to explain it to me? May 7 at 20:40
  • 1
    @MartinHerrmann I added some comments
    – egreg
    May 7 at 20:59
4

I propose to combine playing with \arraycolsep and using a medium-sized matrix (80% of displaystyle size), as defined in the nccmath package:

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

enter image description here

3

You can insert a 4pt space between columns using the @{} syntax for matrix. Also, use &= not =& for proper spacing.

enter image description here

\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}
2
  • This is a very nice solution. However, can I additionally reduce the spacing between the brackets and the first/last columns? May 7 at 20:38
  • 1
    Yes. Just add a negative space. For example: \left[\!\begin{array} will add a "negative thin space".
    – Sandy G
    May 7 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.