# two-line equation with matrix inside

I want to get a two-line equation, in which one line is consisted of both variables and matrix. I tried {align} and {matrix} but not working.

\label{eqk27} \begin{align} \bm{\mathrm{w}}^\mathrm{T} \bm{X}^\mathrm{T} & = [w_1, \hspace{0.1cm} w_2, \hspace{0.1cm} cdots, \hspace{0.1cm} w_n] \left[ \begin{matrix} x_{1,1} & x_{2,1} & \cdots & x_{n,1} \\ x_{1,2} & x_{2,2} & \cdots & x_{n,2} \\ \vdots & \vdots & \vdots & \vdots \\ x_{1,n} & x_{2,n} & \cdots & x_{n,n} \end{matrix} \right] \\ & = [\sum_{i=1}^{} w_i x_{1,i}, \hspace{0.1cm} \sum_{i=1}^{} w_i x_{2,i}, \hspace{0.1cm} \cdots, \hspace{0.1cm} \sum_{i=1}^{} w_i x_{2,i} ] \end{align}


• Welcome to TeX.SE. The equation in the second screenshot doesn't fit inside the textblock. Is there a reason for not placing the final row on a separate line, while aligning the two = symbols?
– Mico
Sep 18, 2021 at 6:25
• Placing the final one on a new line with alignment is my purpose but I failed to get it. Sep 18, 2021 at 6:30
• Hello, Przemysław Scherwentke, egreg and Mico. Thanks to all you guys giving me answers in different ways. They are all working. The problem is from some previous .tex files of my book chapters. I finally choose Przemysław's answer simply because I can choose only one. Merci bien. Sep 22, 2021 at 1:59

You cannot nest align inside equation. Below is a version with aligned. The code need improvements but it is unclear what do you want to obtain with \bm, \hspace, etc.

\documentclass{article}

\usepackage{amsmath,amssymb}

\def\bm{}

\begin{document}
\label{eqk27} \begin{aligned} \bm{\mathrm{w}}^\mathrm{T} \bm{X}^\mathrm{T} & = [w_1, \hspace{0.1cm} w_2, \hspace{0.1cm} \cdots, \hspace{0.1cm} w_n] \left[ \begin{matrix} x_{1,1} & x_{2,1} & \cdots & x_{n,1} \\ x_{1,2} & x_{2,2} & \cdots & x_{n,2} \\ \vdots & \vdots & \vdots & \vdots \\ x_{1,n} & x_{2,n} & \cdots & x_{n,n} \end{matrix} \right] \\ % & = [\sum_{i=1}^{} w_i x_{1,i}, \hspace{0.1cm} \sum_{i=1}^{} w_i x_{2,i}, \hspace{0.1cm} \cdots, \hspace{0.1cm} \sum_{i=1}^{} w_i x_{2,i} ] \end{aligned}

\end{document}

• Thanks for help. @Przemyslaw Scherwentke It is compiled but cannot get real two-line. The second part in the {align} simply moves down one line along the right side of the matrix (first part of the {align}). Sep 18, 2021 at 6:03
• @arifle So it should like as in your (fresh) picture? Sep 18, 2021 at 6:14
• Yes, it is :). @Przemyslaw Scherwentke. Sep 18, 2021 at 6:16

I believe you want split (surely not align that cannot be nested in equation).

You also want

1. a macro for denoting the transpose,
2. a macro for denoting (column) vectors, and
3. a macro for denoting matrices

so to avoid heavy markup, with the bonus that you can change the whole notation by just modifying the definitions.

\documentclass{article}
\usepackage{amsmath}
\usepackage{bm}

