Tikz Graph 中标签边缘的位置

Tikz Graph 中标签边缘的位置

我制作了以下图表在此处输入图片描述

其对应的代码如下:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=1cm]{geometry}
\usepackage{tikz,pgfplots,pgf}
\usetikzlibrary{matrix,shapes,arrows,positioning}
\begin{document}

\begin{tikzpicture}[>=latex]
\path
+(0,-0.75)     node[circle,draw,scale=2, fill=white!50] (S) {}
+(0,0.75)     node[circle,draw,scale=2, fill=white!50] (K) {}
+(-3.5,1.5)  node[circle,draw, scale = 2,fill=white!50]  (x1) {}
+(-3.5,0)    node[circle,draw, scale = 2, fill=white!50]  (x2) {}
+(-3.5,-1.5) node[circle,draw, scale = 2,fill=white!50]  (x3) {};
\draw[->] (x1)--(S) node[pos=.8,above]{$\omega_{1,1}^{(2)}$};
\draw[->] (x2)--(S) node[pos=.8,above]{$\omega_{1,2}^{(2)}$};
\draw[->] (x3)--(S) node[pos=.8,above]{$\omega_{1,3}^{(2)}$};
\draw[->] (x1)--(K) node[pos=.1,above]{$\omega_{1,1}^{(2)}$};
\draw[->] (x2)--(K) node[pos=.1,above]{$\omega_{1,2}^{(2)}$};
\draw[->] (x3)--(K) node[pos=.1,above,bend left=50]{$\omega_{1,3}^{(2)}$};
\end{tikzpicture}

\end{document}

显然,重量标签看起来不太整齐。我希望重量如下图所示;

在此处输入图片描述

这也引出了我的下一个问题:如何在右侧添加大括号,后面跟着一个方程式?任何正确的帮助都将不胜感激。

答案1

这里就有参数\a\b让代码更方便调整!

在此处输入图片描述

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=1cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing} % for brace 
\begin{document}
\begin{tikzpicture}
[n/.style={circle,fill=gray!50,minimum size=8mm}]
\def\a{2}
\def\b{3.5}
\path
(0,0)        node[n] (R)  {}
+(0,\a)      node[n] (Ru) {}
+(0,-\a)     node[n] (Rb) {}
+(\b,.5*\a)  node[n,outer sep=1mm] (Lu) {}
+(\b,-.5*\a) node[n,outer sep=1mm] (Lb) {};
\draw[->] (Ru)--(Lu) node[pos=.2,above=-1mm,sloped]  {$\omega_{1,1}$};
\draw[->] (Ru)--(Lb) node[pos=.85,above=-1mm,sloped] {$\omega_{2,1}$};
\draw[->] (R)--(Lu)  node[pos=.2,above=-1mm,sloped]  {$\omega_{1,2}$};
\draw[->] (R)--(Lb)  node[pos=.75,above=-1mm,sloped] {$\omega_{2,2}$};
\draw[->] (Rb)--(Lu) node[pos=.2,above=-1mm,sloped]  {$\omega_{1,3}$};
\draw[->] (Rb)--(Lb) node[pos=.7,above=-1mm,sloped]  {$\omega_{2,3}$};
\draw[decorate,decoration={brace},gray] (\b+1,\a)--(\b+1,-\a);
\path (\b+1.5,0) node[right]{$\mathbf{\Omega=}$}; 
\end{tikzpicture}
\end{document}

答案2

只需对您的代码进行一些更改:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=1cm]{geometry}
\usepackage{tikz,pgfplots,pgf}
\usetikzlibrary{matrix,shapes,arrows,positioning}
\begin{document}

\[
\left.
\begin{tikzpicture}[>=to,baseline]
\path
+(0,-1)     node[circle,scale=2, fill=lightgray] (S) {}
+(0,1)     node[circle,scale=2, fill=lightgray] (K) {}
+(-3,1.5)  node[circle, scale = 2,fill=lightgray]  (x1) {}
+(-3,0)    node[circle,scale = 2, fill=lightgray]  (x2) {}
+(-3,-1.5) node[circle, scale = 2,fill=lightgray]  (x3) {};
\draw[->, gray] (x1)--(S) node[sloped, pos=.8,above, yshift=-1mm, black]{\small$\omega_{2,1}$};
\draw[->, gray] (x2)--(S) node[sloped, pos=.8,above, yshift=-1mm, black]{\small$\omega_{2,2}$};
\draw[->, gray] (x3)--(S) node[sloped, pos=.8,above, yshift=-1mm, black]{\small$\omega_{2,3}$};
\draw[->, gray] (x1)--(K) node[sloped, pos=.2,above, yshift=-1mm, black]{\small$\omega_{1,1}$};
\draw[->, gray] (x2)--(K) node[sloped, pos=.2,above, yshift=-1mm, black]{\small$\omega_{1,2}$};
\draw[->, gray] (x3)--(K) node[sloped, pos=.2,above, yshift=-1mm, black]{\small$\omega_{1,3}$};
\end{tikzpicture}
\quad\right\}\quad\Omega=
\]
\end{document}
  • fill=lightgray为节点着色(或者您可以使用gray!50或任何百分比
  • 删除draw以移除边框圆
  • gray用于箭头颜色(但black用于标签颜色)
  • 包括sloped让标签跟随箭头
  • 负数yshift可减少箭头和标签之间的空间
  • >=to所需的箭头
  • baseline确保支架相对于图表正确放置
  • \left.\right\}右括号
  • \small减小标签尺寸。(\footnotesize甚至更小)

在此处输入图片描述

相关内容