我有一个由几条线组成的图形:
\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
正如 Peter 所说,您应该添加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}