如何向箭头添加标签以避免与现有节点形状发生冲突

如何向箭头添加标签以避免与现有节点形状发生冲突

好的,我制作了下图:

我想在各个箭头(红点处)添加文本标签,并确保标签不与虚线、箭头线等重叠。因此,

  • 有什么好方法可以为线条添加标签吗(希望与我编码线条的方式兼容)?
  • 有什么好方法可以移动矩形以避免标签文本与矩形和箭头的线条之间发生冲突?

代码如下:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit, arrows, calc, positioning}

\begin{document}

\tikzstyle{redRectangle} = [
    rectangle,
    draw,
    fill=red!20,
    node distance=0.65 cm,
    text width=7 em,
    text centered,
    rounded corners,
    minimum height=4 em,
    minimum width=3 cm,
    thick
]

\tikzstyle{blueRectangle} = [
    rectangle,
    draw,
    fill=blue!20,
    node distance=1.5 cm,
    text width=7 em,
    text centered,
    rounded corners,
    minimum height=4 em,
    minimum width=3 cm,
    thick
]

\tikzstyle{yellowRectangle} = [
    rectangle,
    draw,
    fill=yellow!20,
    node distance=1.5 cm,
    text width=7 em,
    text centered,
    rounded corners,
    minimum height=4 em,
    minimum width=3 cm,
    thick
]

\tikzstyle{greenRectangle} = [
    rectangle,
    draw,
    fill=green!20,
    node distance=1.5 cm,
    text width=7 em,
    text centered,
    rounded corners,
    minimum height=4 em,
    minimum width=3 cm,
    thick
]

\tikzstyle{blueDiamond} = [
    diamond,
    draw,
    fill=blue!20,
    node distance=1.5 cm,
    text width=7 em,
    text badly centered,
    inner sep=0pt,
    thick
]

\tikzstyle{blueEllipse} = [
    ellipse,
    draw,
    fill=blue!20,
    node distance=1.5 cm,
    text width=7 em,
    thick
]

\tikzstyle{container} = [
    rectangle,
    draw,
    inner sep=0.5 cm,
    dashed
]

\tikzstyle{empty} = [
]

\tikzstyle{line} = [
    draw,
    -latex',
    thick
]

\begin{tikzpicture}[auto]

    \node [empty](origin){};
    \node [redRectangle, right=of origin] (primaryxAODData) {primary xAOD data};
    \node [redRectangle, left=of origin] (primaryxAODMC) {primary xAOD MC};
    \node [blueRectangle, below=of origin](DxAOD0And1Lepton){DxAOD\\0 and 1 lepton};
    \node [blueRectangle, left=of DxAOD0And1Lepton] (DxAODMC) {DxAOD\\MC (also called TOPQ1)};
    \node [blueRectangle, right=of DxAOD0And1Lepton](DxAOD2Leptons){DxAOD\\2 leptons};
    \node [yellowRectangle, below=of DxAOD0And1Lepton](AnalysisTopPackage){AnalysisTop package\\TTHbbLeptonic};
    \node [blueRectangle, below=of AnalysisTopPackage](Mini-xAODorflatn-tuple){Mini-xAOD or flat n-tuple};
    \node [greenRectangle, below=of Mini-xAODorflatn-tuple](plots){plots};

    \node [container, fit=(primaryxAODData)(origin)(primaryxAODMC)](container1){};
    \node [container, fit=(AnalysisTopPackage)](container2){};
    \node [container, fit=(plots)](container3){};

    \path [line] (primaryxAODMC) -- (DxAODMC);
    \path [line] (primaryxAODData) -- (DxAOD0And1Lepton);
    \path [line] (primaryxAODData) -- (DxAOD2Leptons);

    \path [line] (DxAODMC) -- (AnalysisTopPackage);
    \path [line] (DxAOD0And1Lepton) -- (AnalysisTopPackage);
    \path [line] (DxAOD2Leptons) -- (AnalysisTopPackage);

    \path [line] (AnalysisTopPackage) -- (Mini-xAODorflatn-tuple);
    \path [line] (Mini-xAODorflatn-tuple) -- (plots);

    \node at (container1.north)[above]{production system};
    \node at (container2.south east)[right]{user grid or local};
    \node at (container3.south east)[right]{user grid or local};

