4

I have several questions about a table I have created for an exercise.

  1. The numbers that are inside the circles. How can they be centred?
  2. I would like to shift the purple polygon, a little to the left and a little down.
  3. Is there a more easy way to do the table. Keep in mind that I used Tikz in order to draw on top of the table and I also need to do many iterations.

Many thanks in advance!

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,fit,automata}
\usepackage{adjustbox}

\usepackage[utf8]{inputenc}



\begin{document}



\begin{adjustbox}{max totalsize={.9\textwidth}{.9\textheight},center}

\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);



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

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

% Solucion
\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};



% poligono
\draw [purple, dashed,very thick](m-2-1.north) -- (m-2-9.north) -- (m-10-9.north) -- (m-10-3.north) -- (m-4-3.north) -- (m-4-1.north) -- (m-2-1.north);


\end{tikzpicture}
\end{adjustbox}

\end{document}

1 Answer 1

1
  • Load the TikZ library calc. It allows you to write ($(a)!0.5!(b)$) for the point between (a) and (b).
  • Draw the circled numbers as circled nodes, not as fitted ones.
  • Define the polygon with coordinates relative to its starting point, then you can shift it by shifting the starting point.
  • Use \foreach loops to avoid duplication.
  • When drawing a rectangular polygon, you can drop every second node by using -| and |- instead of --.

enter image description here

\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,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);

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

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

% Solucion
\foreach \a/\b in {1-9/2-10,3-1/4-2,5-7/6-8,7-5/8-6,9-3/10-4}
  \node[draw=blue,circle] at ($(m-\a)!0.5!(m-\b)$) {\color{black}1};

\foreach \a/\b in {1-1/2-2,5-3/6-4,7-7/8-8,9-9/10-10}
  \node[draw=blue,circle] at ($(m-\a)!0.5!(m-\b)$) {\color{black}0};

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

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

% poligono
% shift it by changing (-0.3,-0.3)
\draw [purple, dashed,very thick] (m-2-1.north)++(-0.3,-0.3)coordinate(X) -| ++(8,-8) -| ++(-6,6) -| (X);
\end{tikzpicture}
\end{document}
1
  • Thanks! Much better and visually more pleasing. Much appreciated. Commented Nov 30, 2020 at 12:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .