# multiline equation inside a matrix that is a part of multiline equation

I have an equation split to multiple lines. But the last line of the multiline equation contains 2x1 vector with long equations that go out of margin. How do I further split the equations in the vector so that they do go over margin and look visually pleasing?

$$\label{eq:7} \begin{split} \mathbf{y}(1)&=tanh(\mathbf{W}^{ro}\cdot\mathbf{h}(1)+\mathbf{W}^{io}\cdot\mathbf{x}(1)) \\ &=\begin{bmatrix} tanh\Big(w^{ro}_{1,1}\cdot\alpha\cdot tanh\big(w^{ir}_{1,1}\cdot x_{1}(1)+w^{ir}_{2,1}\cdot x_{2}(1)\big) + w^{ro}_{2,1}\cdot \alpha \cdot tanh\big(w^{ir}_{1,2}\cdot x_{1}(1)+w^{ir}_{2,2} \cdot x_{2}(1)\big) + w^{io}_{1,1} \cdot x_{1}(1)+w^{io}_{2,1} \cdot x_{2}(1)\Big) \\ tanh\Big(w^{ro}_{1,2}\cdot \alpha \cdot tanh\big(w^{ir}_{1,1}\cdot x_{1}(1)+w^{ir}_{2,1}\cdot x_{2}(1)\big) + w^{ro}_{2,2}\cdot \alpha \cdot tanh\big(w^{ir}_{1,2}\cdot x_{1}(1)+w^{ir}_{2,2} \cdot x_{2}(1)\big) + w^{io}_{1,2}\cdot x_{1}(1)+w^{io}_{2,2}\cdot x_{2}(1)\Big) \end{bmatrix}. \end{split}$$


Current output looks like below where good portion of equations go over margin:

Any help will be greatly appreciated

• Any news? Is any of received answers the most acceptable to you? If it, please click on check mark at top left side of selected answer. As I have seen, so far you not accept any answer on your question ;-(. – Zarko May 19 '19 at 23:07

You can break lines within a matrix by useing of multlined from the mathtools package:

\documentclass{article}
\usepackage{geometry}
\usepackage{mathtools}

\begin{document}
$$\label{eq:7} \begin{split} \mathbf{y}(1)&=\tanh(\mathbf{W}^{ro}\cdot\mathbf{h}(1)+\mathbf{W}^{io}\cdot\mathbf{x}(1)) \\ & = \begin{bmatrix} \begin{multlined} \tanh\Bigl(w^{ro}_{1,1}{\cdot}\alpha{\cdot} \tanh\bigl(w^{ir}_{1,1}{\cdot} x_{1}(1) + w^{ir}_{2,1}{\cdot} x_{2}(1)\bigr) \\[-2ex] + w^{ro}_{2,1}{\cdot} \alpha {\cdot} \tanh\bigl(w^{ir}_{1,2}{\cdot} x_{1}(1) + w^{ir}_{2,2} {\cdot} x_{2}(1)\bigr) + w^{io}_{1,1} {\cdot} x_{1}(1)+w^{io}_{2,1} {\cdot} x_{2}(1)\Bigr) \end{multlined} \\[4ex] % \begin{multlined} \tanh\Bigl(w^{ro}_{1,2}{\cdot} \alpha {\cdot} \tanh\bigl(w^{ir}_{1,1}{\cdot} x_{1}(1) + w^{ir}_{2,1}{\cdot} x_{2}(1)\bigr) \\[-2ex] + w^{ro}_{2,2}{\cdot} \alpha {\cdot} \tanh\bigl(w^{ir}_{1,2}{\cdot} x_{1}(1)+w^{ir}_{2,2} {\cdot} x_{2}(1)\bigr) + w^{io}_{1,2}{\cdot} x_{1}(1)+w^{io}_{2,2}{\cdot} x_{2}(1)\Bigr) \end{multlined} \end{bmatrix}. \end{split}$$
\end{document}


note: I try to fix of use \Big( and \big(. More correct is \Bigl( and \bigl( and \Bigr) and \bigr). most of the math operators are defined, so instead ˙tanh you should use \tanh which write it correct mathrm font. I also reduce width of \cdots enclosing them in curly braces. Do you really need them?

• Thank you for correcting usage of other natations. This is really helpful! I initially didn't have \cdots but I feel like including them makes it more readable. Does {\cdot} have smaller width compared to just plain \cdot ? – user32147 May 16 '19 at 1:10

Here there is my proposal:

\documentclass[a4paper,12pt]{article}
\usepackage{mathtools,amssymb}
\usepackage[left=1in,right=.3in]{geometry}

\begin{document}
\label{eq:7} \begin{aligned} \mathbf{y}(1)&=\tanh(\mathbf{W}^{\mathrm{ro}}\cdot\mathbf{h}(1)+\mathbf{W}^{\mathrm{io}}\cdot\mathbf{x}(1)) \\ &=\begin{bmatrix} \tanh\Big((w^{\mathrm{\mathrm{ro}}}_{1,1}\cdot\alpha\cdot \tanh\mu) + w^{\mathrm{ro}}_{2,1}\cdot \alpha \cdot \tanh \lambda + w^{\mathrm{io}}_{1,1} \cdot x_{1}(1)+w^{\mathrm{io}}_{2,1} \cdot x_{2}(1)\Big) \\ \tanh \Big((w^{\mathrm{ro}}_{1,2}\cdot \alpha \cdot \tanh\mu) + w^{\mathrm{ro}}_{2,2}\cdot \alpha \cdot \tanh \lambda + w^{\mathrm{io}}_{1,2}\cdot x_{1}(1)+w^{\mathrm{io}}_{2,2}\cdot x_{2}(1)\Big) \end{bmatrix} \end{aligned}

where $\mu=(w^{\mathrm{ir}}_{1,1}\cdot x_{1}(1)+w^{\mathrm{ir}}_{2,1}\cdot x_{2}(1))$ and $\lambda=(w^{\mathrm{ir}}_{1,2}\cdot x_{1}(1)+w^{\mathrm{ir}}_{2,2} \cdot x_{2}(1))$
\end{document}

• i see, that now i need new glasses :-). fonts are so tiny ... anyway +1 – Zarko May 15 '19 at 20:30
• @Zarko I have edited my code. Maybe this one's better. – Sebastiano May 15 '19 at 22:36
• this is definitely the best solution; downvote (not me) was probably because it does not answer the OP's question – Brandon Kuczenski May 15 '19 at 23:06
• @Sebastiano, yes, now is better! i can read now without magnifying glass :-) :-) :-) – Zarko May 15 '19 at 23:42
• Thank you! I really appreciate the help. – user32147 May 16 '19 at 1:14

