# Align elements inside a matrix

How can I improve the alignment inside this matrix equation?

My Tex code is

$$\begin{bmatrix} d\varepsilon_v\\ d\varepsilon_q \end{bmatrix} = \left[ \begin{matrix} \dfrac{1}{K^{'}} & 0 \\ 0 & \dfrac{1}{3 G^{'}} \end{matrix} +\ \dfrac{ \begin{matrix} \dfrac{\partial g}{\partial p^{'}} \dfrac{\partial f}{\partial q} & \dfrac{\partial g}{\partial p^{'}} \dfrac{\partial f}{\partial q} \\ \dfrac{\partial g}{\partial q} \dfrac{\partial f}{\partial p^{'}} & \dfrac{\partial g}{\partial q} \dfrac{\partial f}{\partial q} \end{matrix}}{P} \right] \begin{bmatrix} dp^{'}\\ dq \end{bmatrix} \label{eq:149}$$


• Welcome. I find it hard to guess what the expected output would be. Commented Jul 31, 2023 at 9:43
• Please always provide MWE (Minimal working example), a small but complete document with your matrix, which we can compile as it is. Commented Jul 31, 2023 at 9:58

Something like this?

Observe that I've also replaced all instances of ^{'} with '. (Aside: In math mode, ' resolves to ^{\prime}.)

\documentclass{article} % or some other suitable document class
\usepackage{amsmath}    % for 'bmatrix' and 'pmatrix' env. and '\dfrac' macro
\newcommand\mystrut{\vphantom{\partial p'}} % typographic strut
% to fine-tune position of cells in first inner 2x2 matrix

\begin{document}

$$\label{eq:149} \begin{bmatrix} d\varepsilon_v \\ \addlinespace d\varepsilon_q \end{bmatrix} = \left( % delimiter for sum of 2x2 matrices \begin{bmatrix} % first "inner" 2x2 matrix \dfrac{1}{K'\mystrut} & 0 \\ \addlinespace 0 & \dfrac{1}{3G'\mystrut} \end{bmatrix} + \dfrac{1}{P} % multiply matrix with a scalar \begin{bmatrix} % second "inner" 2x2 matrix \dfrac{\partial g}{\partial p'} \dfrac{\partial f}{\partial q} & \dfrac{\partial g}{\partial p'} \dfrac{\partial f}{\partial q} \\ \addlinespace \dfrac{\partial g}{\partial q} \dfrac{\partial f}{\partial p'} & \dfrac{\partial g}{\partial q} \dfrac{\partial f}{\partial q} \end{bmatrix} \right) \begin{bmatrix} dp'\\ \addlinespace dq \end{bmatrix}$$

\end{document}

• Interesting guess, but I’d switch parentheses with brac, because matrices seem to use the latter fences. Commented Jul 31, 2023 at 12:54
• @egreg - Thanks for your suggestion. I've modified the code to use square brackets as delimiters of all matrices and column vectors.
– Mico
Commented Jul 31, 2023 at 13:32
• But the outer fences, enclosing the matrix sum, should probably be parentheses Commented Jul 31, 2023 at 13:35
• Oh, +1 for the mind reading :-). But I agree with egreg: if matrices are denoted by brackets then the outer fences should rather be parentheses. Commented Jul 31, 2023 at 13:41
• @egreg - Thanks, I got it now. :-)
– Mico
Commented Jul 31, 2023 at 13:47