我有一张投影幻灯片:
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\tikzstyle{decision} = [diamond, draw, fill=blue!20, text width=4.5em, text badly centered, node distance=3cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, draw, fill=blue!20, text width=5em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [draw, -latex]
\begin{document}
\begin{frame}{Simulated Annealing (SA)}
\begin{center}
\resizebox{0.4 \linewidth}{!}{%
\begin{tikzpicture}[node distance = 2cm, auto]
\node[block] (init){Init $n=0$, $T_0$, and $S_0$};
\node[block, below of=init] (nbrh){$S_{n+1}=N(S_n)$};
\node[decision, below of=nbrh](ovgt){$f(S_{n+1}) \le f(S_n)$};
\node[block, below of=ovgt] (accp){Accept $S_{n+1}$};
\node[decision, right of=ovgt](rand){Accept with $P = e^{-\frac{\Delta f}{t_n}}$};
\node[block, right of=nbrh, anchor=west] (rejj){Reject $S_{n+1}$};
\node[block, below of=accp] (incr){$T_{n+1} = K(T_n)$ and $n=n+1$};
\node[block, below of=incr] (stop){Stop};
\node[decision, left of=stop] (stcd){Stop?};
\path[line] (init) -- (nbrh);
\path[line] (nbrh) -- (ovgt);
\path[line] (ovgt) -- node{yes}(accp);
\path[line] (ovgt) -- node{no} (rand);
\path[line] (rand) -- node{no} (rejj);
\path[line] (rejj) -- (nbrh);
\path[line] (rand) |- node{yes}(accp);
\path[line] (accp) -- (incr);
\path[line] (incr) -- (stcd);
\path[line] (stcd) -- node{yes}(stop);
\path[line] (stcd) |- node{no} (nbrh);
\end{tikzpicture}%
}%
\end{center}
\end{frame}
\end{document}
这使:
我对输出结果有些不满意:
简单的方程式
$S_{n+1}=N(S_n)%
应该在一行上。文本可以小一点。同样,$T_{n+1}=K(T_n)$
应该在一行上,'和' 可以单独成行,也$n=n+1$
应该单独成行。判决中的不平等也应该在同一行。连接右侧“接受”决策和“拒绝”操作的垂直“否”线不是完全垂直的。此外,此决策比左侧的决策更大。
这两个决定之间的差距很小。
不等式判定下的“接受”操作与上面的判定重叠。相反,该判定与上面的操作相距甚远。
最后,我对底部的两个元素不满意。我更希望将“停止?”决定直接放在下方,将“停止”操作放在右侧。不幸的是,我不知道如何在“停止?”的“否”中放置 2 个弯角,这意味着该线实际上会穿过上面的所有内容。我试过了,
|-|
但-|-
不起作用。
我之前问过一个问题这里关于将整个 tikzpicture 缩放到幻灯片的高度,但是提出的解决方案对我来说不起作用。它仍然在底部被切断。问题可能与我的实际 beamer 演示文稿使用 Hannover 主题有关,因此幻灯片中可用的空间较少?我已经通过0.4 \linewidth
临时使用解决了这个问题。
答案1
我的解决方案与 Altermundus 的上次编辑类似(我太慢了),但我解决了仅在一行中包含方程式的问题。此外,\resizebox
我没有采用,而是更喜欢\scalebox
从graphicx
包中获取。
以下是代码:
\documentclass{beamer}
\usepackage{lmodern}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}
\tikzset{decision/.style={diamond, draw, fill=blue!20, text width=4.5em, text badly centered, inner sep=0pt}}
\tikzset{block/.style={rectangle, draw, fill=blue!20, text width=5em, text centered, rounded corners,
minimum width=3.5cm}}
\tikzset{line/.style={draw, -latex}}
\begin{document}
\begin{frame}{Simulated Annealing (SA)}
\begin{center}
\scalebox{0.6}{
\begin{tikzpicture}[node distance=2.3cm]
\node[block] (init){Init $n=0$, $T_0$, and $S_0$};
\node[block, below of=init] (nbrh){\footnotesize{$S_{n+1}=N(S_n)$}};
\node[decision, below of=nbrh](ovgt){\footnotesize{$f(S_{n+1}) \le f(S_n)$}};
\node[block, below of=ovgt] (accp){Accept $S_{n+1}$};
\node[decision, right=2cm of ovgt](rand){Accept with $P = e^{-\frac{\Delta f}{t_n}}$};
\node[block, above of=rand] (rejj){Reject $S_{n+1}$};
\node[block, below of=accp] (incr){\scriptsize{$T_{n+1} = K(T_n)$} \\[1ex] and\\ \scriptsize{$n=n+1$}};
\node[decision, below of=incr] (stcd){Stop?};
\node[block, right= 2cm of stcd] (stop){Stop};
% invisible node helpful later
\node[left=1cm of accp,scale=0.05](inv){};
\path[line] (init) -- (nbrh);
\path[line] (nbrh) -- (ovgt);
\path[line] (ovgt) -- node[left]{yes}(accp);
\path[line] (ovgt) -- node[above]{no} (rand);
\path[line] (rand) -- node[right]{no} (rejj);
\path[line] (rejj) -- (nbrh);
\path[line] (rand) |- node[below]{yes}(accp);
\path[line] (accp) -- (incr);
\path[line] (incr) -- (stcd);
\path[line] (stcd) -- node[below]{yes}(stop);
\path[-,draw] (stcd) -| node{} (inv.north);
\path[line]{} (inv.north) |- node[above]{no} (nbrh);
\end{tikzpicture}%
}
\end{center}
\end{frame}
\end{document}
这使:
答案2
如果使用该 positioning
库,则可以避免一些问题。您需要使用选项on grid
,您可以局部修改node distance
。菱形节点的宽度与矩形的宽度不同,因此如果您想要垂直边缘,则需要将矩形放置在菱形上方。
\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}
\tikzstyle{decision} = [diamond, draw, fill=blue!20, text width=4.5em, text badly centered, node distance=3cm, inner sep=0pt,on grid]
\tikzstyle{block} = [rectangle, draw, fill=blue!20, text width=5em, text centered, rounded corners, minimum height=4em,on grid]
\tikzstyle{line} = [draw, -latex]
\begin{document}
\begin{center}
\resizebox{0.4 \linewidth}{!}{%
\begin{tikzpicture}[node distance = 2cm, auto]
\node[block] (init) {Init $n=0$, $T_0$, and $S_0$};
\node[block, below= of init] (nbrh) {$S_{n+1}=N(S_n)$};
\node[decision, below= of nbrh] (ovgt) {$f(S_{n+1}) \le f(S_n)$};
\node[block, below=2.5cm of ovgt] (accp) {Accept $S_{n+1}$};
\node[decision, right= 3.5cm of ovgt] (rand) {Accept with $P = e^{-\frac{\Delta f}{t_n}}$};
\node[block, above=3cm of rand] (rejj) {Reject $S_{n+1}$};
\node[block, below= of accp] (incr) {$T_{n+1} = K(T_n)$ and $n=n+1$};
\node[decision, below=2.5cm of incr] (stcd) {Stop?};
\node[block, right=3cm of stcd] (stop) {Stop};
\path[line] (init) -- (nbrh);
\path[line] (nbrh) -- (ovgt);
\path[line] (ovgt) -- node{yes}(accp);
\path[line] (ovgt) -- node{no} (rand);
\path[line] (rand) -- node{no} (rejj);
\path[line] (rejj) -- (nbrh);
\path[line] (rand) |- node{yes}(accp);
\path[line] (accp) -- (incr);
\path[line] (incr) -- (stcd);
\path[line] (stcd) -- node{yes}(stop);
\path[line] (stcd) -- ++(-2,0) |- node[pos=.25]{no} (nbrh);
\end{tikzpicture}%
}%
\end{center}
\end{document}
答案3
您可能会发现使用\matrix[matrix of nodes]
很容易,因为这样您就可以移动节点。这样就剩下绘制路径了,无论如何这都是很无聊的。
为了禁止数学模式中的换行,您可以将\binoppenalty
和设置\relpenalty
为高值。
上面的例子(我在绘制路径时删减了一些角落):
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows}
\tikzset{
% re-usability is the key to happiness
>=triangle 45,
flowchart/.style={
matrix of nodes,
nodes=block,
row sep=2\normalbaselineskip,
column sep=3em,
},
block/.style={
%font=\scriptsize, % <- that'd change font size
draw,
fill=blue!20,
minimum height=3\normalbaselineskip,
text width=7em,
text badly centered,
rounded corners,
execute at begin node={
\hskip0pt
\binoppenalty=10000 % make it bad to break math line
\relpenalty=10000},
},
decision/.style={% inherits from block via matrix
diamond,
sharp corners,
inner sep=1pt,
}
}
\begin{document}
\begin{tikzpicture}
\matrix[flowchart] (fc) {
Init $n=0$, $T_0$, and $S_0$ \\
$S_{n+1}=N(S_n)$ & Reject $S_{n+1}$ \\
|[decision]| $f(S_{n+1})\leq f(S_n)$ &
|[decision]| Accept with $P=e^{-\frac{\Delta f}{t_n}}$ \\
Accept $S_{n+1}$ \\
$T_{n+1}=K(T_n)$ and $n=n+1$ \\
|[decision]| Stop? & Stop \\
};
\begin{scope}[->]
\draw (fc-1-1) -- (fc-2-1) edge (fc-3-1);
\draw (fc-2-1) -- (fc-3-1) edge node[above] {no} (fc-3-2);
\draw (fc-3-2) edge node[right] {no} (fc-2-2) |- node[below] {yes} (fc-4-1);
\draw (fc-2-2) -- (fc-2-1);
\draw (fc-3-1) -- node[right] {yes} (fc-4-1);
\draw (fc-4-1) -- (fc-5-1) edge (fc-6-1);
\draw (fc-6-1) edge node[above] {yes} (fc-6-2) -- +(-3,0) node[below] {no} |- (fc-2-1);
\end{scope}
\end{tikzpicture}
\end{document}
(第一个无分支有一个小缺陷,我将其留作练习;-)
。)