两个节点之间的标签

两个节点之间的标签

基本上,我有下面的代码,它绘制了我的节点,我只想将标签 delta x 放在节点 1 和 2 之间和上方,将 delta y 放在节点 1 和 5 之间和左侧。我该怎么做?

\begin{tikzpicture}[scale=1, transform shape]
    \tikzstyle{every node} = [circle, fill=black!30]


\node [label={ $\Delta x $}] (a)  {1};
\node(b)             [right=of a] {2};
\node(c)             [right=of b] {3};
\node(d)             [right=of c] {4};
\node [label={ $\Delta y $}] (e) [below= of a] {5};
\node(f)             [right=of e] {6};
\node(g)             [right =of f] {7};
\node(h)             [right= of g] {8};


\foreach \from/\to in {a/b, a/e}
\draw [-] (\from) -- (\to);
\end{tikzpicture}

答案1

您指的是下面这样的东西吗?

\documentclass[border=5pt]{standalone}
\usepackage{tikz}
    % load library to position nodes relatively to each other
    \usetikzlibrary{
        positioning,
    }
\begin{document}
    \begin{tikzpicture}
        % use a scope to state the style of the (main) nodes
        \begin{scope}[
            every node/.append style={
                circle,
                fill=black!30,
            },
        ]
            \node (a)              {1};
            \node (b) [right=of a] {2};
            \node (c) [right=of b] {3};
            \node (d) [right=of c] {4};
            \node (e) [below=of a] {5};
            \node (f) [right=of e] {6};
            \node (g) [right=of f] {7};
            \node (h) [right=of g] {8};
        \end{scope}

        % then you can draw the lines between the nodes you want to
        % connect and place nodes on that path
        % (because this nodes are outside of the scope,
        %  they are drawn with the default style)
        \draw  (a) -- node [above] {$\Delta x$} (b);
        \draw  (a) -- node [left]  {$\Delta y$} (e);
    \end{tikzpicture}
\end{document}

该图显示了上述代码的结果

相关内容