解释

解释

我正在使用 TikZ 的链库。
我想在箭头上方添加一个标签(节点?)。可以吗?

我发现,但这太不靠谱了。
有没有标准化的方法?

答案1

您链接到的页面并不黑客。它尽可能简单和干净。

\chainin (m-2-3) [join={node[above] {$x$}}];

还有什么比这更直接的呢?

答案2

我还需要 TikZ 链的标记边,但需要保留原始语法的特征join=with <node> by <options>。因此,我编写了一些代码来接受该语法join=with <node> by <options> nodes <node path>。以下是该代码以及一个示例:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{chains}

\makeatletter

\def\tikz@lib@parse@join#1{%
  \def\tikz@temp{#1}%
  \ifx\tikz@temp\pgfutil@empty%
    \tikz@lib@join@by@nodes by {} nodes \pgfutil@stop%
  \else%
    \pgfutil@in@{nodes }{#1}%
    \ifpgfutil@in@%
      \tikz@lib@parse@join@nodes#1\pgfutil@stop%
    \else%
      \tikz@lib@parse@join@nodes#1 nodes \pgfutil@stop%
    \fi%
  \fi%
}

\def\tikz@lib@parse@join@nodes#1nodes #2\pgfutil@stop{%
  \pgfutil@in@{by }{#1}%
  \ifpgfutil@in@%
    \tikz@lib@parse@join@by#1nodes #2\pgfutil@stop%
  \else%
    \tikz@lib@parse@join@by#1by {} nodes #2\pgfutil@stop%
  \fi%
}

\def\tikz@lib@parse@join@by#1by #2 nodes #3\pgfutil@stop{%
  \pgfutil@in@{with }{#1}%
  \ifpgfutil@in@%
    \tikz@lib@join@with@by@nodes#1by #2 nodes #3\pgfutil@stop%
  \else%
    \tikz@lib@join@by@nodes by #2 nodes #3\pgfutil@stop%
  \fi%
}

\def\tikz@lib@join@with@by@nodes with #1 by #2 nodes #3\pgfutil@stop{%
  \tikzset{after node path={(#1)edge[every join,#2]#3(\tikzchaincurrent)}}%
}

\def\tikz@lib@join@by@nodes by #1 nodes #2\pgfutil@stop{%
  \tikzset{after node path={%
             \ifx\tikzchainprevious\pgfutil@empty%
             \else%
               (\tikzchainprevious)edge[every join,#1]#2(\tikzchaincurrent)%
             \fi}}%
}

\makeatother

\begin{document}
\begin{tikzpicture}[start chain, every join/.append style={->}]
  \node [on chain, join] {A};

  \node [on chain, join] {B};

  \node [on chain, join=nodes {node [above] {$\gamma$}}] {C};

  \node [on chain=going below,
         join=by {red}  nodes {node [right] {$\delta$}}] {D};

  \node [continue chain=going left, on chain,
         join,
         join=with chain-begin by {<-, blue}
              nodes {node [sloped,above] {$\epsilon$}},
         join=with chain-2 by {|->, red}] {E};

  \node [on chain, join=by {<->,green},
                   join=with chain-begin] {F};
\end{tikzpicture}
\end{document}

在此处输入图片描述

解释

\tikz@lib@parse@join

  1. \tikz@lib@parse@join首先检查参数是否为join空。如果是,则连接以空bynodes组件进行,并且 with成分。

  2. 否则,我们检查是否有nodes组件。如果没有,我们将一个空nodes组件附加到;和#1之间插入的空格是必要的,因为(是#1nodes#1不是为空且)不以空格结尾。然后我们调用\tikz@lib@parse@join@nodes

\tikz@lib@parse@join@nodes

现在,得益于前面的宏,nodes可以保证存在一个(可能为空的)组件。接下来,我们检查是否存在组件by。如果没有,则插入一个空by组件。然后我们调用\tikz@lib@parse@join@by

by请注意,如果必须插入一个空的不是#1在和之间添加一个空格by。这是因为:

  • 只有一个nodes组件,在这种情况下#1是空的;或者
  • with除了组件之外还有一个组件nodes,在这种情况下#1包含with并且其本身以空格结尾。

\tikz@lib@parse@join@by

现在,得益于前面的宏, (可能为空)bynodes组件保证存在。接下来我们检查是否存在组件with。如果有,则调用\tikz@lib@join@with@by@nodes。否则,我们调用\tikz@lib@join@by@nodes

答案3

edge node=node {node content}是一个解决方案。下面是我用来绘制状态机的模板:

\usetikzlibrary {automata, chains}
\begin{tikzpicture}[
    shorten >=1pt,
    state/.style={state with output, on chain},
    start chain,
    on grid,
    node distance=2cm,
    auto,
]
\node[state] {a};
\node[
    state,
    join=by {
        edge node=node {1},
        ->,
        bend left
    },
] {b \nodepart{lower} $OK$};
\end{tikzpicture}

或者更奇特的quotes语法:

\usetikzlibrary {automata, chains, quotes}
\begin{tikzpicture}[
    shorten >=1pt,
    state/.style={state with output, on chain},
    start chain,
    on grid,
    node distance=2cm,
    auto,
]
\node[state] {a};
\node[
    state,
    join=by {
        "1",
        ->,
        bend left
    },
] {b \nodepart{lower} $OK$};
\end{tikzpicture}

相关内容