我尝试使用\newcommand
内部tikz
和matrix
环境。请参阅以下代码:
\documentclass[border={0pt 0pt 0pt 0pt}]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc, positioning, shapes, shapes.geometric, shapes.arrows, }
\usepackage{array}
\newcommand{\Block}[3]{
\ifnum #1=1
{ \begin{tikzpicture}[auto, ]
\node[draw, fill=gray, rectangle, thick, text centered, rounded corners =3pt, minimum height=1em, align=center, text=black, minimum width=6em, ] (#2) {#3};
\end{tikzpicture}
}
%\else
% { \begin{tikzpicture}[auto, ]
% \node[fill=gray, rectangle, thick, text centered, rounded corners =3pt, minimum height=1em, align=center, text=black, minimum width=6em, ] (#2) {#3};
% \end{tikzpicture}
% }
\fi
}
\begin{document}
\begin{tikzpicture} [
auto,
block1/.style = { rectangle, , thick, draw,
text width=15em, text centered,
rounded corners, minimum height=1em },
]
%%%% WORKS
\matrix [column sep=5.mm, row sep=12mm] {
\node [block1] (p00) {00}; & \node [block1] (p01) {01}; & \node [block1] (p02) {02}; \\
\node [block1] (p10) {10}; & \node [block1] (p12) {12}; & \node [block1] (p12) {12}; \\
};
\\
%%%%%%% Does not work
% \matrix [column sep=5.mm, row sep=12mm] {
% \Block {1}{p00}{00}; & \Block {1}{p01}{01}; & \Block {1}{p02}{02}; \\
% \Block {1}{p11}{11}; & \Block {1}{p11}{11}; & \Block {1}{p12}{12}; \\
% };
\end{tikzpicture}
\end{document}
第一个matrix
按预期工作(见图),而第二个则不工作(定位混乱)。我的问题是,我应该如何修改它\newcommand
以使其工作(以与前者类似的方式)?
答案1
虽然我不知道你发布的示例是否是最终想要的结果,但在这里你可以通过更自动化的方式使用\pgfmatrixcurrentrow
和来输入类似的矩阵\pgfmatrixcurrentcolumn
。当然它也使用了matrix of nodes
asTorbjørn T. 建议。
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{
Block1/.style = {%
rectangle,
draw,
fill = gray,
thick,
align = center,
rounded corners = 3pt,
minimum height = 1em,
minimum width = 6em,
text = black,
alias = p\the\numexpr\pgfmatrixcurrentrow-1\relax\the\numexpr\pgfmatrixcurrentcolumn-1\relax,
node contents = \the\numexpr\pgfmatrixcurrentrow-1\relax\the\numexpr\pgfmatrixcurrentcolumn-1\relax,
},
}
\begin{document}
\begin{tikzpicture}
\matrix (A) [column sep=5mm, row sep=12mm, matrix of nodes,
nodes in empty cells, nodes=Block1,
row 2/.style={nodes = {Block1, fill=red!30}}]
{
&&\\
&&\\
};
\draw (p00) |- ([shift={(3mm,3mm)}]p02.north east) |- (p12);
\end{tikzpicture}
\end{document}
答案2
tikzpicture
您的问题是宏中有一个环境\Block
。嵌套tikzpicture
s 通常不是一个好主意,例如当一个 tikzpicture 位于另一个 tikzpicture 内部时,出现覆盖问题
如果从宏定义中删除\begin{tikzpicture}[auto]
和,它可以正常工作。\end{tikzpicture}
顺便说一句,也许您可能会对matrix
图书馆和matrix of nodes
选项感兴趣。
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, matrix}
\newcommand{\Block}[3]{
\ifnum #1=1
{
% \begin{tikzpicture}[auto, ]
\node[draw, fill=gray, rectangle, thick, text centered, rounded corners =3pt, minimum height=1em, align=center, text=black, minimum width=6em, ] (#2) {#3};
% \end{tikzpicture}
}
%\else
% { \begin{tikzpicture}[auto, ]
% \node[fill=gray, rectangle, thick, text centered, rounded corners =3pt, minimum height=1em, align=center, text=black, minimum width=6em, ] (#2) {#3};
% \end{tikzpicture}
% }
\fi
}
\begin{document}
\begin{tikzpicture} [
auto,
block1/.style = { rectangle, , thick, draw,
text width=15em, text centered,
rounded corners, minimum height=1em },
]
%%%% WORKS
\matrix [column sep=5.mm, row sep=12mm] (m1) {
\node [block1] (p00) {00}; & \node [block1] (p01) {01}; & \node [block1] (p02) {02}; \\
\node [block1] (p10) {10}; & \node [block1] (p12) {12}; & \node [block1] (p12) {12}; \\
};
\\
%%%%%%% Works
\matrix [column sep=5.mm, row sep=12mm,below=of m1] (m2) {
\Block {1}{p00}{00}; & \Block {1}{p01}{01}; & \Block {1}{p02}{02}; \\
\Block {1}{p11}{11}; & \Block {1}{p11}{11}; & \Block {1}{p12}{12}; \\
};
\matrix [matrix of nodes,nodes={block1},below=of m2,column sep=5.mm, row sep=12mm] (m3) {
00 & 01 & 02 \\
10 & 11 & 12 \\
};
\end{tikzpicture}
\end{document}