我想用以下代码在指定节点之间画线:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node [name=A, inner sep=0pt, outer sep=0pt] at (-1,1) {};
\node [name=B, inner sep=0pt, outer sep=0pt] at (1,1) {};
\node [name=C, inner sep=0pt, outer sep=0pt] at (1,-1) {};
\node [name=D, inner sep=0pt, outer sep=0pt] at (-1,-1) {};
\draw [thick] (A) -- (B);
\draw [thick] (B) -- (C);
\draw [thick] (C) -- (D);
\draw [thick] (D) -- (A);
\end{tikzpicture}
\end{document}
但即使我已将inner sep
和设置outer sep
为0pt
,角落处仍然有缝隙。我该怎么做,才能让角落处不再有缝隙,看起来像一个有一条连续线条的干净矩形?
上述代码编译后的图像如下所示:
答案1
首先,我假设您希望将节点保留为节点,而不是坐标,并希望在四个单独的路径中构建矩形。然后您可以添加linecap=rect
并使用center
锚点。
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[line cap=rect]
\node [name=A, inner sep=0pt, outer sep=0pt] at (-1,1) {};
\node [name=B, inner sep=0pt, outer sep=0pt] at (1,1) {};
\node [name=C, inner sep=0pt, outer sep=0pt] at (1,-1) {};
\node [name=D, inner sep=0pt, outer sep=0pt] at (-1,-1) {};
\draw [thick] (A.center) -- (B.center);
\draw [thick] (B.center) -- (C.center);
\draw [thick] (C.center) -- (D.center);
\draw [thick] (D.center) -- (A.center);
\end{tikzpicture}
\end{document}
这有效,但可能只是偶然,因为这是一个矩形。另一方面,如果你使用coordinate
“形状”并一次性绘制该物体,这也适用于以不同于 90 度或 180 度的角度相交的线。
\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}
\node [name=A, coordinate] at (-1,1) {};
\node [name=B, coordinate] at (1,1) {};
\node [name=C, coordinate] at (1,-1) {};
\node [name=D, coordinate] at (-1,-1) {};
\draw [thick] (A) -- (B) -- (C) -- (D) --cycle;
\end{tikzpicture}
\end{document}
这些代码的结果相当不引人注目。