# alignment of different elements inside and outside matrix in tikz

I am facing issues in getting the different elements of the following tikzpicture at correct positions. I think the problem arises because the width of elements within the matrix differs.

\documentclass[10pt]{article}

\usepackage[margin=0.75in]{geometry}

\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing,calc,graphs,decorations.markings,intersections,positioning}
\usepackage{mathtools}
\usepackage{amsmath}
\usepackage{amsthm} %for proof

\begin{document}

$X= \begin{tikzpicture}[baseline=-0.5ex] \matrix [matrix of math nodes,left delimiter=(,right delimiter=),row sep=1cm,column sep=1cm] (m) { 0&\frac{p^{I}_{1S}.F^{-1}_{1S}(y^{f}_{2S}+y^{I}_{2S})}{p^{f}_{2S}.y^{f}_{2S}+p^{I}_{2S}.y^{I}_{2S}}&0&0 \\ \frac{p^{I}_{2S}.F^{-1}_{2S}(y^{f}_{1S}+y^{I}_{1S})}{p^{f}_{1S}.y^{f}_{1S}+p^{I}_{1S}.y^{I}_{1S}}&0&0&0 \\ 0&0&0&\frac{p^{I}_{1L}.F^{-1}_{1L}(y^{f}_{2L}+y^{I}_{2L})}{p^{f}_{2L}.y^{f}_{2L}+p^{I}_{2L}.y^{I}_{2L}} \\ 0&0&\frac{p^{I}_{1L}.F^{-1}_{1L}(y^{f}_{2L}+y^{I}_{2L})}{p^{f}_{2L}.y^{f}_{2L}+p^{I}_{2L}.y^{I}_{2L}}&0 \\}; \draw[dashed] (0.5*(m-1-2.north east)+0.5*(m-1-3.north west)) -- (0.5*(m-4-2.south east)+0.5*(m-4-3.south west)); \draw[dashed] (0.5*(m-2-1.south west)+0.5*(m-3-1.north west)) -- (0.5*(m-2-4.south east)+0.5*(m-3-4.north east)); \node[above=12pt of m-1-1] (top-1) {1}; \node[above=12pt of m-1-2] (top-2) {2}; \node[above=12pt of m-1-3] (top-3) {1}; \node[above=12pt of m-1-4] (top-4) {2}; \node[left=12pt of m-1-1] (left-1) {1}; \node[left=12pt of m-2-1] (left-2) {2}; \node[left=12pt of m-3-1] (left-3) {1}; \node[left=12pt of m-4-1] (left-4) {2}; \node[rectangle,above delimiter=\{] (del-top-1) at (0.5*(top-1.south) +0.5*(top-2.south)) {\tikz{\path (top-1.south west) rectangle (top-2.north east);}}; \node[above=10pt] at (del-top-1.north) {S}; \node[rectangle,above delimiter=\{] (del-top-2) at (0.5*(top-3.south) +0.5*(top-4.south)) {\tikz{\path (top-3.south west) rectangle (top-4.north east);}}; \node[above=10pt] at (del-top-2.north) {L}; \node[rectangle,left delimiter=\{] (del-left-1) at (0.5*(left-1.east) +0.5*(left-2.east)) {\tikz{\path (left-1.north east) rectangle (left-2.south west);}}; \node[left=10pt] at (del-left-1.west) {S}; \node[rectangle,left delimiter=\{] (del-left-2) at (0.5*(left-3.east) +0.5*(left-4.east)) {\tikz{\path (left-3.north east) rectangle (left-4.south west);}}; \node[left=10pt] at (del-left-2.west) {L}; \end{tikzpicture}$

\end{document}

• – Ignasi Sep 26 '14 at 14:39

Something like this. You need to define the size of the node as such in the option.

nodes={anchor=center,minimum width=3cm,minimum height=2cm}


Code

\documentclass[10pt]{article}

\usepackage[margin=0.75in]{geometry}

\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing,calc,graphs,decorations.markings,intersections,positioning}
\usepackage{mathtools}
\usepackage{amsmath}
\usepackage{amsthm} %for proof

\begin{document}

$X= \begin{tikzpicture}[baseline=-0.5ex] \matrix [matrix of math nodes,left delimiter=(,right delimiter=),row sep=1cm,column sep=1cm, nodes={anchor=center,minimum width=3cm,minimum height=2cm} ] (m) { 0&\frac{p^{I}_{1S}.F^{-1}_{1S}(y^{f}_{2S}+y^{I}_{2S})}{p^{f}_{2S}.y^{f}_{2S}+p^{I}_{2S}.y^{I}_{2S}}&0&0 \\ \frac{p^{I}_{2S}.F^{-1}_{2S}(y^{f}_{1S}+y^{I}_{1S})}{p^{f}_{1S}.y^{f}_{1S}+p^{I}_{1S}.y^{I}_{1S}}&0&0&0 \\ 0&0&0&\frac{p^{I}_{1L}.F^{-1}_{1L}(y^{f}_{2L}+y^{I}_{2L})}{p^{f}_{2L}.y^{f}_{2L}+p^{I}_{2L}.y^{I}_{2L}} \\ 0&0&\frac{p^{I}_{1L}.F^{-1}_{1L}(y^{f}_{2L}+y^{I}_{2L})}{p^{f}_{2L}.y^{f}_{2L}+p^{I}_{2L}.y^{I}_{2L}}&0 \\}; \draw[dashed] (0.5*(m-1-2.north east)+0.5*(m-1-3.north west)) -- (0.5*(m-4-2.south east)+0.5*(m-4-3.south west)); \draw[dashed] (0.5*(m-2-1.south west)+0.5*(m-3-1.north west)) -- (0.5*(m-2-4.south east)+0.5*(m-3-4.north east)); \node[above=12pt of m-1-1] (top-1) {1}; \node[above=12pt of m-1-2] (top-2) {2}; \node[above=12pt of m-1-3] (top-3) {1}; \node[above=12pt of m-1-4] (top-4) {2}; \node[left=12pt of m-1-1] (left-1) {1}; \node[left=12pt of m-2-1] (left-2) {2}; \node[left=12pt of m-3-1] (left-3) {1}; \node[left=12pt of m-4-1] (left-4) {2}; \node[yshift=-2cm,rectangle,above delimiter=\{] (del-top-1) at (0.5*(top-1.south) +0.5*(top-2.south)) {\tikz{\path (top-1.south west) rectangle (top-2.north east);}}; \node[above=10pt] at (del-top-1.north) {S}; \node[yshift=-2cm,rectangle,above delimiter=\{] (del-top-2) at (0.5*(top-3.south) +0.5*(top-4.south)) {\tikz{\path (top-3.south west) rectangle (top-4.north east);}}; \node[above=10pt] at (del-top-2.north) {L}; \node[rectangle,left delimiter=\{] (del-left-1) at (0.5*(left-1.east) +0.5*(left-2.east)) {\tikz{\path (left-1.north east) rectangle (left-2.south west);}}; \node[left=10pt] at (del-left-1.west) {S}; \node[rectangle,left delimiter=\{] (del-left-2) at (0.5*(left-3.east) +0.5*(left-4.east)) {\tikz{\path (left-3.north east) rectangle (left-4.south west);}}; \node[left=10pt] at (del-left-2.west) {L}; \end{tikzpicture}$

\end{document}