Math & ScienceBig matrix equation and matrix rows vertical alignment

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
mgulin
Posts: 18
Joined: Thu May 31, 2012 7:16 pm

Big matrix equation and matrix rows vertical alignment

Post by mgulin »

Hi all!

Is it possible to align rows of 2 matrices, please see attached fig for example. Rows are not aligned, but I want them to be, so that the equation is more intuitive.

Thanks!

Best,
Marko.
Attachments
meq.png
meq.png (29.79 KiB) Viewed 8502 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
mgulin
Posts: 18
Joined: Thu May 31, 2012 7:16 pm

Big matrix equation and matrix rows vertical alignment

Post by mgulin »

I did the hack with \vphantom, but that is not elegant solution. I hope there is a package that enables this.
Attachments
matrices.png
matrices.png (42.53 KiB) Viewed 8495 times
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Re: Big matrix equation and matrix rows vertical alignment

Post by Johannes_B »

Can you post the code that outputs the screenshot?
It is much easier to help having some code. Nobody wants to type in the numbers to make a test document.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
mgulin
Posts: 18
Joined: Thu May 31, 2012 7:16 pm

Big matrix equation and matrix rows vertical alignment

Post by mgulin »

Here I post the code without \vphantom hack:

Code: Select all

\documentclass[conference]{article}


\usepackage[cmex10]{amsmath}
\usepackage{amssymb}

\usepackage{graphicx}
\newcommand*{\mathminus}{%
  \leavevmode
  \hphantom{0}%
  \llap{%
    \settowidth{\dimen0 }{$0$}%
    \resizebox{1.1\dimen0 }{\height}{$-$}%
  }%
}

\begin{document}

$$
\begin{bmatrix}
\mathminus m_u^{(1)} & 0 \\
\mathminus M_{u,\varepsilon}^{(1)} & 0 \\
\mathminus M_u^{(1)} & 0 \\
m_u^{(1)} & 0 \\
\mathminus m_u^{(1)} & 0 \\
M_u^{(1)} & 0 \\
0 & \mathminus m_u^{(2)} \\
0 & \mathminus M_{u,\varepsilon}^{(2)} \\
0 & \mathminus M_u^{(2)} \\
0 & m_u^{(2)} \\
0 & \mathminus m_u^{(2)} \\
0 & M_u^{(2)} \\
0 & 0 \\
0 & 0 \\
0 & 0 \\
0 & 0 \\
0 & 0 \\
0 & 0
\end{bmatrix} \delta_k + 
\begin{bmatrix}
0 & 0 \\
0 & 0 \\
1 & 0 \\
\mathminus 1 & 0 \\
1 & 0 \\ 
\mathminus 1 & 0 \\ 
0 & 0 \\ 
0 & 0 \\ 
0 & 1 \\ 
0 & \mathminus 1 \\ 
0 & 1 \\ 
0 & \mathminus 1 \\ 
0 & 0 \\ 
0 & 0 \\ 
0 & 0 \\
0 & 0 \\
0 & 0 \\
0 & 0
\end{bmatrix} z_k \leq
\begin{bmatrix}
1 & 0 \\
\mathminus 1 & 0 \\
0 & 0 \\
0 & 0 \\
1 & 0 \\
\mathminus 1 & 0 \\
0 & 1 \\
0 & \mathminus 1 \\
0 & 0 \\
0 & 0 \\
0 & 1 \\
0 & \mathminus 1 \\
0 & 0 \\
0 & 0 \\
0 & 0 \\
0 & 0 \\
\mathminus 1 & \mathminus 1 \\
1 & 1
\end{bmatrix} u_k +
\begin{bmatrix}
0 & 0 \\
0 & 0 \\
0 & 0 \\
0 & 0 \\
0 & 0 \\
0 & 0 \\
0 & 0 \\
0 & 0 \\
0 & 0 \\
0 & 0 \\
0 & 0 \\
0 & 0 \\
1 & 0 \\
\mathminus 1 & 0 \\
0 & 1 \\
0 & \mathminus 1 \\
0 & 0 \\
0 & 0
\end{bmatrix} x_k +
\begin{bmatrix}
\mathminus m_u^{(1)} \\
\mathminus \varepsilon \\
0 \\
0 \\
\mathminus m_u^{(1)} \\
M_u^{(1)} \\
\mathminus m_u^{(2)} \\
\mathminus \varepsilon \\
0 \\
0 \\
\mathminus m_u^{(2)} \\
M_u^{(2)} \\
m_x^{(1)} \\
M_x^{(1)} \\
m_x^{(2)} \\
M_x^{(2)} \\
\mathminus m_{d,k} \\
M_{d,k}
\end{bmatrix}
$$

