我有一個由幾行組成的圖形:
\begin{tikzpicture}
\shade[top color=black,bottom color=white] (-2,2) rectangle (2,1.75) node [above] {$n_2$};
\shade[top color=white, bottom color=black] (-2,0.75) rectangle (2,0.5);
\draw (-2,2)--(2,2);
\draw (-2,1.75)--(2,1.75);
\draw (-2,0.75)--(2,0.75);
\draw (-2,0.5)--(2,0.5);
\draw[blue, very thick, ->] (-3,2)--(-1.75,0.75) -- (-0.75,1.75)--(0.25,0.75)--(1.25,1.75)--(3.25,-0.25);
\draw[red, very thick, ->] (-3,1)--(-1.75,0.75)--(3.25,1.75);
\draw[green, very thick, ->] (-3,3)--(-1.75,0.75)--(-1.5,0.5)--(-0.5,0);
\node (-1.5,0.5) {$n_2$};
\node (10,10) {$n_1$};
\end{tikzpicture}
現在我想將名稱為 $n_1$ 的節點放在水平線的中間(即點 (0,1) 處)。但無論我在那裡輸入的值如何,節點都不會移動,它始終與名稱為 $n_2$ 的第二個節點保持在同一位置。為什麼?我怎樣才能移動它?
答案1
正如彼得所說,你應該添加at
來指定座標。但是,您不需要這些額外的節點。您可以將節點直接新增到上面的“欄”。您可以為任何路徑新增節點,矩形仍然是路徑。
輸出
程式碼
\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\shade[top color=black,bottom color=white] (-2,2) rectangle (2,1.75) node [midway,above, inner sep=6pt] {$n_2$};
\shade[top color=white, bottom color=black] (-2,0.75) rectangle (2,0.5) node [midway, below, inner sep=6pt] {$n_1$};
\draw (-2,2)--(2,2);
\draw (-2,1.75)--(2,1.75);
\draw (-2,0.75)--(2,0.75);
\draw (-2,0.5)--(2,0.5);
\draw[blue, very thick, ->] (-3,2)--(-1.75,0.75) -- (-0.75,1.75)--(0.25,0.75)--(1.25,1.75)--(3.25,-0.25);
\draw[red, very thick, ->] (-3,1)--(-1.75,0.75)--(3.25,1.75);
\draw[green, very thick, ->] (-3,3)--(-1.75,0.75)--(-1.5,0.5)--(-0.5,0);
\end{tikzpicture}
\end{document}