# TikZ Macro: Problem with relative positioning

\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
\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 (third) [below=\offset of first] {\TwoQuad{Third}};
\end{tikzpicture}

\end{document}


Hi, I'm having an issue with the relative positioning of tikz nodes together with a macro, where some other nodes are created. The red line and the heading are changing their relative positions when using the below = ... of or similar syntax. I don't know why the result is not everywhere the same. Any ideas how to fix this?

P.s. The result should look everywhere like "First" (Red line and Headline centered). "Second" should be on the same height at the right, "Third" below "First" and "Fourth" below "Second".

Thanks

Nico

• It is not recommended to nest tikzpictures. – marmot Feb 7 '18 at 17:03
• context of your approach is not known, so it is difficult give you an advice. what is purposes of nested nodes? is not possible achieve your goal without nesting? – Zarko Feb 7 '18 at 17:20

This is a more Tikzy way of doing this. (It is not recommended to nest 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}


• This is a clever solution that does what I want. Thank you! – Nico Feb 8 '18 at 12:16