2

I have the following Tikzpicture which creates a table. My question is, the numbers in the upper left corners, is it posible to asign a variable with a matrix of values.

Using pseudo-code: Something like variable={-3,4,3,M; 0,-2,0,0; -1,-1,-3,M; 0,M,0,0} so that the values can be accessed using \variable{1,1}, for instance. The line \path (m-1-1) node {-3}; would be some like \path (m-1-1) node {\variable{1,1}};

enter image description here

\documentclass[border=1cm]{standalone}
\usepackage{graphicx} % Required for inserting images
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,fit,calc, automata}

\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}] { & & & & & & & & & & & \ & & & & & & & & & & & \ & & & & & & & & & & & \ & & & & & & & & & & & \ & & & & & & & & & & & \ & & & & & & & & & & & \ & & & & & & & & & & & \ & & & & & & & & & & & \ & & & & & & & & & & & \ & & & & & & & & & & & \ & & & & & & & & & & & \ & & & & & & & & & & & \ };

% Costos % Primera fila \path (m-1-1) node {-3}; \path (m-1-4) node {4}; \path (m-1-7) node {3}; \path (m-1-10) node {M};

% Segunda fila \path (m-4-1) node {0}; \path (m-4-4) node {-2}; \path (m-4-7) node {0}; \path (m-4-10) node {0}; \path (m-4-12) node {(-)};

% Tercera fila \path (m-7-1) node {-1}; \path (m-7-4) node {-1}; \path (m-7-7) node {-3}; \path (m-7-10) node {M};

% Cuarta fila \path (m-10-1) node {0}; \path (m-10-3) node {(-)}; \path (m-10-4) node {M}; \path (m-10-7) node {0}; \path (m-10-10) node {0};

% Limites superiores y inferiores

% Segunda fila \path (m-4-3) node {8}; \path (m-4-6) node {4};

% Tercera fila \path (m-7-6) node {5}; \path (m-7-9) node {4};

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

% Lineas horizontales \foreach \i in {3,6,9} { \draw (m-\i-1.south west) -- (m-\i-12.south east); }

% Lineas verticales \foreach \j in {3,6,9} { \draw (m-1-\j.north east) -- (m-12-\j.south east); }

\foreach \k in {4-3,4-6, 7-6,7-9} { \draw (m-\k.south west) rectangle (m-\k.north east); }

\end{tikzpicture} \end{document}

John W. de Burgh
  • 1,245
  • 1
  • 7
  • 15

2 Answers2

5

This is very similar to my previous answer: read a list of items

You can use a vector-like expression by making a list (using the listofitems package) and then retrieving the elements individually.

The list are created using the \readlist command.

The list is comma separated and the elements are recovered using the notation

\ListName[<order of the element>]

For this example, two one-dimensional lists will suffice, the first for the left corner values and the second for the right corner values: \varR and \varL, with 16 elements each. (Some are empty)

A

\documentclass[border=1cm]{standalone}
\usepackage{graphicx} % Required for inserting images
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,fit,calc, automata}

\usepackage{listofitems}% added <<<

\begin{document}

\readlist\varL{-3,4,3,M,0,-2,0,0,-1,-1,-3,M,0,M,0,0}% <<<<<<<<<<<<<<<< \readlist\varR{,,,,8,4,,(-),,5,4,,(-),,,}% <<<<<<<<<<<<<<<<