\end{document}
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Big matrix equation and matrix rows vertical alignment

Post by Johannes_B »

I renewed the arraystretch inside the math environment (be sure
not to use $$ <math> $$) because those massive
entries looked to cramped.

Code: Select all

\documentclass{article}
\usepackage[cmex10]{mathtools}

\usepackage{graphicx}
\newcommand*{\mathminus}{%
	\leavevmode
	\hphantom{0}%
	\llap{%
		\settowidth{\dimen0 }{$0$}%
		\resizebox{1.1\dimen0 }{\height}{$-$}%
	}%
}

\begin{document}

%$$
\[
\renewcommand{\arraystretch}{1.35}
\begin{bmatrix*}
	\mathminus m_u^{(1)} & 0 \\
	\mathminus M_{u,\varepsilon}^{(1)} & 0 \\
	\mathminus M_u^{(1)} & 0 \\
	m_u^{(1)} & 0 \\
	\mathminus m_u^{(1)} & 0 \\
	M_u^{(1)} & 0 \\
	0 & \mathminus m_u^{(2)} \\
	0 & \mathminus M_{u,\varepsilon}^{(2)} \\
	0 & \mathminus M_u^{(2)} \\
	0 & m_u^{(2)} \\
	0 & \mathminus m_u^{(2)} \\
	0 & M_u^{(2)} \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0
\end{bmatrix*} \delta_k +
\begin{bmatrix*}[r]
	0 & 0 \\
	0 & 0 \\
	1 & 0 \\
	\mathminus 1 & 0 \\
	1 & 0 \\
	\mathminus 1 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 1 \\
	0 & \mathminus 1 \\
	0 & 1 \\
	0 & \mathminus 1 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0
\end{bmatrix*} z_k \leq
\begin{bmatrix*}[r]
	1 & 0 \\
	\mathminus 1 & 0 \\
	0 & 0 \\
	0 & 0 \\
	1 & 0 \\
	\mathminus 1 & 0 \\
	0 & 1 \\
	0 & \mathminus 1 \\
	0 & 0 \\
	0 & 0 \\
	0 & 1 \\
	0 & \mathminus 1 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	\mathminus 1 & \mathminus 1 \\
	1 & 1
\end{bmatrix*} u_k +
\begin{bmatrix*}[r]
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	1 & 0 \\
	\mathminus 1 & 0 \\
	0 & 1 \\
	0 & \mathminus 1 \\
	0 & 0 \\
	0 & 0
\end{bmatrix*} x_k +
\begin{bmatrix*}
	\mathminus m_u^{(1)} \\
	\mathminus \varepsilon \\
	0 \\
	0 \\
	\mathminus m_u^{(1)} \\
	M_u^{(1)} \\
	\mathminus m_u^{(2)} \\
	\mathminus \varepsilon \\
	0 \\
	0 \\
	\mathminus m_u^{(2)} \\
	M_u^{(2)} \\
	m_x^{(1)} \\
	M_x^{(1)} \\
	m_x^{(2)} \\
	M_x^{(2)} \\
	\mathminus m_{d,k} \\
	M_{d,k}
\end{bmatrix*}
%$$
\]

\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
mgulin
Posts: 18
Joined: Thu May 31, 2012 7:16 pm

Big matrix equation and matrix rows vertical alignment

Post by mgulin »

Wow, thanks! This was very helpful.
Why \[ and \] are used, what do they do? And why bmatrix* environment, because of the alignment?

Another issuse: now that you've aligned contents to the right, the right bracket is too close to contents. I can add hphantom to all entries, but is there any way to do this automatically? (only for right bracket)

Best,
Marko.
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Big matrix equation and matrix rows vertical alignment

Post by Johannes_B »

Using $$ math $$ is the old TeX way. LaTeX uses \[ math \] to do the task. The starred variant for the different matrices is provided by package mathtools. If you have a lot of maths, you should check out the documentation. There are some useful things in there.
Check out the following, it looks as the content is too close, but actually it just appears like that, because youre other content is so spacy.

Code: Select all

\documentclass{article}
\usepackage[cmex10]{mathtools}

\usepackage{graphicx}
\newcommand*{\mathminus}{%
	\leavevmode
	\hphantom{0}%
	\llap{%
		\settowidth{\dimen0 }{$0$}%
		\resizebox{1.1\dimen0 }{\height}{$-$}%
	}%
}

\begin{document}

%$$
\[
\renewcommand{\arraystretch}{1.35}
\begin{bmatrix*}
	\mathminus m_u^{(1)} & 0 \\
	\mathminus M_{u,\varepsilon}^{(1)} & 0 \\
	\mathminus M_u^{(1)} & 0 \\
	m_u^{(1)} & 0 \\
	\mathminus m_u^{(1)} & 0 \\
	M_u^{(1)} & 0 \\
	0 & \mathminus m_u^{(2)} \\
	0 & \mathminus M_{u,\varepsilon}^{(2)} \\
	0 & \mathminus M_u^{(2)} \\
	0 & m_u^{(2)} \\
	0 & \mathminus m_u^{(2)} \\
	0 & M_u^{(2)} \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0
\end{bmatrix*} \delta_k +
\begin{bmatrix*}[r]
	0 & 0 \\
	0 & 0 \\
	1 & 0 \\
	\mathminus 1 & 0 \\
	1 & 0 \\
	\mathminus 1 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 1 \\
	0 & \mathminus 1 \\
	0 & 1 \\
	0 & \mathminus 1 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0
\end{bmatrix*} z_k \leq
\begin{bmatrix*}[r]
	1 & 0 \\
	\mathminus 1 & 0 \\
	0 & 0 \\
	0 & 0 \\
	1 & 0 \\
	\mathminus 1 & 0 \\
	0 & 1 \\
	0 & \mathminus 1 \\
	0 & 0 \\
	0 & 0 \\
	0 & 1 \\
	0 & \mathminus 1 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	\mathminus 1 & \mathminus 1 \\
	1 & 1
\end{bmatrix*} u_k +
\begin{bmatrix*}[r]
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	0 & 0 \\
	1 & 0 \\
	\mathminus 1 & 0 \\
	0 & 1 \\
	0 & \mathminus 1 \\
	0 & 0 \\
	0 & 0
\end{bmatrix*} x_k +
\begin{bmatrix*}
	\mathminus m_u^{(1)} \\
	\mathminus \varepsilon \\
	0 \\
	0 \\
	\mathminus m_u^{(1)} \\
	M_u^{(1)} \\
	\mathminus m_u^{(2)} \\
	\mathminus \varepsilon \\
	0 \\
	0 \\
	\mathminus m_u^{(2)} \\
	M_u^{(2)} \\
	m_x^{(1)} \\
	M_x^{(1)} \\
	m_x^{(2)} \\
	M_x^{(2)} \\
	\mathminus m_{d,k} \\
	M_{d,k}
\end{bmatrix*}
+
\begin{bmatrix*}
	0 \\
	0 \\
	0 \\
	0 \\
	0 \\
	0 \\
	0 \\
	0 \\
	0 \\
	0 \\
	0 \\
	0 \\
	0 \\
	0 \\
	0 \\
	0 \\
	0 \\
	0 \\
\end{bmatrix*}
1
%$$
\]

\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Post Reply