# How to control the coordinates in Tikz & Matrix?

I use Tikz with Matrix because I need to draw arrows and write on and beside the matrix. However, I don´t control the coordinates well other than using trail and error. For instance in the matrix below I have put an X at (0,0) and it is not in the centre of the matrix. However, if the matrix is 4x4 than it is in the centre. Is there any convenient way to know the coordinates?

\documentclass[border=1cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}
\matrix(m) [matrix of math nodes, nodes in empty cells, nodes={minimum size=1cm, outer sep=0pt, text height=1.5ex, text depth=.25ex}]
{
36     &   & 32    &   & 33    &   \\
&   &       &   &       &   \\
10     &   & 8     &   & 7     &   \\
&   &       &   &       &   \\
12     &   & 17    &   & 16    &   \\
&   &       &   &       &   \\
23     &   & 15    &   & 16    &   \\
&   &   &       &   &       &   \\
};

\draw (m-1-1.north west) rectangle (m-8-6.south east);

% Lineas horizontales
\foreach \i in {2,4,6,8} {
\draw (m-\i-1.south west) -- (m-\i-6.south east);
}

% Lineas verticales
\foreach \j in {2,4,6} {
\draw (m-1-\j.north east) -- (m-8-\j.south east);
}

% dual ui
\node [font=\color{red}] at (3.5,3) {$\ge 10$};
\node [font=\color{red}] at (3.5,1) {$\ge 8$};
\node [font=\color{red}] at (3.5,-1) {$=4$};
\node [font=\color{red}] at (3.5,-3) {$=3$};

% dual vj
\node [font=\color{red}] at (-2.5,-5) {$\le12$};
\node [font=\color{red}] at (-0.5,-5) {$=12$};
\node [font=\color{red}] at (1.5,-5) {$=6$};

\node [font=\color{red}] at (0,0) {X};

\end{tikzpicture}

\end{document}

• I added a solution where you don't have to manually place your red nodes around your matrix, allowing you to change your matrix properties without having to place them manually afterwards. Commented Mar 13, 2021 at 17:27

You can do two things two see what's happening:

1. add \draw [cyan, densely dotted] (-5,-5) grid (5,5); at the start of the graph, to draw a cartesian grid,
2. add a to the nodes the draw=red (to draw the borders). Now you see:

Where you notice you have an extra column, namely a stray & in the last line...

(You should use m.center and coordinates relative to the m matrix, though!)

\documentclass[border=1cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}

\begin{tikzpicture}

\draw [cyan, densely dotted] (-5,-5) grid (5,5);

\matrix(m) [matrix of math nodes, nodes in empty cells, nodes={minimum size=1cm, outer sep=0pt, text height=1.5ex, text depth=.25ex, draw=red}]
{
36     &   & 32    &   & 33    &   \\
&   &       &   &       &   \\
10     &   & 8     &   & 7     &   \\
&   &       &   &       &   \\
12     &   & 17    &   & 16    &   \\
&   &       &   &       &   \\
23     &   & 15    &   & 16    &   \\
&   &   &       &   &       &   \\
};

\draw (m-1-1.north west) rectangle (m-8-6.south east);

% Lineas horizontales
\foreach \i in {2,4,6,8} {
\draw (m-\i-1.south west) -- (m-\i-6.south east);
}

% Lineas verticales
\foreach \j in {2,4,6} {
\draw (m-1-\j.north east) -- (m-8-\j.south east);
}

% dual ui
\node [font=\color{red}] at (3.5,3) {$\ge 10$};
\node [font=\color{red}] at (3.5,1) {$\ge 8$};
\node [font=\color{red}] at (3.5,-1) {$=4$};
\node [font=\color{red}] at (3.5,-3) {$=3$};

% dual vj
\node [font=\color{red}] at (-2.5,-5) {$\le12$};
\node [font=\color{red}] at (-0.5,-5) {$=12$};
\node [font=\color{red}] at (1.5,-5) {$=6$};

\node [font=\color{red}] at (m.center) {X};

\end{tikzpicture}

\end{document}


If you remove the extra & (and the debugging commands) you have:

• That's a very helpful answer. Commented Mar 13, 2021 at 17:06
• @Lisch64 thanks, happy to help. Commented Mar 13, 2021 at 17:18

\documentclass[border=1cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}
\matrix(m) [matrix of math nodes,  nodes in empty cells, nodes={draw,minimum size=1cm, outer sep=0pt, text height=1.5ex, text depth=.25ex}]
{
36     & 32       & 33  \\
10     & 8        & 7 \\
12        & 17       & 16  \\
23        & 15       & 16   \\
};

\draw (m-1-1.north west) --(m-4-3.south east)node[midway]{X};
\end{tikzpicture}

\end{document}


or

\documentclass[border=1cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{matrix, calc}

\begin{document}

\begin{tikzpicture}
\matrix(m) [matrix of math nodes,  nodes in empty cells, nodes={draw,minimum size=1cm, outer sep=0pt, text height=1.5ex, text depth=.25ex}]
{
36     & 32       & 33  \\
10     & 8        & 7 \\
12        & 17       & 16  \\
23        & 15       & 16   \\
};

%\draw (m-1-1.north west) --(m-4-3.south east)node[midway]{X};
\node at($(m-1-1.north west)!0.5!(m-4-3.south east)$)[red]{X};

\end{tikzpicture}

\end{document}


A comprehensive answer on which I was working while you received other ones.

It's definitely not a good idea to try and place your red nodes around your matrix with direct coordinates. You would replace that by relative references to your matrix nodes.

Mostly, you can use (m-1-1.south east) reference to any node corner. Then create paths between those corners and place your nodes along those paths. Using this method, you can now change your matrix properties without having to place once again manually all your red legends around.

\documentclass[border=1cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{matrix,positioning}

\begin{document}

\begin{tikzpicture}

\matrix(m) [matrix of math nodes, nodes in empty cells, nodes={minimum size=1cm, outer sep=0pt, text height=1.5ex, text depth=.25ex}]
{
36     &   & 32    &   & 33    &   \\
&   &       &   &       &   \\
10     &   & 8     &   & 7     &   \\
&   &       &   &       &   \\
12     &   & 17    &   & 16    &   \\
&   &       &   &       &   \\
23     &   & 15    &   & 16    &   \\
&   &   &       &   &       &   \\
};

\draw (m-1-1.north west) rectangle (m-8-6.south east);

% Lineas horizontales
\foreach \i in {2,4,6,8} {
\draw (m-\i-1.south west) -- (m-\i-6.south east);
}

% Lineas verticales
\foreach \j in {2,4,6} {
\draw (m-1-\j.north east) -- (m-8-\j.south east);
}

\begin{scope}[every node/.style={font=\color{red}}]

% dual ui
\foreach \i/\v in {
1/$\ge 10$,
3/$\ge 8$,
5/$=4$,
7/$=3$}
{
\pgfmathtruncatemacro\j{\i+1}
\path (m-\i-6.north east) -- (m-\j-6.south east) node[midway,right=10pt] {\v};
}

% dual vj
\foreach \i/\v in {
1/$\le12$,
3/$=12$,
5/$=6$}
{
\pgfmathtruncatemacro\j{\i+1}
\path (m-8-\i.south west) -- (m-8-\j.south east) node[midway,below=10pt] {\v};
}

\path (m-1-1) -- (m-8-6) node [midway] {X};
\end{scope}

\end{tikzpicture}

\end{document}