为 Beamer 幻灯片缩放 tikzpicture

为 Beamer 幻灯片缩放 tikzpicture

我有以下 Beamer 幻灯片:

\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    ]
\tikzstyle{cloud} = [draw, ellipse,fill=red!20, node distance=3cm, minimum height=2em]

\begin{document}

\begin{frame}{Simulated Annealing (SA)}
    \begin{center}
        \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){$e^{-\frac{\Delta f}{t_n}}$};
            \node[block, right of=nbrh]   (rejj){Reject $S_{n+1}$};
            \node[block, below of=accp]   (incr){$T_{n+1} = K(T_n)$ and $n++$};
            \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) -|          (stcd);
            \path[line] (stcd) -- node{yes}(stop);
            \path[line] (stcd) |- node{no} (nbrh);
        \end{tikzpicture}
    \end{center}
\end{frame}

\end{document}

我的代码主要来自这里

唯一的问题是它不适合投影仪幻灯片。它不能自动缩放,而且它从底部流出很远。

我尝试添加scale = 0.25\begin{tikzpicture}[]没有任何效果。

有什么方法可以将整个 tikzpicture 自动放入框架的可用空间吗?

在此处输入图片描述

附加问题:为什么 Reject S_{n+1} 没有与下面的指数决策水平对齐,为什么箭头以一定角度连接它们?(即,如何解决这个问题?)

答案1

使用scale = 0.25\begin{tikzpicture}[]缩放坐标,而不是节点大小。对于像tikzpictures 这样复杂的事情,我推荐我的adjustbox包。在这里您可以指定最大尺寸,例如文本宽度和高度的因素。请注意,文本高度还包括标题行。您可以添加键center以水平居中它,而无需像环境那样在前后添加垂直空间center

% Preamble:
\usepackage{adjustbox}

% Body:

\begin{frame}
\begin{adjustbox}{max totalsize={.9\textwidth}{.7\textheight},center}
\begin{tikzpicture}[..]
 ..
\end{tikzpicture}
\end{adjustbox}
\end{frame}

请参阅adjustbox手册以了解更多按键和其他详细信息。

答案2

您可以使用\resizebox{5.0cm}{!}{}调整图像大小,并指定锚点来解决问题Reject $S_{n+1}$

\node[block, right of=nbrh, anchor=west]   (rejj){Reject $S_{n+1}$};

同样地,要修复该问题Accept $S_{n+1}$,请使用

\node[block, below of=ovgt, anchor=north]   (accp){Accept $S_{n+1}$};

在此处输入图片描述

代码:

\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    ]
\tikzstyle{cloud} = [draw, ellipse,fill=red!20, node distance=3cm, minimum height=2em]

\begin{document}
\begin{frame}{Simulated Annealing (SA)}
    \begin{center}
\resizebox{5.0cm}{!}{%
        \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, anchor=north]   (accp){Accept $S_{n+1}$};
            \node[decision, right of=ovgt](rand){$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++$};
            \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) -|          (stcd);
            \path[line] (stcd) -- node{yes}(stop);
            \path[line] (stcd) |- node{no} (nbrh);
        \end{tikzpicture}%
        }%
    \end{center}
\end{frame}
\end{document}

相关内容