# Simple calculations inside Tikz matrix

I have this rather complicated matrix and it would be great to do some calculations directly in LaTeX as I need to generate several iterations for each problem I prepare.

The calculations are simple, I would like to calculate the number in the lower right corner for each cell (in blue). They are calculated as cij-(ui+vj) e.g. the number in the upper left corner minus the sum of the numbers in red of the intersecting row and column. For instance, the first cell: 1-(0+1)=0

I know that there are several packages for calculations but I don't know which one would be the most appropriate.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{matrix,fit,calc}

\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}]
{
1  &                   & 2 &                   & 3 &                   &   5    &                   & 0 &                  \\
&\textcolor{blue}{0}&   &\textcolor{blue}{1}&   &\textcolor{blue}{2}&        &\textcolor{blue}{4}&   &\textcolor{blue}{0}\\
0   &                   & 1 &                   & 5 &                   &   3    &                   & 3 &                  \\
&\textcolor{blue}{0}&   &\textcolor{blue}{1}&   &\textcolor{blue}{5}&        &\textcolor{blue}{3}&   &\textcolor{blue}{4}\\
3   &                   & 0 &                   & 4 &                   &   0    &                   & 3 &                  \\
&\textcolor{blue}{3}&   &\textcolor{blue}{0}&   &\textcolor{blue}{4}&        &\textcolor{blue}{0}&   &\textcolor{blue}{4}\\
5   &                   & 7 &                   & 0 &                   &  0     &                   & 2 &                  \\
&\textcolor{blue}{5}&   &\textcolor{blue}{7}&   &\textcolor{blue}{0}&        &\textcolor{blue}{0}&   &\textcolor{blue}{3}\\
3  &                   & 1 &                   & 4 &                   & 5      &                   & 0 &                  \\
&\textcolor{blue}{3}&   &\textcolor{blue}{0}&   &\textcolor{blue}{3}&        &\textcolor{blue}{5}&   &\textcolor{blue}{0}\\
};

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

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

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

% Solution
\node[draw=blue,circle,fit=(m-1-9)(m-2-10),inner sep = -19pt] {1};
\node[draw=blue,circle,fit=(m-3-1)(m-4-2),inner sep = -19pt] {1};
\node[draw=blue,circle,fit=(m-5-7)(m-6-8),inner sep = -19pt] {1};
\node[draw=blue,circle,fit=(m-7-5)(m-8-6),inner sep = -19pt] {1};
\node[draw=blue,circle,fit=(m-9-3)(m-10-4),inner sep = -19pt] {1};

\node[draw=blue,circle,fit=(m-1-1)(m-2-2),inner sep = -19pt] {0};
\node[draw=blue,circle,fit=(m-5-3)(m-6-4),inner sep = -19pt] {0};
\node[draw=blue,circle,fit=(m-7-7)(m-8-8),inner sep = -19pt] {0};
\node[draw=blue,circle,fit=(m-9-9)(m-10-10),inner sep = -19pt] {0};

% dual ui
\node [font=\color{red}] at (6,4) {0};
\node [font=\color{red}] at (6,2) {-1};
\node [font=\color{red}] at (6,0) {-1};
\node [font=\color{red}] at (6,-2) {-1};
\node [font=\color{red}] at (6,-4) {0};

% dual vj
\node [font=\color{red}] at (-4,-6) {1};
\node [font=\color{red}] at (-2,-6) {1};
\node [font=\color{red}] at (0,-6) {1};
\node [font=\color{red}] at (2,-6) {1};
\node [font=\color{red}] at (4,-6) {0};

\end{tikzpicture}

\end{document}


I give you a solution that not uses matrix library. I made a \newcommand that defines a "square", draw it and put inside all the values, given and calculated. This is my code:

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usepackage{ifthen}

\def\l{1.2} % cell lenght
\newcommand{\square}[4] % x-position, y-position, main value, circled value (if any)
{%
\begin{scope}[shift={(#1*\l,-#2*\l)}]
\draw    (0,0) rectangle (\l,-\l);
\node at (0.25*\l ,-0.25*\l) {#3};
% calculate bottom right corener:
\pgfmathtruncatemacro{\temp}{#3-\bottom[#1]-\right[#2]}
\node[blue] at (0.75*\l ,-0.75*\l) {$\temp$};
% circled value
\ifthenelse{\equal{#4}{}}
{} % then do nothing
{%   else...
\draw[blue] (0.5*\l ,-0.5*\l) circle (0.2);
\node at (0.5*\l ,-0.5*\l) {$#4$};
}
\end{scope}
}

\begin{document}
\begin{tikzpicture}[line cap=round,line join=round]
\def\right {{0,-1,-1,-1,0}} % right  red values (dual ui)
\def\bottom{{1, 1, 1, 1,0}} % bottom red values (dual vj)
% row 1
\square{0}{0}{1}{0};
\square{1}{0}{2}{};
\square{2}{0}{3}{};
\square{3}{0}{5}{};
\square{4}{0}{0}{1};
% row 2
\square{0}{1}{0}{1};
\square{1}{1}{1}{};
\square{2}{1}{5}{};
\square{3}{1}{3}{};
\square{4}{1}{3}{};
% row 3
\square{0}{2}{3}{};
\square{1}{2}{0}{0};
\square{2}{2}{4}{};
\square{3}{2}{0}{1};
\square{4}{2}{3}{};
% row 4
\square{0}{3}{5}{};
\square{1}{3}{7}{};
\square{2}{3}{0}{1};
\square{3}{3}{0}{0};
\square{4}{3}{2}{};
% row 5
\square{0}{4}{3}{};
\square{1}{4}{1}{1};
\square{2}{4}{4}{};
\square{3}{4}{5}{};
\square{4}{4}{0}{0};
% red values (dual ui and dual vj)
\foreach\i in {0,...,4}
{
\pgfmathtruncatemacro{\temp}{\bottom[\i]}
\node[red] at (\i*\l+0.5*\l,-5.5*\l) {$\temp$};
\pgfmathtruncatemacro{\temp}{\right[\i]}
\node[red,left] at (5.75*\l,-\i*\l-0.5*\l) {$\temp$};
}
\end{tikzpicture}
\end{document}


And the result:

• Very elegant solution! Great routine. Feb 15, 2021 at 14:36