![如何向箭头添加标签以避免与现有节点形状发生冲突](https://linux22.com/image/306233/%E5%A6%82%E4%BD%95%E5%90%91%E7%AE%AD%E5%A4%B4%E6%B7%BB%E5%8A%A0%E6%A0%87%E7%AD%BE%E4%BB%A5%E9%81%BF%E5%85%8D%E4%B8%8E%E7%8E%B0%E6%9C%89%E8%8A%82%E7%82%B9%E5%BD%A2%E7%8A%B6%E5%8F%91%E7%94%9F%E5%86%B2%E7%AA%81.png)
好的,我制作了下图:
我想在各个箭头(红点处)添加文本标签,并确保标签不与虚线、箭头线等重叠。因此,
- 有什么好方法可以为线条添加标签吗(希望与我编码线条的方式兼容)?
- 有什么好方法可以移动矩形以避免标签文本与矩形和箭头的线条之间发生冲突?
代码如下:
\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 A
fill=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}