3

I'm having problems trying to align single and multiple line bmatrices:

\documentclass[12pt]{article}

\usepackage{amsmath}

\begin{document}

\begin{align*}
    x = &\begin{bmatrix} 1&2&3 \end{bmatrix}\\
    y = &\begin{bmatrix} 1&2&3\\3&2&1 \end{bmatrix}\\
    z = &\begin{bmatrix} 1&2&3\\3&2&1\\4&4&4 \end{bmatrix}
\end{align*}

\end{document}

I guess the solution would be to force the same matrix tracing used in the one with the biggest amount of lines, but how to do it?

The above example led to a false interpretation of the problem. What I want is to have the alignment on the beginning of each bracket:

\documentclass[12pt]{article}

\usepackage{amsmath}

\begin{document}

\begin{align*}
    x = &\begin{bmatrix} 1&2&3&6&5 \end{bmatrix}\\
    y = &\begin{bmatrix} 1&2&3\\3&2&1 \end{bmatrix}\\
    z = &\begin{bmatrix} 1&2&3&4\\3&2&4&1\\5&4&4&4 \end{bmatrix}
\end{align*}

\end{document}

and the answers no longer work:

enter image description here

  • You might want to clarify that it is the vertical alignment of the columns you are talking about. Not quite obvious without an image. It is due to the width of the fences being different. It could probably be hacked but it may be very hard to do since it is dealing with internal \left ... \right constructions – daleif Jun 25 '13 at 14:58
  • @daleif I didn't even consider the horizontal alignment. – egreg Jun 25 '13 at 15:28