I propose this layout, which doesn't require multiline equations inside matrix. Instead, I suppressed the \cdots (unnecessary, from my point of view) and used the mmatrix (medium-size matrix) environment from nccmath, and the fleqn environment (same package). This size remains readable, as it is ~80 % of \displaystyle.

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{mathtools, nccmath}

\begin{document}
\vspace*{1cm}
\begin{fleqn}

\begin{aligned}[b] \label{eq:7} &\mathbf{y}(1)=\tanh(\mathbf{W}^{ro}\cdot\mathbf{h}(1)+\mathbf{W}^{io}\cdot\mathbf{x}(1)) = \\ & \begin{mmatrix} \begin{bmatrix} \tanh\Bigl(w^{ro}_{1,1} \alpha \tanh\bigl(w^{ir}_{1,1} x_{1}(1) + w^{ir}_{2,1} x_{2}(1)\bigr) + w^{ro}_{2,1} \alpha \tanh\bigl(w^{ir}_{1,2} x_{1}(1) + w^{ir}_{2,2} x_{2}(1)\bigr) + w^{io}_{1,1} x_{1}(1)+w^{io}_{2,1} x_{2}(1)\Bigr) \\ % \tanh\Bigl(w^{ro}_{1,2} \alpha \tanh\bigl(w^{ir}_{1,1} x_{1}(1) + w^{ir}_{2,1} x_{2}(1)\bigr) + w^{ro}_{2,2} \alpha \tanh\bigl(w^{ir}_{1,2} x_{1}(1)+w^{ir}_{2,2} x_{2}(1)\bigr) + w^{io}_{1,2} x_{1}(1)+w^{io}_{2,2} x_{2}(1)\Bigr) \end{bmatrix} \end{mmatrix} \end{aligned}
\end{fleqn}

\end{document}


• thank you for the suggestion! this is really helpful! – user32147 May 16 '19 at 1:07
• initially I was able to fit the matrix in one line without \cdot but I decided to put \cdot as I feel like the equation is easier to parse and read with them. – user32147 May 16 '19 at 1:16