\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}]
{
    &amp;    &amp;  &amp;   &amp;   &amp;    &amp;     &amp;    &amp;    &amp;    &amp;    &amp;     \\
    &amp;    &amp;  &amp;   &amp;   &amp;    &amp;     &amp;    &amp;    &amp;    &amp;    &amp;     \\
    &amp;    &amp;  &amp;   &amp;   &amp;    &amp;     &amp;    &amp;    &amp;    &amp;    &amp;     \\
    &amp;    &amp;  &amp;   &amp;   &amp;    &amp;     &amp;    &amp;    &amp;    &amp;    &amp;     \\
    &amp;    &amp;  &amp;   &amp;   &amp;    &amp;     &amp;    &amp;    &amp;    &amp;    &amp;     \\
    &amp;    &amp;  &amp;   &amp;   &amp;    &amp;     &amp;    &amp;    &amp;    &amp;    &amp;     \\
    &amp;    &amp;  &amp;   &amp;   &amp;    &amp;     &amp;    &amp;    &amp;    &amp;    &amp;     \\
    &amp;    &amp;  &amp;   &amp;   &amp;    &amp;     &amp;    &amp;    &amp;    &amp;    &amp;     \\
    &amp;    &amp;  &amp;   &amp;   &amp;    &amp;     &amp;    &amp;    &amp;    &amp;    &amp;     \\
    &amp;    &amp;  &amp;   &amp;   &amp;    &amp;     &amp;    &amp;    &amp;    &amp;    &amp;     \\
    &amp;    &amp;  &amp;   &amp;   &amp;    &amp;     &amp;    &amp;    &amp;    &amp;    &amp;     \\
    &amp;    &amp;  &amp;   &amp;   &amp;    &amp;     &amp;    &amp;    &amp;    &amp;    &amp;     \\
};


% Costos
% Primera fila
\path (m-1-1) node  {\varL[1]};
\path (m-1-4) node  {\varL[2]};
\path (m-1-7) node  {\varL[3]};
\path (m-1-10) node {\varL[4]};

% Segunda fila
\path (m-4-1) node  {\varL[5]};
\path (m-4-4) node  {\varL[6]};
\path (m-4-7) node  {\varL[7]};
\path (m-4-10) node {\varL[8]};

% Tercera fila
\path (m-7-1) node  {\varL[9]};
\path (m-7-4) node  {\varL[10]};
\path (m-7-7) node  {\varL[11]};
\path (m-7-10) node {\varL[12]};

% Cuarta fila
\path (m-10-1) node  {\varL[13]};
\path (m-10-4) node  {\varL[14]};
\path (m-10-7) node  {\varL[15]};
\path (m-10-10) node {\varL[16]};

% Limites superiores y inferiores

% Segunda fila
\path (m-4-3) node  {\varR[5]};
\path (m-4-6) node  {\varR[6]};
\path (m-4-12) node {\varR[8]};

% Tercera fila
\path (m-7-6) node  {\varR[10]};
\path (m-7-9) node  {\varR[11]};

%Cuarta fila
\path (m-10-3) node  {\varR[13]};

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

% Lineas horizontales
\foreach \i in {3,6,9} {
    \draw (m-\i-1.south west) -- (m-\i-12.south east);
}

% Lineas verticales
\foreach \j in {3,6,9} {
    \draw (m-1-\j.north east) -- (m-12-\j.south east);
}

\foreach \k in {4-3,4-6,
    7-6,7-9} {
    \draw (m-\k.south west) rectangle (m-\k.north east);
}

\end{tikzpicture}

\end{document}

Simon Dispa
  • 39,141
2

With a slightly different list syntax, namely

{-3,  4,  3, M},
{ 0, -2,  0, 0},
{-1, -1, -3, M},
{ 0,  M,  0, 0}

you can do this on-the-fly.

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix}
\NewDocumentCommand{\fforeach}{D(){m} O{} m O{$\col$} m}{
\foreach[count=\iRow] \row in {#5}
  \foreach[count=\iCol] \col in \row{
    % fragile check for empty \col
    \expandafter\unless\expandafter\ifx\expandafter\relax\col\relax
      \node[anchor={#3}, at={(#1-\iRow-\iCol.#3)}, node contents={#4},#2];
    \fi}}
\begin{document}
\begin{tikzpicture}[inner sep=+0pt, outer sep=+0pt]
\matrix (m) [
  matrix of nodes,
  nodes in empty cells,
  row sep=+-.5\pgflinewidth,
  column sep=+-.5\pgflinewidth,
  nodes={draw, minimum size=3cm}
]{ & & & \\ & & & \\ & & & \\ & & & \\};
\tikzset{minimum size=1cm}
\fforeach{north west}{
  {-3,  4,  3, M},
  { 0, -2,  0, 0},
  {-1, -1, -3, M},
  { 0,  M,  0, 0}}
\fforeach[draw]{north east}{, {8,4}, {,5,4}}
\fforeach{north east}{, {,,,(-)},,(-)}
\end{tikzpicture}
\end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821