在下面的示例中,我希望矩形的垂直线对齐,但由于某种原因它们没有对齐。我认为这是因为没有使用节点的实际坐标,但它们包含某种偏移量。我还尝试对矩形命令中的每个节点使用
.base
,但它没有解决该问题。
如何访问我在节点中定义的实际坐标?
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\node (d) at (0, 7) [anchor=west] {$D_i$};
\node (c) at (0, 5) [anchor=west] {$C^j$};
\node (b) at (0, 3) [anchor=west] {$B_k$};
\node (a) at (0, 1) [anchor=west] {$A$};
\draw ($(a)+(-1, -1)$) rectangle ($(b)+(1,1)$);
\draw ($(c)+(-1, -1)$) rectangle ($(d)+(1,1)$);
\end{tikzpicture}
\end{document}
更新:我意识到对齐问题可以通过首先定义坐标来解决,如下面的代码所示,但我仍然有兴趣了解如何从节点中提取实际坐标。
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (d) at (0, 7);
\coordinate (c) at (0, 5);
\coordinate (b) at (0, 3);
\coordinate (a) at (0, 1);
\node at (d) [anchor=west] {$D_i$};
\node at (c) [anchor=west] {$C^j$};
\node at (b) [anchor=west] {$B_k$};
\node at (a) [anchor=west] {$A$};
\draw ($(a)+(-1, -1)$) rectangle ($(b)+(1,1)$);
\draw ($(c)+(-1, -1)$) rectangle ($(d)+(1,1)$);
\end{tikzpicture}
\end{document}
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[every node/.style={minimum width=2cm,minimum height=2cm}]
\node (d) at (0, 7) {$D_i$};
\node[below of=d] (c) {$C^j$};
\node[below of=c,anchor=north] (b) {$B_k$};
\node[below of=b] (a) {$A$};
\draw (a.south west) rectangle (b.north east);
\draw (c.south west) rectangle (d.north east);
\end{tikzpicture}
\end{document}