TikZ,如何将标签放在路径箭头上方

TikZ,如何将标签放在路径箭头上方

在下面的图片(使用 TikZ 库生成)中,我想将 AA 放在箭头上方,将 BB 放在箭头下方:

在此处输入图片描述

这是我目前的代码:

\begin{figure}
    \centering
    \begin{tikzpicture}[thick, node distance=9em and 15em, every node/.style={align=center}]
    
    % Nodes
    \node[block, drop shadow=black, fill=lightgray] (space) {Search Space};
    \node[block, drop shadow=black, fill=lightgray, right of=space, xshift=1cm] (strategy) {Search Strategy};
    \node[block, drop shadow=black, fill=lightgray, right of=strategy,xshift=3cm] (performance){Performance \\Estimation Strategy};
    

    % Arrows
    \path[-latex] (space) edge node[fill=white, anchor=center, inner sep=2pt] {a} (strategy);
    \path[-latex] (strategy) edge[bend left=0.5cm] node[fill=white, anchor=center, yshift=-0.1cm] {AA} (performance);
    
     \path[-latex] (performance) edge[bend left=0.5cm] node[fill=white, anchor=center, yshift=0.1cm] {BB} (strategy);

    \end{tikzpicture}
    \caption{Graph-based structure of the behaviours}
    \label{fig:nas-def}
\end{figure}

我找不到任何地方如何做到这一点,有什么想法吗?

答案1

如果要将节点放置在上方,只需放入above其声明即可。

上面和下面

\documentclass[tikz,border=3.14mm]{standalone}

\usetikzlibrary{shadows,shapes,positioning}
\tikzset{block/.style={draw,minimum height=1cm, drop shadow=black, fill=lightgray}}

\begin{document}
    \begin{tikzpicture}[thick, node distance=2cm]
    
    \node[block] (space) {Search Space};
    \node[block, right = of space] (strategy) {Search Strategy};
    \node[block, right = of strategy, text width=3.5cm,align=center] (performance){Performance \\ Estimation Strategy};
    
    \draw[-latex] (space) -- (strategy) node[fill=white,inner sep=2pt,midway] {a};
    \draw[-latex] ([yshift=5pt]strategy.east) to[bend left] node[above,midway] {AA} ([yshift=5pt]performance.west) ;
    \draw[-latex] ([yshift=-5pt]performance.west) to[bend left] node[below,midway] {BB} ([yshift=-5pt]strategy.east) ;

    \end{tikzpicture}
\end{document}

答案2

在检查了您的图表后,我发现还有一些我认为需要优化的地方。以下是我的看法:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shadows,shapes,positioning}
\begin{document}
    \begin{tikzpicture}[
        node distance=2cm,
        block/.style={rectangle, drop shadow=black, fill=lightgray,align=center},
        arrow/.style={thick,->,>=latex}]
    
    % Nodes
    \node[block] (space) {Search Space};
    \node[block, right =of space] (strategy) {Search Strategy};
    \node[block, right =of strategy, text width=3.5cm,align=center] (performance){Performance \\ Estimation Strategy};
    
    % Arrows
    \draw[arrow] (space) -- (strategy) node[anchor=south,inner sep=2pt,midway] {a};
    \draw[arrow] (strategy.5) -- (performance.170) node[anchor=south,midway,sloped] {AA};
    \draw[arrow] (performance.190) -- (strategy.355) node[anchor=north, midway,sloped] {BB} ;

    \end{tikzpicture}
\end{document}

结果是:

在此处输入图片描述

编辑:或者如果你坚持使用弯曲的箭头:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shadows,shapes,positioning}
\begin{document}
    \begin{tikzpicture}[
        node distance=2cm,
        block/.style={rectangle, drop shadow=black, fill=lightgray,align=center},
        arrow/.style={thick,->,>=latex}]
        
        % Nodes
        \node[block] (space) {Search Space};
        \node[block, right =of space] (strategy) {Search\\ Strategy};
        \node[block, right =of strategy,text width=3.5cm] (performance){Performance \\ Estimation Strategy};
        
        % Arrows
        \draw[arrow] (space) -- (strategy) node[anchor=south,inner sep=2pt,midway] {a};
        \draw[arrow] (strategy.north east) to[out=60,in=130] node[anchor=south,midway] {AA} (performance.north west);
        \draw[arrow] (performance.south west) to[out=240,in=300] node[anchor=north, midway] {BB} (strategy.south east);
    \end{tikzpicture}
\end{document}

看起来像这样: 在此处输入图片描述

相关内容