这是我的图形模型的代码:
\documentclass{standalone}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,fit}
\usetikzlibrary{shadows}
\usetikzlibrary{positioning}
\begin{document}
\pagestyle{empty}
% Define block styles
\tikzset{
latentnode/.style ={draw,minimum width=2.5em, shape=circle,thick, black,fill=white},
visiblenode/.style ={draw, minimum width=2.5em, shape=circle,thick, black,fill=black!20},
plate/.style={draw,
shape=rectangle,
thick,
minimum width=4em,
minimum height=4em,
align=right,
inner sep=5em,
inner ysep=5em,
label={[xshift=-16pt,yshift=11pt]south east:#1}}
}
\tikzstyle{line} = [draw, -latex']
\begin{tikzpicture}[auto,thick,node distance=5em]
% NODES
\node[latentnode] (z0) {$\mathbf{x}_1$};
\node[latentnode, right of=z0] (z1) {$\mathbf{x}_2$};
\node[latentnode, right of=z1] (z2) {$\mathbf{x}_3$};
\node[draw=none,,right= 1.5em of z2] (zdots) {$\cdots$};
\node[latentnode,right= 1.5em of zdots] (zN) {$\mathbf{x}_T$};
% hyperparameter
\node[latentnode,left of = z0] (pi) {$\boldsymbol \pi_k$};
\node[plate=$\infty$, inner sep=5pt, fit=(pi)] (plate1) {};
\node[draw=none,left of = pi] (alpha) {$\alpha$};
\node[draw=none,left of = pi, above=0.6em of pi] (kap) {$\kappa$};
\node[latentnode,above of = pi] (b) {$\boldsymbol \beta$};
\node[draw=none,left of = b] (g) {$\gamma$};
\node[visiblenode,below of=z0] (x0) {$\mathbf{y}_1$};
\node[visiblenode,below of=z1] (x1) {$\mathbf{y}_2$};
\node[visiblenode, right of=x1] (x2) {$\mathbf{y}_3$};
\node[draw=none,right= 1.5em of x2] (xdots) {$\cdots$};
\node[visiblenode,right= 1.5em of xdots] (xN) {$\mathbf{y}_T$};
% hyperparameters
\node[latentnode,left of =x0] (t) {$\boldsymbol \theta_k$};
\node[plate=$\infty$, inner sep=5pt, fit=(t)] (plate1) {};
\node[draw=none,left of = t] (lam) {$\boldsymbol \lambda$};
% PATHS
% horizontal
\path [line] (z0) -- (z1);
\path [line] (z1) -- (z2);
% vertical
\path [line] (z0) -- (x0);
\path [line] (z1) -- (x1);
\path [line] (z2) -- (x2);
\path [line] (zN) -- (xN);
% connect dots
\path [line] (z2) -- (zdots);
\path [line] (zdots) -- (zN);
%hyperparameter
\path [line] (lam) -- (t);
\path [line] (alpha) -- (pi);
\path [line] (b) -- (pi);
\path [line] (g) -- (b);
\path [line] (kap) -- (pi);
\path [line] (pi) -- (z0);
\path [line] (pi.35) to [bend left=20] (z1.135);
\path [line] (pi.35) to [bend left=20] (z2.135);
\path [line] (pi.35) to [bend left=20] (zN.135);
% \path [line] (pi.0) -- (zN.140);
\path [line] (t.0) -- (x0);
\path [line] (t.325) to [bend right=20] (x1.225);
\path [line] (t.325) to [bend right=20] (x2.225);
\path [line] (t.325) to [bend right=20] (xN.225);
\end{tikzpicture}
\end{document}
结果是这样的:
如您所见,整个图像偏离中心,并且最右侧节点的部分缺失。
为什么模型没有完全居中渲染?
答案1
它位于中心,但最左侧的节点未绘制,因此它们inner sep
显然不是节点的一部分,而右侧的节点已绘制,因此它们inner sep
看起来是节点边界内内容的一部分。要查看这一点,只需将最左侧的节点更改draw=none
为draw=red
。
可以看出,最左侧节点的最左侧紧靠图片的边界,就像最右侧节点边界上的最右侧点一样。(并非所有最左侧节点都紧靠图片边界,因为节点的宽度会根据其内容而变化,但最宽的节点紧靠边界。)
底部的空间可能是由于在构建弯曲路径时使用了不可见的点,尽管我对此并不确定。
\documentclass[tikz]{standalone}
\usepackage{amsmath}
\usetikzlibrary{arrows,fit,positioning}
\begin{document}
% Define block styles
\tikzset{
latentnode/.style ={draw,minimum width=2.5em, shape=circle,thick, black,fill=white},
visiblenode/.style ={draw, minimum width=2.5em, shape=circle,thick, black,fill=black!20},
plate/.style={draw,
shape=rectangle,
thick,
minimum width=4em,
minimum height=4em,
align=right,
inner sep=5em,
inner ysep=5em,
label={[xshift=-16pt,yshift=11pt]south east:#1}},
line/.style={draw, -latex'}
}
\begin{tikzpicture}[auto,thick,node distance=5em]
% NODES
\node[latentnode] (z0) {$\mathbf{x}_1$};
\node[latentnode, right of=z0] (z1) {$\mathbf{x}_2$};
\node[latentnode, right of=z1] (z2) {$\mathbf{x}_3$};
\node[draw=none,,right= 1.5em of z2] (zdots) {$\cdots$};
\node[latentnode,right= 1.5em of zdots] (zN) {$\mathbf{x}_T$};
% hyperparameter
\node[latentnode,left of = z0] (pi) {$\boldsymbol \pi_k$};
\node[plate=$\infty$, inner sep=5pt, fit=(pi)] (plate1) {};
\node[draw=red,left of = pi] (alpha) {$\alpha$};
\node[draw=red,left of = pi, above=0.6em of pi] (kap) {$\kappa$};
\node[latentnode,above of = pi] (b) {$\boldsymbol \beta$};
\node[draw=red,left of = b] (g) {$\gamma$};
\node[visiblenode,below of=z0] (x0) {$\mathbf{y}_1$};
\node[visiblenode,below of=z1] (x1) {$\mathbf{y}_2$};
\node[visiblenode, right of=x1] (x2) {$\mathbf{y}_3$};
\node[draw=none,right= 1.5em of x2] (xdots) {$\cdots$};
\node[visiblenode,right= 1.5em of xdots] (xN) {$\mathbf{y}_T$};
% hyperparameters
\node[latentnode,left of =x0] (t) {$\boldsymbol \theta_k$};
\node[plate=$\infty$, inner sep=5pt, fit=(t)] (plate1) {};
\node[draw=red,left of = t] (lam) {$\boldsymbol \lambda$};
% PATHS
% horizontal
\path [line] (z0) -- (z1);
\path [line] (z1) -- (z2);
% vertical
\path [line] (z0) -- (x0);
\path [line] (z1) -- (x1);
\path [line] (z2) -- (x2);
\path [line] (zN) -- (xN);
% connect dots
\path [line] (z2) -- (zdots);
\path [line] (zdots) -- (zN);
%hyperparameter
\path [line] (lam) -- (t);
\path [line] (alpha) -- (pi);
\path [line] (b) -- (pi);
\path [line] (g) -- (b);
\path [line] (kap) -- (pi);
\path [line] (pi) -- (z0);
\path [line] (pi.35) to [bend left=20] (z1.135);
\path [line] (pi.35) to [bend left=20] (z2.135);
\path [line] (pi.35) to [bend left=20] (zN.135);
% \path [line] (pi.0) -- (zN.140);
\path [line] (t.0) -- (x0);
\path [line] (t.325) to [bend right=20] (x1.225);
\path [line] (t.325) to [bend right=20] (x2.225);
\path [line] (t.325) to [bend right=20] (xN.225);
\end{tikzpicture}
\end{document}
编辑
作为Ignasi提到,inner sep
或者inner xsep
可以对相关节点进行归零。例如,我们可以对这些节点使用以下样式:
jump left/.style={draw=none, inner xsep=0pt},
但这会影响右侧和左侧,因为伊格纳西说,所以你需要对此进行补偿。例如,我们可以line
为这些线条定义一个修改版本的样式。
jump line/.style={line, shorten <=5pt}
进行这些更改会产生以下结果。
完整代码:
\documentclass[tikz]{standalone}
\usepackage{amsmath}
\usetikzlibrary{arrows,fit,positioning}
\begin{document}
% Define block styles
\tikzset{
latentnode/.style ={draw,minimum width=2.5em, shape=circle,thick, black,fill=white},
visiblenode/.style ={draw, minimum width=2.5em, shape=circle,thick, black,fill=black!20},
plate/.style={draw,
shape=rectangle,
thick,
minimum width=4em,
minimum height=4em,
align=right,
inner sep=5em,
inner ysep=5em,
label={[xshift=-16pt,yshift=11pt]south east:#1}},
line/.style={draw, -latex'},
jump left/.style={draw=none, inner xsep=0pt},
jump line/.style={line, shorten <=5pt}
}
\begin{tikzpicture}[auto,thick,node distance=5em]
% NODES
\node[latentnode] (z0) {$\mathbf{x}_1$};
\node[latentnode, right of=z0] (z1) {$\mathbf{x}_2$};
\node[latentnode, right of=z1] (z2) {$\mathbf{x}_3$};
\node[draw=none,,right= 1.5em of z2] (zdots) {$\cdots$};
\node[latentnode,right= 1.5em of zdots] (zN) {$\mathbf{x}_T$};
% hyperparameter
\node[latentnode,left of = z0] (pi) {$\boldsymbol \pi_k$};
\node[plate=$\infty$, inner sep=5pt, fit=(pi)] (plate1) {};
\node[jump left, left of = pi] (alpha) {$\alpha$};
\node[jump left, left of = pi, above=0.6em of pi] (kap) {$\kappa$};
\node[latentnode,above of = pi] (b) {$\boldsymbol \beta$};
\node[jump left, left of = b] (g) {$\gamma$};
\node[visiblenode,below of=z0] (x0) {$\mathbf{y}_1$};
\node[visiblenode,below of=z1] (x1) {$\mathbf{y}_2$};
\node[visiblenode, right of=x1] (x2) {$\mathbf{y}_3$};
\node[draw=none,right= 1.5em of x2] (xdots) {$\cdots$};
\node[visiblenode,right= 1.5em of xdots] (xN) {$\mathbf{y}_T$};
% hyperparameters
\node[latentnode,left of =x0] (t) {$\boldsymbol \theta_k$};
\node[plate=$\infty$, inner sep=5pt, fit=(t)] (plate1) {};
\node[jump left, left of = t] (lam) {$\boldsymbol \lambda$};
% PATHS
% horizontal
\path [line] (z0) -- (z1);
\path [line] (z1) -- (z2);
% vertical
\path [line] (z0) -- (x0);
\path [line] (z1) -- (x1);
\path [line] (z2) -- (x2);
\path [line] (zN) -- (xN);
% connect dots
\path [line] (z2) -- (zdots);
\path [line] (zdots) -- (zN);
%hyperparameter
\path [jump line] (lam) -- (t);
\path [jump line] (alpha) -- (pi);
\path [line] (b) -- (pi);
\path [jump line] (g) -- (b);
\path [jump line] (kap) -- (pi);
\path [line] (pi) -- (z0);
\path [line] (pi.35) to [bend left=20] (z1.135);
\path [line] (pi.35) to [bend left=20] (z2.135);
\path [line] (pi.35) to [bend left=20] (zN.135);
% \path [line] (pi.0) -- (zN.140);
\path [line] (t.0) -- (x0);
\path [line] (t.325) to [bend right=20] (x1.225);
\path [line] (t.325) to [bend right=20] (x2.225);
\path [line] (t.325) to [bend right=20] (xN.225);
\end{tikzpicture}
\end{document}