我有以下 Tikzpicture,它创建了一个表格。我的问题是,左上角的数字,是否可以用值矩阵分配变量。
使用伪代码:类似于 variable={-3,4,3,M; 0,-2,0,0; -1,-1,-3,M; 0,M,0,0},这样就可以使用 \variable{1,1} 访问值。该行\path (m-1-1) node {-3};
可能类似于\path (m-1-1) node {\variable{1,1}};
\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}
答案1
列表语法略有不同,即
{-3, 4, 3, M},
{ 0, -2, 0, 0},
{-1, -1, -3, M},
{ 0, M, 0, 0}
您可以即时完成此操作。
代码
\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}
输出
答案2
这和我之前的回答很相似:阅读项目列表
您可以通过创建列表(使用包listofitems
)然后单独检索元素来使用类似向量的表达式。
该列表是使用 \readlist
命令创建的。
该列表以逗号分隔,并使用符号恢复元素
\ListName[<order of the element>]
对于此示例,两个一维列表就足够了,第一个用于左角值,第二个用于右角值:\varR
和\varL
,每个列表有 16 个元素。(有些是空的)
\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}]
{
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
& & & & & & & & & & & \\
};
% 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}