每个人!
我正在绘制一些图表,遇到了一个问题。这是我的代码
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows.meta, positioning, quotes, shadows}
\usepackage{tikz}
\usetikzlibrary{fadings}
\usetikzlibrary{patterns}
\usetikzlibrary{shadows.blur}
\usetikzlibrary{shapes}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{calc,intersections}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,chains}
\usetikzlibrary[calc]
\begin{document}
\begin{tikzpicture}[auto,>=stealth,
node distance = 25mm and 10mm,
box/.style = {draw, rounded corners, fill=blue!6,
minimum width=22mm, minimum height=5mm, align=center},
squarednode/.style={rectangle, draw=black!0, fill=blue!1, very thick, minimum size=1mm},
> = {Straight Barb[angle=60:2pt 3]},
bend angle = 15,
auto = right,
]
\node (node1) [box,draw,name path=node1] {\large{Heat} \\ \large{market}};
\node (node2) [box,draw,name path=node2, above right=of node1] {\large{Combined Heat and Power Plants, }\\\large{Power-to-Heat units}};
\node (node3) [box, draw, name path=node3, below right=of node2] {\large{Electricity} \\ \large{market}};
\node (node4) [box, left =of node2] {};
\path [name path=node12] let \p1 = ($(node2)-(node1)$)
in (node1) ($(node1)!5pt!($(node1)+(-\y1,\x1)$)$) to +(\p1);
\path [name path=node21] let \p1 = ($(node2)-(node1)$)
in (node1) ($(node1)!-5pt!($(node1)+(-\y1,\x1)$)$) to +(\p1);
\path [name path=node23] let \p1 = ($(node3)-(node2)$)
in (node2) ($(node2)!5pt!($(node2)+(-\y1,\x1)$)$) to +(\p1);
\path [name path=node32] let \p1 = ($(node3)-(node2)$)
in (node2) ($(node2)!-5pt!($(node2)+(-\y1,\x1)$)$) to +(\p1);
\node [name intersections={of=node1 and node12}] (start) at (intersection-1){};
\draw [<-,very thick,name intersections={of=node2 and node12}]
(start.center) -- node[text width=3.5cm,sloped,swap,midway,above, align=center] {1. Heat offer} (intersection-1);
\node [name intersections={of=node2 and node21}] (start) at (intersection-1){};
\draw [<-,very thick,name intersections={of=node1 and node21}]
(start.center) -- node[text width=3cm,sloped, align=center] {2. Heat dispatch and price} (intersection-1);
\node [name intersections={of=node2 and node23}] (start) at (intersection-1){};
\draw [<-,very thick,name intersections={of=node3 and node23}]
(start.center) -- node[text width=3cm,sloped,swap,midway,above, align=center] {4. Electricity dispatch and price} (intersection-1);
\node [name intersections={of=node3 and node32}] (start) at (intersection-1){};
\draw [<-,very thick,name intersections={of=node2 and node32}]
(start.center) -- node[sloped] {3. Electricity offer} (intersection-1);
\draw [->] (node4) --node[text width=3cm, above, midway] {Electricity price forecast} (node2);
\end{tikzpicture}
\end{document}
首先,我绘制一些节点,然后计算距离来绘制平行线,如下所示https://tex.stackexchange.com/a/232828/262804。然后,我将文本绘制在线上方,如下所示 https://tex.stackexchange.com/a/249540/262804使用以下行
\draw [->] (node4) --node[text width=3cm, above, midway] {Electricity price forecast} (node2);
然后我得到了这个结果
我怎样才能使“电价预测”文本节点始终位于节点 4 和节点 2 之间并且永远不会跨越它们?
答案1
嗯,你的 MWE 非常复杂(我认为没有必要)(因此很容易丢失)
- 我的建议大多是题外话——可能的解决方案已经在@SebGlav 评论中提到——但似乎最好增加公共节点距离,例如
node distance = 19mm and 17mm
同时对边缘引用使用较小的字体大小。 - 在您的 MWE 序言中,您最多加载了三次包和库!为什么?一次就足够了。
- 请在 MWE 序言中仅提供与其相关的内容,就像我的回答中所做的那样。
transform canvas
通过使用图片中的内边缘和quotes
边缘标签库,可以降低 MWE 的复杂性并使其更短:
%\documentclass{article}
\documentclass[border=3.141592]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows.meta,
positioning,
quotes}
\begin{document}
\begin{tikzpicture}[
node distance = 19mm and 17mm,
box/.style = {draw, rounded corners, fill=blue!6,
minimum width=22mm, minimum height=11mm, align=center},
every edge/.style = {draw, -{Straight Barb[angle=60:2pt 3]}, semithick},
every edge quotes/.style = {auto, align=center,
font=\footnotesize\linespread{0.84}\selectfont,
sloped},
]
\node (n1) [box] {Combined Heat\\
and Power Plants,\\
Power-to-Heat units};
\node (n2) [box, left=of n1] {};
\node (n3) [box, below left=of n1] {Heat\\ market};
\node (n4) [box, below right=of n1] {Electricity\\ market};
%
\path (n2) edge["Electricity\\price\\forecast"] (n1)
(n1) edge["1. Heat offer"] (n3.north)
(n1) edge["3. Electricity\\offer"] (n4.north)
;
\path[transform canvas={xshift=+9pt}]
(n3.north) edge["2. Heat dispatch\\
and price" '] (n1);
\path[transform canvas={xshift=-9pt}]
(n4.north) edge["4. Electricity\\ dispatch and\\
price" '] (n1);
\end{tikzpicture}
\end{document}