4

I have 4 \begin{bmatrix} matrices. Because the matrix values are different the overall 4x4 matrices have different column widths. Is there a way to align them so that all columns have the same width so that the matrices look symmetric?

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{mathtools}

\begin{document}

\begin{align} \Phi_{1} &= \begin{bmatrix}[r] 0 & -\pi & -\pi & 0 \ \pi & 0 & 0 & \pi \ \pi & 0 & 0 & \pi \ 0 & -\pi & -\pi & 0 \ \end{bmatrix} & \Phi_{2} &= \begin{bmatrix}[r] 0 & 0 & -\pi & -\pi \ 0 & 0 & -\pi & -\pi \ \pi & \pi & 0 & 0 \ \pi & \pi & 0 & 0 \ \end{bmatrix} \ \Phi_{3} &= \begin{bmatrix}[r] 0 & -\pi & 0 & -\pi \ \pi & 0 & \pi & \pi \ 0 & -\pi & 0 & -\pi \ \pi & -\pi & \pi & 0 \ \end{bmatrix} & \Phi_{4} &= \begin{bmatrix}[r] 0 & -\frac{\pi}{2} & -\pi & -\frac{3\pi}{2} \ \frac{\pi}{2} & 0 & -\frac{\pi}{2} & -\pi \ \pi & \frac{\pi}{2} & 0 & -\frac{\pi}{2} \ \frac{3\pi}{2} & \pi & \frac{\pi}{2} & 0 \ \end{bmatrix*}\ \

\end{align*}

\end{document}

which results in this

FR_MPI
  • 43

2 Answers2

3

You need different column widths for each column. With an up-to-date TeX distribution you can use the w column type provided by array. But I'm afraid you have to decide the widest entry of each column.

I provide both the right and the center aligned versions; I much prefer the latter.

Avoid trailing \\ as a general rule (unless your alignment has a bottom rule). They do no harm inside array or matrix, they do in align.

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{array,calc}

\begin{document}

\begin{align} \Phi_{1} &= \begin{bmatrix} \begin{array}{ w{r}{\widthof{$\pi$}} w{r}{\widthof{$-\pi$}} w{r}{\widthof{$-\pi$}} w{r}{\widthof{$-\pi$}} } 0 & -\pi & -\pi & 0 \ \pi & 0 & 0 & \pi \ \pi & 0 & 0 & \pi \ 0 & -\pi & -\pi & 0 \end{array} \end{bmatrix} & \Phi_{2} &= \begin{bmatrix} \begin{array}{ w{r}{\widthof{$\frac{3\pi}{2}$}} w{r}{\widthof{$-\pi$}} w{r}{\widthof{$-\pi$}} w{r}{\widthof{$-\frac{3\pi}{2}$}} } 0 & 0 & -\pi & -\pi \ 0 & 0 & -\pi & -\pi \ \pi & \pi & 0 & 0 \ \pi & \pi & 0 & 0 \end{array} \end{bmatrix} \ \Phi_{3} &= \begin{bmatrix} \begin{array}{ w{r}{\widthof{$\pi$}} w{r}{\widthof{$-\pi$}} w{r}{\widthof{$-\pi$}} w{r}{\widthof{$-\pi$}} } 0 & -\pi & 0 & -\pi \ \pi & 0 & \pi & \pi \ 0 & -\pi & 0 & -\pi \ \pi & -\pi & \pi & 0 \end{array} \end{bmatrix} & \Phi_{4} &= \begin{bmatrix} \begin{array}{ w{r}{\widthof{$\frac{3\pi}{2}$}} w{r}{\widthof{$-\pi$}} w{r}{\widthof{$-\pi$}} w{r}{\widthof{$-\frac{3\pi}{2}$}} } 0 & -\frac{\pi}{2} & -\pi & -\frac{3\pi}{2} \ \frac{\pi}{2} & 0 & -\frac{\pi}{2} & -\pi \ \pi & \frac{\pi}{2} & 0 & -\frac{\pi}{2} \ \frac{3\pi}{2} & \pi & \frac{\pi}{2} & 0 \end{array} \end{bmatrix} \end{align}

