48

I have the following Beamer slide:

\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}

and I got the code mostly from here.

The only problem is that it doesn't fit in the beamer slide. It isn't auto scaled, and it runs way off the bottom.

I have tried adding scale = 0.25 to the \begin{tikzpicture}[] but that didn't do anything at all.

Is there any way to auto-fit the whole tikzpicture into the available space in my frame?

enter image description here

Bonus question: Why is Reject S_{n+1} not horizontally aligned with the exponential decision underneath, and as such - why is the arrow connecting them at an angle? (i.e., how is this fixed?)

Peter Grill
  • 223,288
Ozzah
  • 1,209
  • While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. – Peter Grill Jul 10 '12 at 01:17
  • 1
    You could try \resizebox{\linewidth}{!}{begin{tikzpicture} ... \end{tikzpicture}}. – Peter Grill Jul 10 '12 at 01:18
  • @PeterGrill That makes it even wider, and hence taller. Without the \resizebox the width is only about 75% the width of the slide. – Ozzah Jul 10 '12 at 01:21
  • Sorry, I had assumed that your image was too wide when I posted that comment. – Peter Grill Jul 10 '12 at 01:32

2 Answers2

47

Using scale = 0.25 to the \begin{tikzpicture}[] only scales coordinates, but not node sizes. For complex things like tikzpictures I recommend my adjustbox package. Here you can specify the maximal size, e.g. in factors of the text width and height. Note that the text height also includes the head line. You can add the center key to horizontally center it without adding vertical space before and after like with the center environment:

% Preamble:
\usepackage{adjustbox}

% Body:

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

See the adjustbox manual for more keys and other details.

Martin Scharrer
  • 262,582
18

You can use \resizebox{5.0cm}{!}{} to resize the image, and specifying and anchor point fixes the problem with the Reject $S_{n+1}$:

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

Similarly to fix the issue with Accept $S_{n+1}$, use

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

enter image description here

Code:

\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)$}; \nodedecision, below of=nbrh{$f(S_{n+1}) \le f(S_n)$}; \node[block, below of=ovgt, anchor=north] (accp){Accept $S_{n+1}$}; \nodedecision, right of=ovgt{$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}

Peter Grill
  • 223,288