TikZ Macro:相对定位问题

TikZ Macro:相对定位问题
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, calc}

\begin{document}
%define length of square
\def\length{5}

%define offset of squares
\def\offset{10mm}

%define square-layout
\newcommand{\TwoQuad}[1]{
\begin{tikzpicture}
\node (rectangle) at (0,0) {
\tikz \draw (0,0) rectangle (\length,\length);
};
\node (line) at (rectangle.center) {\tikz \draw[red] (0,-\length/2) -- (0,\length/2);};
\node (text) at (rectangle.north) {#1};
\end{tikzpicture}
}

\begin{tikzpicture} 
    \node (first) at (0,0) {\TwoQuad{First}};
    \node (second) [right=\offset of first]{\TwoQuad{Second}};
    \node (third) [below=\offset of first] {\TwoQuad{Third}};
    \node (fourth) [right=\offset of third]{\TwoQuad{Fourth}};
\end{tikzpicture}

\end{document}

结果

嗨,我在 tikz 节点与宏的相对定位方面遇到了问题,其中创建了一些其他节点。使用below = ... of或类似语法时,红线和标题会改变它们的相对位置。我不知道为什么结果不是到处都一样。有什么想法可以解决这个问题吗?

Ps 结果应该到处都像“第一”(红线和标题居中)。“第二”应该在右侧的同一高度,“第三”在“第一”下方,“第四”在“第二”下方。

谢谢

尼科

答案1

这是一个更 Tizy 方式执行此操作。(不建议嵌套 tikzpictures。)

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, calc}

\begin{document}
%define length of square
\def\length{5cm}

%define offset of squares
\def\offset{10mm}
\tikzset{BoxNode/.style={draw,minimum width=\length,minimum height=\length,
append after command={% courtesy of Alenanno ref: https://tex.stackexchange.com/questions/287967/drawing-thin-line-around-a-multipart-tikz-shape#comment696552_287972
      \pgfextra{\draw[red] (\tikzlastnode.north) -- (\tikzlastnode.south);}
}}}
\begin{tikzpicture} 
    \node[BoxNode,label=above:First] (first) at (0,0) {};
    \node[BoxNode,label=above:Second] (second) [right=\offset of first]{};
    \node[BoxNode,label=above:Third] (third) [below=\offset of first] {};
    \node[BoxNode,label=above:Fourth] (fourth) [right=\offset of third]{};
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容