\begin{align} \Phi_{1} &= \begin{bmatrix} \begin{array}{ w{c}{\widthof{$\pi$}} w{c}{\widthof{$-\pi$}} w{c}{\widthof{$-\pi$}} w{c}{\widthof{$-\pi$}} } 0 & -\pi & -\pi & 0 \ \pi & 0 & 0 & \pi \ \pi & 0 & 0 & \pi \ 0 & -\pi & -\pi & 0 \end{array} \end{bmatrix} & \Phi_{2} &= \begin{bmatrix} \begin{array}{ w{c}{\widthof{$\frac{3\pi}{2}$}} w{c}{\widthof{$-\pi$}} w{c}{\widthof{$-\pi$}} w{c}{\widthof{$-\frac{3\pi}{2}$}} } 0 & 0 & -\pi & -\pi \ 0 & 0 & -\pi & -\pi \ \pi & \pi & 0 & 0 \ \pi & \pi & 0 & 0 \end{array} \end{bmatrix} \ \Phi_{3} &= \begin{bmatrix} \begin{array}{ w{c}{\widthof{$\pi$}} w{c}{\widthof{$-\pi$}} w{c}{\widthof{$-\pi$}} w{c}{\widthof{$-\pi$}} } 0 & -\pi & 0 & -\pi \ \pi & 0 & \pi & \pi \ 0 & -\pi & 0 & -\pi \ \pi & -\pi & \pi & 0 \end{array} \end{bmatrix} & \Phi_{4} &= \begin{bmatrix} \begin{array}{ w{c}{\widthof{$\frac{3\pi}{2}$}} w{c}{\widthof{$-\pi$}} w{c}{\widthof{$-\pi$}} w{c}{\widthof{$-\frac{3\pi}{2}$}} } 0 & -\frac{\pi}{2} & -\pi & -\frac{3\pi}{2} \ \frac{\pi}{2} & 0 & -\frac{\pi}{2} & -\pi \ \pi & \frac{\pi}{2} & 0 & -\frac{\pi}{2} \ \frac{3\pi}{2} & \pi & \frac{\pi}{2} & 0 \end{array} \end{bmatrix} \end{align}

\end{document}

enter image description here

egreg
  • 1,121,712
  • for me this only compiles when I out $\pi$ into the matrix elements. Otherwise I get a "missing $" error – FR_MPI Nov 20 '20 at 13:18
  • I was also unable to compile it. My current version is

    This is LuaHBTeX, Version 1.12.0 (MiKTeX 20.7) (format=lualatex 2020.11.13) 20 NOV 2020 12:54 system commands enabled.

    LaTeX2e <2020-02-02> patch level 5

    Not new enough?

    – Simon Dispa Nov 20 '20 at 16:37
  • you need the latest array package (when w was first added it forced text mode) – David Carlisle Nov 20 '20 at 18:55
  • I find different alignment of \pi and -\pi distracting, Except for the fractions, I think the right-alignment both most pleasing and most logical. (Zero and pi are close enough in width that no adjustment is needed.) I'd tinker with the fractions to center them on the horizontal midpoint of the zeros, and \llap a minus if one is there. – barbara beeton Oct 02 '22 at 20:44
1

The package nicematrix provides tools to ensure that all the matrices constructed by the environments of nicematrix ({pNiceMatrix}, {bNiceMatrix}, etc.) in a given scope have all their columns of the same width.

\documentclass[10pt,a4paper]{article}
\usepackage{nicematrix}
\usepackage{mathtools}

\begin{document}

\begin{NiceMatrixBlock}[auto-columns-width] \begin{align} \Phi_{1} &= \begin{bmatrix}[r] 0 & -\pi & -\pi & 0 \ \pi & 0 & 0 & \pi \ \pi & 0 & 0 & \pi \ 0 & -\pi & -\pi & 0 \ \end{bmatrix} & \Phi_{2} &= \begin{bNiceMatrix}[r] 0 & 0 & -\pi & -\pi \ 0 & 0 & -\pi & -\pi \ \pi & \pi & 0 & 0 \ \pi & \pi & 0 & 0 \ \end{bNiceMatrix} \ \Phi_{3} &= \begin{bmatrix}[r] 0 & -\pi & 0 & -\pi \ \pi & 0 & \pi & \pi \ 0 & -\pi & 0 & -\pi \ \pi & -\pi & \pi & 0 \ \end{bmatrix} & \Phi_{4} &= \begin{bNiceMatrix}[r] 0 & -\frac{\pi}{2} & -\pi & -\frac{3\pi}{2} \ \frac{\pi}{2} & 0 & -\frac{\pi}{2} & -\pi \ \pi & \frac{\pi}{2} & 0 & -\frac{\pi}{2} \ \frac{3\pi}{2} & \pi & \frac{\pi}{2} & 0 \ \end{bNiceMatrix}\ \end{align} \end{NiceMatrixBlock}

\end{document}

You need several compilations (because nicematrix writes informations on the aux file).

Output of the above code

F. Pantigny
  • 40,250