如何在节点和路径中间之间画一条线(使用双向箭头)?
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usepackage{tikzscale}
\usetikzlibrary{arrows,automata,shadows}
\begin{document}
\centering
\tikzstyle{vertex_basic}=[rectangle,thick,top color=white,bottom color=black!20, minimum size=50pt,scale=1]
\tikzstyle{vertex}=[vertex_basic, draw=black!50]
\tikzset{initial text={}}
\begin{tikzpicture}[,>=stealth,ultra thick,black!50,text=black, scale=1.8,graphs/every graph/.style={edges=rounded corners},every new ->/.style={shorten >=1pt},auto]
\matrix[row sep=20mm,column sep=2mm] {
\node (X) [vertex,align=center] {X}; & & \node (Y) [vertex,align=center] {Y}; \\
& \node (O) [vertex,align=center] {Outcome}; &\\
};
\path[<->] (X) edge node[align=center] {} (O);
\path[<->] (Y) edge[densely dotted] node[align=left] {} (O);
\path[<->] (X) edge[densely dotted] node[align=left] {} (Y);
\end{tikzpicture}
\end{document}
如何在红线位置使用其他线条的样式添加一条线条?谢谢!
答案1
O
您已在箭头中间放置了一个空节点X
,为其命名,然后可以绘制一条线到该节点。但如果您不使用该节点做任何事情,请将其替换coordinate
为
\path[<->] (X) edge coordinate (m) (O);
然后你可以用
\draw [<->] (m) -- (Y);
在下面的代码中我还建议进行一些其他更改。
\tikzstyle
被认为已被弃用,因此我将样式定义移到了 中\tikzset
,参见应该\tikzset
或被\tikzstyle
用来定义 TikZ 样式吗?
我在矩阵中添加了matrix of nodes
需要matrix
库的选项,并为矩阵中的所有节点nodes={vertex}
设置样式。vertex
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix} % <-- added
\tikzset{
vertex_basic/.style={
rectangle,thick,
top color=white,
bottom color=black!20,
minimum size=50pt},
vertex/.style={vertex_basic, draw=black!50, align=center},
}
\begin{document}
\begin{tikzpicture}[
>=stealth,
ultra thick,
black!50,
text=black,
scale=1.8
]
\matrix[
row sep=20mm,column sep=2mm,
matrix of nodes,nodes={vertex}
]
{
|[alias=X]| X & & |[alias=Y]| Y \\
& |[alias=O]| Outcome \\
};
\draw[<->] (X) --coordinate(m) (O);
\draw[<->, densely dotted] (Y) -- (O);
\draw[<->, densely dotted] (X) -- (Y);
\draw[<->,red] (m) -- (Y);
\end{tikzpicture}
\end{document}
答案2
像这样吗?
诀窍是使用 tizlibrary从到calc
进行绘制。也就是说,从和之间的中间位置到。(X)!0.5!(O)
(Y)
(X)
(O)
(Y)
完整代码如下:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usepackage{tikzscale}
\usetikzlibrary{arrows,automata,shadows,calc}
\begin{document}
\centering
\tikzstyle{vertex_basic}=[rectangle,thick,top color=white,bottom color=black!20, minimum size=50pt,scale=1]
\tikzstyle{vertex}=[vertex_basic, draw=black!50]
\tikzset{initial text={}}
\begin{tikzpicture}[,>=stealth,ultra thick,black!50,text=black, scale=1.8,graphs/every graph/.style={edges=rounded corners},every new ->/.style={shorten >=1pt},auto]
\matrix[row sep=20mm,column sep=2mm] {
\node (X) [vertex,align=center] {X}; & & \node (Y) [vertex,align=center] {Y}; \\
& \node (O) [vertex,align=center] {Outcome}; &\\
};
\path[<->] (X) edge node[align=center] {} (O);
\path[<->] (Y) edge[densely dotted] node[align=left] {} (O);
\path[<->] (X) edge[densely dotted] node[align=left] {} (Y);
\draw[<->, red, very thick] ($ (X)!0.5!(O) $) -- (Y);
\end{tikzpicture}
\end{document}
如果你想画到西南角,Y
那么你当然可以使用
\draw[<->, red, very thick] ($ (X)!0.5!(O) $) -- (Y.south west);
生产: