我正在尝试使用 TikZ 库绘制概率图形模型BayesNet
,该库用于在 latex 中绘制图形模型。我尝试插入板块标题 - 任务我、类C, 工人杰, 特征钾- 就像下面给出的第一张图片一样,但我无法更改车牌标题的位置。我如何修改第二张图片的源代码来像第一张图片一样更改所有车牌标题的位置?
以下是我使用的代码:
\documentclass{article} % For LaTeX2e
\usepackage{nips13submit_e,times}
\usepackage{hyperref}
\usepackage{url}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{bayesnet}
\begin{document}
\begin{tikzpicture}
% Define nodes
\node[obs] (y) {$y_{i}^{(j)}$};
\node[obs, right=of y] (f) {$f_{ik}^{(j)}$};
\factor[above=of y,yshift=0.7cm] {yf} {Cat} {} {} ; %
\factor[above=of f,yshift=0.7cm] {rf} {Cat} {} {} ;
\node[latent, left=of yf] (pi) {$\pi_{c}^{(j)}$};
\node[latent, above=of pi, yshift=0.5cm] (beta) {$\beta_{c}^{(j)}$};
\factor[above=of pi, yshift=0.4cm] {pi-f} {left:Dir} {} {} ; %
\node[latent, above=of yf, xshift=0.8cm, yshift=3cm ] (t) {$t_i$};
\node[latent, above=of t, yshift=0.5cm] (p) {$p$};
\node[latent, right=of p, xshift=0.8cm] (alpha) {$\alpha$};
\factor[above=of t, yshift=0.5cm] {t-f} {left:Dir} {} {} ; %
\factor[right=of p, xshift=0.5cm] {p-f} {Dir} {} {} ; %
\node[latent, right=of rf] (d) {$z_{k,c}^{(i)}$};
\node[latent, above=of d, yshift=0.5cm] (gamma) {$\gamma_{0,c}^{(i)}$};
\factor[above=of d, yshift=0.4cm] {d-f} {right:Dir} {} {} ; %
% Connect the nodes
\factoredge {gamma} {d-f} {d} ;
\factoredge {alpha} {p-f} {p}; %
\factoredge {p} {t-f} {t}; %
\factoredge {beta} {pi-f} {pi} ;
\factoredge {pi} {yf} {y} ;
\factoredge {d} {rf} {f} ;
\gate {y-gate} {(yf)(yf-caption)} {t}
\gate {f-gate} {(rf)(rf-caption)} {t}
% Plates
\plate [inner sep=0.3cm, xshift=0.1cm, yshift=-0.2cm,color=red] {pA} {(f)(rf)(d)(d-f)} {$k \in K_i$}; %
\plate [inner sep=0.5cm, xshift=0.2cm, yshift=0.5cm, color=blue] {pC} {(beta)(pi)(d)(gamma)} {C classes};
\plate [inner sep=0.7cm, xshift=0.3cm, yshift=-0.4cm, color=brown] {pT} {(t)(gamma)(d)(yf)(y)(rf)(f)} {N objects};
\plate [inner sep=0.5cm, xshift=-0.2cm] {pM} {(beta)(pi)(yf)(y)(rf)(f)} {M workers} ;
\plate [inner sep=0.2cm, xshift=-0.05cm, yshift=0.15cm] {pJi} {(y)(f)} {$J_i$}
%minimum height=1.5cm, minimum width=3.0cm, yshift=0.4cm,
\end{tikzpicture}
\end{document}
答案1
之前没有见过这个库,可能是因为它是第三方库,而且 TikZ 手册中没有讨论。唯一的文档似乎是一个简短的自述文件,所以我查看了库本身的代码。
可以通过kpsewhich tikzlibrarybayesnet.code.tex
在终端中运行来找到该库文件,在我的系统上它位于/usr/local/texlive/2015/texmf-dist/tex/latex/tikz-bayesnet/tikzlibrarybayesnet.code.tex
。
该\plate
命令定义如下:
\newcommand{\plate}[4][]{ %
\node[wrap=#3] (#2-wrap) {}; %
\node[plate caption=#2-wrap] (#2-caption) {#4}; %
\node[plate=(#2-wrap)(#2-caption), #1] (#2) {}; %
}
标题由第二个节点创建,该节点使用样式plate caption
。此样式又定义为
\tikzstyle{plate caption} = [caption, node distance=0, inner sep=0pt,
below left=5pt and 0pt of #1.south east]
因此,您可以看到它相对于south east
节点 ( #1
) 的角进行定位,该节点由赋予样式的参数定义,即#2-wrap
在 的定义中\plate
。#2
这里是命令的第一个强制参数\plate
,即选项后的第一个参数。节点#2-wrap
是使用选项定义的节点fit
,并且是包含 的第二个强制参数中给出的所有节点的节点\plate
。
的实际框架\plate
由其定义中的第三个绘制\node
,它是一个包含标题和节点列表的节点\plate
(也通过fit
库)。
移动标题的一种方法是重新定义样式plate caption
。正如库所写,必须在\plate
使用 的每个命令之前完成此\tikzset
操作。也就是说,添加类似
\tikzset{plate caption/.append style={below right=0pt and 0pt of #1.north west}
在 之前\plate
。使用append style
上述方法,您不必包含其余的样式定义,只有位置会被覆盖。如果您只想修改一个,请在和\plate
周围放置一对括号,即\tikzset
\plate
{ % opening brace
\tikzset{..}
\plate{..}{..}{..}
} % closing brace
太晚了,我没法修改所有内容,所以在下面的代码中,我只移动了标题C classes
。希望您能自己弄清楚其余部分。
\documentclass[border=3mm]{standalone} % For LaTeX2e
\usepackage{tikz}
\usetikzlibrary{bayesnet}
\begin{document}
\begin{tikzpicture}
% Define nodes
\node[obs] (y) {$y_{i}^{(j)}$};
\node[obs, right=of y] (f) {$f_{ik}^{(j)}$};
\factor[above=of y,yshift=0.7cm] {yf} {Cat} {} {} ; %
\factor[above=of f,yshift=0.7cm] {rf} {Cat} {} {} ;
\node[latent, left=of yf] (pi) {$\pi_{c}^{(j)}$};
\node[latent, above=of pi, yshift=0.5cm] (beta) {$\beta_{c}^{(j)}$};
\factor[above=of pi, yshift=0.4cm] {pi-f} {left:Dir} {} {} ; %
\node[latent, above=of yf, xshift=0.8cm, yshift=3cm ] (t) {$t_i$};
\node[latent, above=of t, yshift=0.5cm] (p) {$p$};
\node[latent, right=of p, xshift=0.8cm] (alpha) {$\alpha$};
\factor[above=of t, yshift=0.5cm] {t-f} {left:Dir} {} {} ; %
\factor[right=of p, xshift=0.5cm] {p-f} {Dir} {} {} ; %
\node[latent, right=of rf] (d) {$z_{k,c}^{(i)}$};
\node[latent, above=of d, yshift=0.5cm] (gamma) {$\gamma_{0,c}^{(i)}$};
\factor[above=of d, yshift=0.4cm] {d-f} {right:Dir} {} {} ; %
% Connect the nodes
\factoredge {gamma} {d-f} {d} ;
\factoredge {alpha} {p-f} {p}; %
\factoredge {p} {t-f} {t}; %
\factoredge {beta} {pi-f} {pi} ;
\factoredge {pi} {yf} {y} ;
\factoredge {d} {rf} {f} ;
\gate {y-gate} {(yf)(yf-caption)} {t}
\gate {f-gate} {(rf)(rf-caption)} {t}
% Plates
\plate [inner sep=0.3cm, xshift=0.1cm, yshift=-0.2cm,color=red] {pA} {(f)(rf)(d)(d-f)} {$k \in K_i$}; %
{\tikzset{plate caption/.append style={above=5pt of #1.north west}}
\plate [inner sep=0.3cm, xshift=0.2cm, color=blue] {pC} {(beta)(pi)(d)(gamma)} {C classes};}
\plate [inner sep=0.7cm, xshift=0.3cm, yshift=-0.4cm, color=brown] {pT} {(t)(gamma)(d)(yf)(y)(rf)(f)} {N objects};
\plate [inner sep=0.2cm, xshift=-0.2cm] {pM} {(beta)(pi)(yf)(y)(rf)(f)} {M workers} ;
\plate [inner sep=0.2cm, xshift=-0.05cm, yshift=0.15cm] {pJi} {(y)(f)} {$J_i$}
%minimum height=1.5cm, minimum width=3.0cm, yshift=0.4cm,
\end{tikzpicture}
\end{document}
答案2
我正在使用同一个 repo,遇到了这个问题。仔细检查了 repo 后,我发现另一个解决方案是简单地将车牌标签包含在新车牌的参数中。这确保标题不会重叠。
做这样的事:
\plate {A} {(a)} {$M$} ;
\plate {B} {(b)(A)} {$N$} ;
代替 :
\plate {A} {(a)} {$M$} ;
\plate {B} {(b)(a)} {$N$} ;