[Tex/LaTex] Scale TikZ figure to linewidth when relative positioning used

marginsscalingtikz-pgf

This question is a follow-up to How to scale a tikzpicture to \textwidth.
Unfortunately I haven't been a member long enough to post comments in that thread to ask for clarification.

I have some figures that I've drawn with relative positioning commands like "right of" and "below of". It seems like when you use these, most of the typical scaling approaches don't work anymore. For an example, there's the simple flow chart from http://texample.net. I've included the code below in case the example changes in the future.

So, suppose I want to scale the figure from the example below. If I use

\begin{tikzpicture}[node distance = 2cm, auto, scale=0.5]

nothing happens. I've also tried the first method at the above link that scales a tikz figure to \linewidth. It did not work for me. Can someone tell me how to make this type of figure

  1. scale to some constant amount like I've shown, and

  2. scale to \linewidth?

Example simple flow chart (modified) from
http://www.texample.net/tikz/examples/simple-flow-chart/

\documentclass[12pt,oneside, ]{report} 

\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}

\begin{document}
%\pagestyle{empty}

% Define block styles
\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{tikzpicture}[node distance = 2cm, auto]
    % Place nodes
    \node [block] (init) {initialize model};
    \node [cloud, left of=init] (expert) {expert};
    \node [cloud, right of=init] (system) {system};
    \node [block, below of=init] (identify) {identify candidate models};
    \node [block, below of=identify] (evaluate) {evaluate candidate models};
    \node [block, left of=evaluate, node distance=3cm] (update) {update model};
    \node [decision, below of=evaluate] (decide) {is best candidate better?};
    \node [block, below of=decide, node distance=3cm] (stop) {stop};
    % Draw edges
    \path [line] (init) -- (identify);
    \path [line] (identify) -- (evaluate);
    \path [line] (evaluate) -- (decide);
    \path [line] (decide) -| node [near start] {yes} (update);
    \path [line] (update) |- (identify);
    \path [line] (decide) -- node {no}(stop);
    \path [line,dashed] (expert) -- (init);
    \path [line,dashed] (system) -- (init);
    \path [line,dashed] (system) |- (evaluate);
\end{tikzpicture}

\end{document}

Best Answer

You can use \resizebox{\linewidth}{!}{<tikz picture>} from the graphics package which is already loaded by tikz anyway. It scales its content to the given width and height (! for the height means that it scales with the width). One drawback of this approach is that everything is scaled including the line width and node texts which might not be what some people want.

Note that \resizebox reads the whole picture as a macro argument, which isn't very efficient and does not allow verbatim or other special content. Alternatives are \Resizebox (same syntax) from my realboxes package or the {adjustbox}{width=\linewidth} environment from my adjustbox package.