4
\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{calc}
\newlength\lone
\newcommand\fitto[2]{%
  \setlength\lone{\widthof{$#1$}}%
  \makebox[\lone][c]{$#2$}%
}
\begin{document}
\def\x{\begin{bmatrix} 1&2&3\\3&2&1\\4&4&4 \end{bmatrix}}
\begin{align*}
    x = &\fitto{\x}{\begin{bmatrix} 1&2&3 \end{bmatrix}}\\
    y = &\fitto{\x}{\begin{bmatrix} 1&2&3\\3&2&1 \end{bmatrix}}\\
    z = &\fitto{\x}{\x}
\end{align*}
\end{document}

enter image description here

| improve this answer | |
  • Something like that, but I don't think it is worth doing. It takes to much work in general. – daleif Jun 25 '13 at 15:11
  • @daleif I understand your point, but in general with such a macro approach, all the matrices to be stacked have to be known in advance, so that the widest can be determined. To do it "automatically" would require a single call in which all the matrices were simultaneously passed to the macro for assessment, along with the auxiliary material. I think that would quickly grow more complicated than this approach, in which the user picks the widest array manually, and tells the macro to fit to it. – Steven B. Segletes Jun 25 '13 at 15:39
2

Sadly enough, there is no easy override to the alignment of the align-like environments. They always follow (correctly) a right-left (R&L) alignment. You can change that by using a different preamble (align@preamble):

enter image description here

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\makeatletter
\newenvironment{rcalign*}{%
  \def\align@preamble{%
    &\hfil
      \strut@
      \setboxz@h{\@lign$\m@th\displaystyle{####}$}%
      \ifmeasuring@\savefieldlength@\fi
      \set@field
      \tabskip\z@skip
    &\hfil\setboxz@h{\@lign$\m@th\displaystyle{{}####}$}%
      \ifmeasuring@\savefieldlength@\fi
      \set@field
      \hfil
      \tabskip\alignsep@
  }%
  \start@align\@ne\st@rredtrue\m@ne%
  }
  {\endalign}
\makeatother
\begin{document}
\begin{align*}
  x = &\begin{bmatrix} 1&2&3 \end{bmatrix} \\
  y = &\begin{bmatrix} 1&2&3\\3&2&1 \end{bmatrix} \\
  z = &\begin{bmatrix} 1&2&3\\3&2&1\\4&4&4 \end{bmatrix}
\end{align*}

\begin{rcalign*}
  x = &\begin{bmatrix} 1&2&3 \end{bmatrix} \\
  y = &\begin{bmatrix} 1&2&3\\3&2&1 \end{bmatrix} \\
  z = &\begin{bmatrix} 1&2&3\\3&2&1\\4&4&4 \end{bmatrix}
\end{rcalign*}
\end{document}

The above example defines the environment rcalign* that has right-centre (R&C) alignment. This only works if you use the operator with the R-column (left-hand side) and most certainly don't include anything other than these matrices in the C-column (right-hand side).

| improve this answer | |
1

Another way:

(Updated) (variant added, following egreg's suggestion) (done in two steps...)

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{array}
\begin{document}
\begin{equation*}\tabcolsep0pt
  \begin{tabular}{>{$\displaystyle}c<{$}>{$\displaystyle}c<{$}}
     x = {} &\begin{bmatrix} 1&2&3 \end{bmatrix}\\
\noalign{\vskip\jot}
     y = {} &\begin{bmatrix} 1&2&3\\3&2&1 \end{bmatrix}\\
\noalign{\vskip\jot}
     z = {} &\begin{bmatrix} 1&2&3\\3&2&1\\4&4&4 \end{bmatrix}
  \end{tabular}
\end{equation*}
% former proposal 
% \begin{equation*}
%    \begin{matrix}
%      x & = &\begin{bmatrix} 1&2&3 \end{bmatrix}\\
% \noalign{\vskip\jot}
%      y & = &\begin{bmatrix} 1&2&3\\3&2&1 \end{bmatrix}\\
% \noalign{\vskip\jot}
%      z & = &\begin{bmatrix} 1&2&3\\3&2&1\\4&4&4 \end{bmatrix}
%    \end{matrix}
% \end{equation*}
\end{document}

(I am not sure whether the \displaystyle are useful; not in the case at hand but in other situations perhaps) aligned matrices

The commented out code gave:

aligned matrices

And here it is now with egreg's tabular column specification (I had forgotten that @{} is a better way to kill intercolumn). I have also taken out the {}'s from the x={}, y={}, etc... and added the last matrix typeset normally to compare the spacing.

\begin{equation*}
  \begin{tabular}{@{}>{$\displaystyle}r<{$}@{}>{$\displaystyle{}}c<{$}@{}}
     x = &\begin{bmatrix} 1&2&3 \end{bmatrix}\\
\noalign{\vskip\jot}
     y = &\begin{bmatrix} 1&2&3\\3&2&1 \end{bmatrix}\\
\noalign{\vskip\jot}
     z = &\begin{bmatrix} 1&2&3\\3&2&1\\4&4&4 \end{bmatrix}
  \end{tabular}
\end{equation*}

\begin{equation*}
     z = \begin{bmatrix} 1&2&3\\3&2&1\\4&4&4 \end{bmatrix}
\end{equation*}

aligned matrices

I had not yet moved the = signs to the location suggested by egreg. Here it is:

\begin{equation*}
  \begin{tabular}{@{}>{$\displaystyle}r<{$}@{}>{$\displaystyle{}}c<{$}@{}}
     x &=\begin{bmatrix} 1&2&3 \end{bmatrix}\\
\noalign{\vskip\jot}
     y &=\begin{bmatrix} 1&2&3\\3&2&1 \end{bmatrix}\\
\noalign{\vskip\jot}
     z &=\begin{bmatrix} 1&2&3\\3&2&1\\4&4&4 \end{bmatrix}
  \end{tabular}
\end{equation*}

aligned matrices

| improve this answer | |
  • Probably @{}>{$\displaystyle}r<{$}@{}>{$\displaystyle{}}c<{$}@{}} would be a better specification, with the input x &= \begin{bmatrix}... that conforms to the usual conventions. – egreg Jun 25 '13 at 17:53
  • @egreg --- thanks! incorporated to the answer. No wait, I understood half of your suggestion, I need to re-do it. Ok, done now. – user4686 Jun 25 '13 at 19:07

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.