\newcommand{\TP}{{\mathrm{T}}} % transpose
\newcommand{\vect}[1]{\mathbf{#1}}
\newcommand{\matr}[1]{\bm{#1}} % matrix

\begin{document}

$$\label{eqk27} \begin{split} \vect{w}^\TP \matr{X}^\TP & = \begin{bmatrix} w_1 & w_2 & \dots & w_n \end{bmatrix} \begin{bmatrix} x_{1,1} & x_{2,1} & \dots & x_{n,1} \\ x_{1,2} & x_{2,2} & \dots & x_{n,2} \\ \vdots & \vdots & \vdots & \vdots \\ x_{1,n} & x_{2,n} & \dots & x_{n,n} \end{bmatrix} \\[1ex] & = \begin{bmatrix} \sum\limits_{i=1}^{n} w_i x_{1,i} & \sum\limits_{i=1}^{n} w_i x_{2,i} & \dots & \sum\limits_{i=1}^{n} w_i x_{2,i} \end{bmatrix} \end{split}$$

\end{document}


• Thanks, @egreg. This works well. Sep 22, 2021 at 2:00

Instead of placing a matrix environment inside a \left\[ ... \right] construct, just use a bmatrix environment; the b in bmatrix stands for "square bracket". Second, since the row vectors are, mathematically speaking, matrices as well and since they're encased in square brackets, just use bmatrix environments for them too. (Aside: An advantage of using bmatrix environments for row vectors is that you needn't concern yourself with the sizing of the square brackets.) Third, \bm{\mathrm{w}} may be stated more simply as \mathbf{w}.

Last but not least, you should use either an aligned or a split environment to (a) split the full expression across two lines and (b) align the equation fragments on the = symbols. The split environment centers the equation number vertically, while the aligned environment lets you choose to place the equation number on the top or bottom line. The output produced by the aligned and split environments is shown in the following screenshot.

\documentclass{article}
\usepackage{amsmath}   % for 'aligned', 'split', and 'bmatrix' environments
\begin{document}
\noindent
with \texttt{aligned} environment
\label{eqk27aligned} \begin{aligned}[b] \mathbf{w}^\mathrm{T} \mathbf{X}^\mathrm{T} % &= \begin{bmatrix} w_1 & w_2 & \dots & w_n \end{bmatrix} \begin{bmatrix} x_{11} & x_{21} & \dots & x_{n1} \\ x_{12} & x_{22} & \dots & x_{n2} \\ \vdots & \vdots & \ddots& \vdots \\ x_{1n} & x_{2n} & \dots & x_{nn} \end{bmatrix} \\[\jot] % &= \begin{bmatrix} \, \sum_{i=1}^{n} w_i x_{1i} & \sum_{i=1}^{n} w_i x_{2i} & \dots & \sum_{i=1}^{n} w_i x_{ni} \end{bmatrix} \end{aligned}

\bigskip\noindent
with \texttt{split} environment
$$\label{eqk27split} \begin{split} \mathbf{w}^\mathrm{T} \mathbf{X}^\mathrm{T} % &= \begin{bmatrix} w_1 & w_2 & \dots & w_n \end{bmatrix} \begin{bmatrix} x_{11} & x_{21} & \dots & x_{n1} \\ x_{12} & x_{22} & \dots & x_{n2} \\ \vdots & \vdots & \ddots& \vdots \\ x_{1n} & x_{2n} & \dots & x_{nn} \end{bmatrix} \\[\jot] % &= \begin{bmatrix} \, \sum_{i=1}^{n} w_i x_{1i} & \sum_{i=1}^{n} w_i x_{2i} & \dots & \sum_{i=1}^{n} w_i x_{ni} \end{bmatrix} \end{split}$$

\end{document}

• Compilation issues. Sep 18, 2021 at 7:15
• @arifle - Very sorry, but my mind-reading skills are utterly worthless. I therefore have no idea what you mean by the two-word fragment "Compilation issues." Please elaborate.
– Mico
Sep 18, 2021 at 7:18
• Sorry, it is IT word. Latex engine gave compilation errors to this but I did try using {bmatrix} for the solution of @Przemysław Scherwentke and it was still the same. That means {bmatrix} works at least. Sep 18, 2021 at 7:27
• @arifle - You wrote, "Latex engine gave compilation errors". For sure, the code I showed in my answer does not generate "compilation issues". It must be the case that your code does something that's materially different from mine. Unless you report what these differences may be and/or start providing actual details about the "compilation errors" -- remember, my mind-reading skills are worthless -- you can't expect me to provide meaningful advice at this point.
– Mico
Sep 18, 2021 at 7:34
• Yes, I am checking them piece by piece. Sep 18, 2021 at 7:40