# Scaling a tikzpicture for a Beamer slide

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?

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?)

-
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 1:17
You could try \resizebox{\linewidth}{!}{begin{tikzpicture} ... \end{tikzpicture}}. –  Peter Grill Jul 10 '12 at 1: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 1:21
Sorry, I had assumed that your image was too wide when I posted that comment. –  Peter Grill Jul 10 '12 at 1:32

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:

% Body:

\begin{frame}
\begin{tikzpicture}[..]
..
\end{tikzpicture}
\end{frame}


See the adjustbox manual for more keys and other details.

-

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}$};


## 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)$};
\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}

-
The "no" line going up to the reject box isn't quite vertical :( I'm also about to start another question about the overlapped flow line from "Stop?" to "S_{n+1}". –  Ozzah Jul 10 '12 at 1:40
+1 did you try using scale=<number> where <number> is less than 1? –  cmhughes Jul 10 '12 at 2:03
@cmhughes: Yep, does not seem to have any effect. –  Peter Grill Jul 10 '12 at 2:08