\end{tikzpicture}

\end{document}

答案1

下面的这段代码展示了一些示例:

\path [line] (primaryxAODMC) -- (DxAODMC) node[fill=white, pos=0.7, right=2mm]{label A};

primaryxAODMC.center在和之间画一条线,并在此路径 0.7 位置(1 为总路径长度)的右侧DxAODMC.center添加一个带有文本的节点。该节点的背景可以覆盖任何先前的路径。label Afill=white

fitting如果您调整节点,则可以更改节点inner sep,例如:

\node [container, fit=(plots), label={[anchor=west]south east:user grid or local}, inner sep=7mm](container3){};

label您可以使用节点选项,而不是使用新节点来为虚线节点添加标签:

    \node [container, fit=(primaryxAODData)(origin)(primaryxAODMC), label=above:production system](container1){};

而不是redRectangle,,blueRectangle...您可以定义一个Rectangle带有参数的,稍后该参数将定义其颜色。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit, arrows, calc, positioning}

\begin{document}

\tikzstyle{Rectangle} = [
    rectangle,
    draw,
    fill=#1!20,
    node distance=1.5 cm,
    text width=7 em,
    text centered,
    rounded corners,
    minimum height=4 em,
    minimum width=3 cm,
    thick
]

\tikzstyle{blueDiamond} = [
    diamond,
    draw,
    fill=blue!20,
    node distance=1.5 cm,
    text width=7 em,
    text badly centered,
    inner sep=0pt,
    thick
]

\tikzstyle{blueEllipse} = [
    ellipse,
    draw,
    fill=blue!20,
    node distance=1.5 cm,
    text width=7 em,
    thick
]

\tikzstyle{container} = [
    rectangle,
    draw,
    inner sep=0.2 cm,
    dashed
]

\tikzstyle{empty} = [
]

\tikzstyle{line} = [
    draw,
    -latex',
    thick
]

\begin{tikzpicture}[auto]

    \node [empty](origin){};
    \node [Rectangle=red, right=of origin] (primaryxAODData) {primary xAOD data};
    \node [Rectangle=red, left=of origin] (primaryxAODMC) {primary xAOD MC};
    \node [Rectangle=blue, below=of origin](DxAOD0And1Lepton){DxAOD\\0 and 1 lepton};
    \node [Rectangle=blue, left=of DxAOD0And1Lepton] (DxAODMC) {DxAOD\\MC (also called TOPQ1)};
    \node [Rectangle=blue, right=of DxAOD0And1Lepton](DxAOD2Leptons){DxAOD\\2 leptons};
    \node [Rectangle=yellow, below=of DxAOD0And1Lepton](AnalysisTopPackage){AnalysisTop package\\TTHbbLeptonic};
    \node [Rectangle=blue, below=of AnalysisTopPackage](Mini-xAODorflatn-tuple){Mini-xAOD or flat n-tuple};
    \node [Rectangle=green, below=of Mini-xAODorflatn-tuple](plots){plots};

    \node [container, fit=(primaryxAODData)(origin)(primaryxAODMC), label=above:production system](container1){};
    \node [container, fit=(AnalysisTopPackage), label={[anchor=west]south east:user grid or local}](container2){};
    \node [container, fit=(plots), label={[anchor=west]south east:user grid or local}, inner sep=7mm](container3){};

    \path [line] (primaryxAODMC) -- (DxAODMC) node[fill=white, pos=0.7, right=2mm]{label A};
    \path [line] (primaryxAODData) -- (DxAOD0And1Lepton) node[midway, left]{label B};
    \path [line] (primaryxAODData) -- (DxAOD2Leptons)node[anchor=center,fill=white, midway]{label C};

    \path [line] (DxAODMC) -- (AnalysisTopPackage);
    \path [line] (DxAOD0And1Lepton) -- (AnalysisTopPackage);
    \path [line] (DxAOD2Leptons) -- (AnalysisTopPackage);

    \path [line] (AnalysisTopPackage) -- (Mini-xAODorflatn-tuple);
    \path [line] (Mini-xAODorflatn-tuple) -- (plots);

%    \node at (container1.north)[above]{production system};
%    \node at (container2.south east)[right]{user grid or local};
%    \node at (container3.south east)[right]{user grid or local};

\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容