顯示 `\node` 和 `\coordinate` 實作的差異

顯示 `\node` 和 `\coordinate` 實作的差異

這裡有四種TikZ繪製兩條平行線段的環境。在所有環境中,第一條線段的一個端點由 或 指定\coordinate(A)\node(A){$A$};第二條線段的一個端點由\coordinate(B)或指定\node(B){$B$};

首先指定A點;沒有給出它的座標。TikZ預設放置在原點嗎?它似乎被放置在原點,因為在第二張圖中,我有命令\node at (0,0) {$O$};,並且「O」在 A 的座標處排版。

除了“A”的位置外,第二個和第三個顯示器是相同的。在第二個顯示中,我使用\coordinate(A);\node at (0,0) {$A$};來定位“A”,在第三個顯示中,我使用來\node[inner sep=0pt,outer sep=0pt] (A){$A$};定位“A”。在第二個顯示中,「A」似乎以 (0,0) 為中心,而在第三個顯示中,「A」似乎排版在 (0,0) 的左側。為什麼會有差異?

第三和第四個顯示器的編碼的唯一區別是,我inner sep=0pt在節點命令中有一個選項用於在第三個顯示器中排版“A”,而inner sep=1.5pt在節點命令中有一個選項用於在第四個顯示器中排版“A”。與第三個顯示器相比,為什麼第四個顯示器中的「一切」都會改變?

\documentclass{amsart}
\usepackage{tikz}\usetikzlibrary{calc,positioning}
\begin{document}

    \begin{tikzpicture}
    \coordinate(A);
    \coordinate[right=of A](B);
    \draw[yellow, line width=2pt] (A) -- ++(1,1);
    \draw[red] (B) -- ++(1,1);
    \end{tikzpicture}
    \vskip1.25mm

    \begin{tikzpicture}
    \coordinate(A);
    \coordinate[right=of A](B);
    \draw[yellow, line width=2pt] (A) -- ++(1,1);
    \draw[red] (B) -- ++(1,1);
    \node at (0,0) {$A$};
    \end{tikzpicture}
    \vskip1.25mm

    \begin{tikzpicture}
    \node[inner sep=0pt,outer sep=0pt] (A){$A$};
    \coordinate[right=of A](B);
    \draw[yellow, line width=2pt] (A) -- ++(1,1);
    \draw[red] (B) -- ++(1,1);
    \end{tikzpicture}
    \vskip1.25mm

    \begin{tikzpicture}
    \node[inner sep=1.5pt,outer sep=0pt] (A) {$A$};
    \coordinate[right=of A] (B);
    \draw[yellow, line width=2pt] (A) -- ++(1,1);
    \draw[red] (B) -- ++(1,1);
    \end{tikzpicture}

\end{document}

答案1

以下內容有幫助嗎?

\documentclass[tikz,border=5pt,mult]{standalone}
\usetikzlibrary{positioning}

\begin{document}

  \begin{tikzpicture}[every node/.style={draw}]
    \draw [help lines] (-1,-1) grid (2,2);
    \coordinate(A);
    \coordinate[right=of A](B);
    \draw [blue] (A) -- ++(1,1);
    \draw (B) -- ++(1,1);
    \node at (0,0) {$A$};
    \path [fill=blue] (0,0) circle (1.5pt);
  \end{tikzpicture}

  \begin{tikzpicture}[every node/.style=draw]
    \draw [help lines] (-1,-1) grid (2,2);
    \node[inner sep=0pt,outer sep=0pt] (A){$A$};
    \coordinate[right=of A](B);
    \draw [blue] (A) -- ++(1,1);
    \draw (B) -- ++(1,1);
    \path [fill=blue] (0,0) circle (1.5pt);
  \end{tikzpicture}

\end{document}

情況1

在第一種情況下,藍線是從座標 繪製的A,並B相對於該座標定位。由於座標是點,這相當於從 中繪製(0,0)

案例2

在第二種情況下,藍線從節點 繪製A,並B相對於節點定位。 TikZ 假設您想要從節點邊界上最近的點進行繪製或測量。即使 和為零A,它仍然具有正大小,因為它包含佔用空間的字母。因此,在本例中,該線是從 稍上方、稍右繪製的,並且相對於 稍右側的點進行定位。inner sepouter sepA(0,0)B(0,0)

您可以透過指定錨點來模擬使用座標的效果center

  \begin{tikzpicture}[every node/.style=draw]
    \draw [help lines] (-1,-1) grid (2,2);
    \node[inner sep=0pt,outer sep=0pt] (A){$A$};
    \coordinate[right=of A.center](B);
    \draw [blue] (A.center) -- ++(1,1);
    \draw (B) -- ++(1,1);
    \path [fill=blue] (0,0) circle (1.5pt);
  \end{tikzpicture}

使用錨點來模擬座標

或者,如果節點A沒有內容且為零sep,它的行為類似於座標,因為它基本上不佔空間:

  \begin{tikzpicture}[every node/.style=draw]
    \draw [help lines] (-1,-1) grid (2,2);
    \node[inner sep=0pt,outer sep=0pt] (A){};
    \coordinate[right=of A](B);
    \draw [blue] (A) -- ++(1,1);
    \draw (B) -- ++(1,1);
    \node at (A) {$A$};
    \path [fill=blue] (0,0) circle (1.5pt);
  \end{tikzpicture}

使用空節點來模擬座標

請注意,在每種情況下A, 都以原點(0,0)(由藍點標記)為中心。

相關內容