我正在尝试绘制框图。我想使边缘(红色)正交,就像用 制作的黑色路径一样-|
。我知道有命令\tikztostart
和\tikztotarget
运算....!0.5!...
符,但无法让它们在 内工作every edge/.style{}
。这是我的代码(不知道如何添加编译版本)。
\begin{tikzpicture}[line width=1pt,-{Stealth[scale=3,length=3,width=2]},on grid,node distance=2cm,every node/.style={font=\huge},every edge/.style={draw,red,rounded corners}]
\node[draw,rectangle] (B) {$B$};
\node[draw,circle] (S) [right=of B] {$+$};
\node[draw,rectangle] (I) [right=of S] {$\dfrac{1}{s}$};
\node[draw,rectangle] (C) [right=of I] {$C$};
\node[draw,rectangle] (A) [below=of I] {$A$};
\node[draw,rectangle] (D) [above=of I] {$D$};
\draw (B) edge [auto=left] node {} (S)
(S) edge [auto=right] node {} (I)
(A) edge [auto] node {$A$} (S);
\draw (A) -| node {$A$} (S);
\end{tikzpicture}
答案1
像这样吗?
代码:
\documentclass[border=3mm,
tikz,
preview
]{standalone}
\usetikzlibrary{arrows.meta,chains,positioning}
\usepackage{amsmath}
\begin{document}
%---------------------------------------------------------------%
\begin{tikzpicture}[
line width = 1pt,
on grid,
start chain = going right,
node distance = 2cm,
box/.style = {draw, rectangle, font=\huge, on chain},
L/.style = {draw, red, -{Stealth[scale=3,length=3,width=2]}},
T/.style = {draw, red, rounded corners,
to path={-| (\tikztotarget)},
-{Stealth[scale=3,length=3,width=2]}}
]
\node[box] (B) {$B$};
\node[draw,circle,
on chain] (S) {$+$};
\node[box] (I) {$\frac{1}{s}$};
\node[box] (C) {$C$};
\node[box,below=of I] (A) {$A$};
\node[box,above=of I] (D) {$D$};
\draw%[]
(B) edge[L] (S)
(S) edge[L] (I)
(I) edge[L] (C)
(D) edge[T] (S)
(A) edge[T] (S)
;
\end{tikzpicture}
%---------------------------------------------------------------%
\end{document}
正交边的思想来源于这里。
编辑:
上面的代码中输入错误:positionings
应该是 而不是positioning
。我现在改正了。
升级:
如果您希望标记边,则可以仅对类型的边进行简单的标记L
,例如:
\draw (B) edge[L] node {b} (S)
(S) edge[L] (I);
然而,对于类型的边T
,边的定义应该更改如下:
...
T/.style args = {#1/#2}{draw, red, rounded corners,
to path={-| node[pos=#1] {#2}
(\tikztotarget)},
-{Stealth[scale=3,length=3,width=2]}},
T/.default = / ]
和边宽度节点是:
\draw (D) edge[T=0.75/a] (S)
(A) edge[T=0.25/a] (S);
或者当边没有节点时:
\draw (D) edge[T] (S)
(A) edge[T=0.25/a] (S);
另一个细节:由于边被定义为红色,因此如果您希望使用黑色,则需要text=black
在边节点选项中添加选项。带有边标签的完整示例:
\documentclass[border=3mm,
tikz,
preview
]{standalone}
\usetikzlibrary{arrows.meta,chains,positioning}
\usepackage{amsmath}
\begin{document}
%---------------------------------------------------------------%
\begin{tikzpicture}[
line width = 1pt,
auto,
start chain = going right,
node distance = 2cm,
box/.style = {draw, rectangle, font=\huge, on chain},
L/.style = {draw, red, rounded corners,
-{Stealth[scale=3,length=3,width=2]}},
]
\node[box] (B) {$B$};
\node[draw,circle,
on chain] (S) {$+$};
\node[box] (I) {$\frac{1}{s}$};
\node[box] (C) {$C$};
\node[box,below=of I] (A) {$A$};
\node[box,above=of I] (D) {$D$};
\draw (B) edge[L] node {b} (S)
(S) edge[L] (I)
(I) edge[L] (C)
(D) edge[T=0.75/$d$] (S)
(A) edge[T=0.25/a] (S)
;
\end{tikzpicture}
%---------------------------------------------------------------%
\end{document}
除了上述解决方案外,还有更简单的解决方案:
\documentclass[border=3mm,
tikz,
preview
]{standalone}
\usetikzlibrary{arrows.meta,chains,positioning}
\usepackage{amsmath}
\begin{document}
%---------------------------------------------------------------%
\begin{tikzpicture}[
line width = 1pt,
auto,
start chain = going right,
node distance = 2cm,
box/.style = {draw, rectangle, font=\huge, on chain},
L/.style = {draw, red, rounded corners,
-{Stealth[scale=3,length=3,width=2]}},
T/.style args = {#1/#2}{draw, red, rounded corners,
to path={-| node[pos=#1,text=black] {#2}
(\tikztotarget)},
-{Stealth[scale=3,length=3,width=2]}},
T/.default = / ]
\node[box] (B) {$B$};
\node[draw,circle,
on chain] (S) {$+$};
\node[box] (I) {$\frac{1}{s}$};
\node[box] (C) {$C$};
\node[box,below=of I] (A) {$A$};
\node[box,above=of I] (D) {$D$};
\draw[L] (B) edge node {b} (S)
(S) edge (I)
(I) edge (C);
\draw[L] (D) -| node[pos=0.75] {$d$} (S);
\draw[L] (A) -| node[pos=0.25] {a} (S);
\end{tikzpicture}
两者的结果相同:
第二种更简单的解决方案是使用正交路径代替复杂的边,分别绘制节点之间的每条正交路径,即您需要为每条路径写入\draw[L] ...
第一种情况下不需要的内容。这两种解决方案各有利弊。哪种更合适?我留给用户 :-)。