如何绘制两个矩形(或一般两个形状)使它们相互接触。以下是最小工作示例。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning}
\begin{document}
\begin{tikzpicture}
\node[rectangle,draw,minimum width=2in, minimum height=1.00in, fill=cyan] (a) {}
node[rectangle,draw,fill=magenta] (b) [left=of a] {};
\end{tikzpicture}
\end{document}
答案1
您可以使用left= 0pt of a
:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning}
\begin{document}
\begin{tikzpicture}
\node[rectangle,draw,minimum width=2in, minimum height=1.00in, fill=cyan] (a) {}
node[rectangle,draw,fill=magenta] (b) [left= 0pt of a] {};
\end{tikzpicture}
\end{document}
或者left= -\pgflinewidth of a
如果你想要边缘重叠:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning}
\begin{document}
\begin{tikzpicture}
\node[rectangle,draw,minimum width=2in, minimum height=1.00in, fill=cyan] (a) {}
node[rectangle,draw,fill=magenta] (b) [left= -\pgflinewidth of a] {};
\end{tikzpicture}
\end{document}
作为 奎伯比尔贝尔在评论中提到,使边缘重叠的另一种方法是将outer sep
(或outer xsep
,outer ysep
)设置为零,从而将锚点置于边缘线的中间。
如果这应该适用于所有节点,那么您最好使用node distance
键 for tikzpicture
(或 a scope
);例如:
\begin{tikzpicture}[node distance=0pt]
...
\end{tikzpicture}
另一个选择是使用锚点:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning}
\begin{document}
\begin{tikzpicture}
\node[rectangle,draw,minimum width=2in, minimum height=1.00in, fill=cyan] (a) {}
node[rectangle,draw,fill=magenta,anchor=east] (b) at (a.west) {};
\node[rectangle,draw,fill=green,anchor=south] at (a.north) {};
\end{tikzpicture}
\end{document}