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

by user32147   Last Updated May 15, 2019 20:23 PM

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?

\begin{equation} \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}
\end{equation}

Current output looks like below where good portion of equations go over margin: enter image description here

Any help will be greatly appreciated



Answers 2


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

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

\begin{document}
\begin{equation} \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{equation}
\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.

enter image description here

Zarko
Zarko
May 15, 2019 20:02 PM

A short proposal using \resizebox to decrease the size the characters if you want to keep the same length without splitting it. You can also use the geometry package to have more space and margin control. enter image description here

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

\begin{document}
\begin{equation} \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)) \\
&=\!\!\resizebox{.85 \textwidth}{!}{$\begin{bmatrix} \tanh\Big(w^{\mathrm{ro}}_{1,1}\cdot\alpha\cdot \tanh\big(w^{\mathrm{ir}}_{1,1}\cdot x_{1}(1)+w^{\mathrm{ir}}_{2,1}\cdot x_{2}(1)\big) + w^{\mathrm{ro}}_{2,1}\cdot \alpha \cdot \tanh\big(w^{\mathrm{ir}}_{1,2}\cdot x_{1}(1)+w^{\mathrm{ir}}_{2,2} \cdot x_{2}(1)\big) + w^{\mathrm{io}}_{1,1} \cdot x_{1}(1)+w^{\mathrm{io}}_{2,1} \cdot x_{2}(1)\Big) \\ 
\tanh\Big(w^{ro}_{1,2}\cdot \alpha \cdot \tanh\big(w^{\mathrm{ir}}_{1,1}\cdot x_{1}(1)+w^{\mathrm{ir}}_{2,1}\cdot x_{2}(1)\big) + w^{\mathrm{ro}}_{2,2}\cdot \alpha \cdot \tanh\big(w^{\mathrm{ir}}_{1,2}\cdot x_{1}(1)+w^{\mathrm{ir}}_{2,2} \cdot x_{2}(1)\big) + w^{\mathrm{io}}_{1,2}\cdot x_{1}(1)+w^{\mathrm{io}}_{2,2}\cdot x_{2}(1)\Big) \end{bmatrix}$}
\end{aligned}
\end{equation}
\end{document}
Sebastiano
Sebastiano
May 15, 2019 20:15 PM

Related Questions


Updated April 23, 2018 20:23 PM

Updated August 16, 2017 04:23 AM

Updated August 02, 2019 15:23 PM

Updated August 29, 2018 13:23 PM