我在一个大型节点矩阵中使用了很多节点,我想将节点的创建与它们在矩阵中的放置分开。语法接近:
init (node1) ...;
init (node2) ...;
init (node3) ...;
init (node4) ...;
matrix[matrix of nodes]{
(node1) & (node2) \\
(node3) & (node4) \\
};
这在 TikZ 中存在吗?谢谢!
答案1
这里有一个在定义中嵌套代码的选项,您还可以根据预定义的大小和节点分离来放置图像,当然还有一些随机的鸭子。
结果:
梅威瑟:
\documentclass[tikz,border=3.14]{standalone}
\usepackage{tikzducks}
\usetikzlibrary{shapes.geometric,matrix}
\begin{document}
\begin{tikzpicture}[
%Global config
baseline=(current bounding box.center),
scale=0.25,
>=latex,
line width=1pt,
%Styles
Brace/.style={
decorate,
decoration={
brace,
raise=-7pt
}
},
Matrix/.style={
matrix of nodes,
text height=2.5ex,
text depth=0.75ex,
text width=3.25ex,
align=center,
left delimiter=\{,
right delimiter=\},
column sep=5pt,
row sep=5pt,
%nodes={draw=black!10}, % Uncoment to see the square nodes.
nodes in empty cells,
}
]
\def\nodA#1{\node[draw,circle,inner sep=0,fill=green!50!cyan]{#1};}
\def\nodB#1{\node[draw,rectangle,inner sep=0,fill opacity=0.2,fill=blue, text opacity=1]{#1};}
\def\nodC#1{\node[draw,shape=star,inner sep=0,fill=lime!70!blue]{#1};}
\def\nodD#1{\node[inner sep=0,label={[label distance=-5,scale=0.5]#1}]{\includegraphics[height=2.5ex]{example-image-a}};}
\def\nodE#1{\tikz\shuffleducks\duck[\randomhead,scale=0.3]\node{#1};}
\matrix[Matrix] at (0,0) (M){ % Matrix contents
\nodA{1}& \nodB{2} & \nodC{3} & \nodE{4} \\
\nodB{5}& \nodC{6} & \nodE{7} & \nodD{8} \\
\nodC{9}& \nodE{10} & \nodD{11} & \nodE{12} \\
\nodE{9}& \nodD{10} & \nodE{11} & \nodE{12} \\
};
\end{tikzpicture}
\end{document}