[Tex/LaTex] TikZ: Can’t crop the bounding box

tikz-pgf

I've written this code for TikZ to make a sketch of the Stern Gerlach experiment. Since the weight of the image is determined by the drawing itself and not by the annotations (the nodes), I thought I could use [use as bounding box] so that the \centering command will center the main body of my drawing instead of the drawing and all it's annotations. (I want the nodes to be ignored when centering)

I've drawn a box around what I would like to see centered and I used the [use as bounding box]
command to make sure this will be the bounding box. Something is amiss it seems as the figure is still centered considering the nodes.

This is very frustrating, since I've tried out the [use as bounding box] command in a much simpler drawing to see if it actually works (and it does!).

here's my code:

 \documentclass[a4paper]{article}

 \usepackage{tikz}
 \usepackage{tikz-3dplot}

 \tikzset{
  vlak/.style={fill=lightgray!80,draw=none,opacity=1},
  obj/.style={fill=lightgray,draw=none},
  opp/.style={fill=lightgray,opacity=0.6,draw=none},
  rand/.style={thick,draw=gray},
  vector/.style={>=stealth',draw=black,line width=0.55pt}
  }



 \begin{document}

 \tdplotsetmaincoords{75}{140}

 \begin{figure}[htp]
 \centering
 \begin{tikzpicture}[tdplot_main_coords,scale=0.7]
  \newcommand{\nodin}[1]{coordinate(#1)node{#1}}%draw nodes on coordinates, I alter           
                                                   %this later to hide the nodes


  \draw[vlak,rand](0,-2,0)--(0,2,0)\nodin{A}--(0,2,2)\nodin{B}--(0,1,2)\nodin{C}--(0,1,1)\nodin{Q}--(0,-1,1)\nodin{D}--(0,-1,2)\nodin{E}--(0,-2,2)\nodin{F}--cycle;

  \draw[vlak,rand](F)++(-6,0,0)--(F)--(E)--++(-6,0,0)--cycle;
  \draw[vlak,rand](D)++(-6,0,0)--(D)--(E)--++(-6,0,0)--cycle;

  \draw[vlak,rand](0,-1,3)--(0,0,2)\nodin{G}--(0,1,3)\nodin{H}--(0,1,4)\nodin{I}--(0,-1,4)\nodin{J}--cycle;
  \draw[vlak,rand](G)++(-6,0,0)--(G)--(H)--++(-6,0,0)--cycle;
  \draw[vlak,rand](H)++(-6,0,0)--(H)--(I)--++(-6,0,0)--cycle;
  \draw[vlak,rand](J)++(-6,0,0)--(J)--(I)--++(-6,0,0)--cycle;
  \draw[vlak,rand](Q)++(-6,0,0)--(Q)--(D)--++(-6,0,0)--cycle;

  \foreach \x in{0,2,4,6}{
   \draw[vector,->] (-\x,0,2)--(-\x,1,1);
   \draw[vector,->] (-\x,0,2)--(-\x,0.5,1);
   \draw[vector,->] (-\x,0,2)--(-\x,0,1);
   \draw[vector,->] (-\x,0,2)--(-\x,-0.5,1);
   \draw[vector,->] (-\x,0,2)--(-\x,-1,1);
   }


  \draw[vlak,rand](0,-2,0)--(0,2,0)\nodin{A}--(0,2,2)\nodin{B}--(0,1,2)\nodin{C}--(0,1,1)\nodin{Q}--(0,-1,1)\nodin{D}--(0,-1,2)\nodin{E}--(0,-2,2)\nodin{F}--cycle;

  \draw(-9,0,1.8)\nodin{R};


  \draw[vlak,rand](R)++(0,-0.5,-0.5)--++(0,0,1)\nodin{R3}--++(0,1,0)\nodin{R2}--++(0,0,-1)\nodin{R1}--cycle;
  \draw[vlak,rand](R1)++(-1,0,0)--(R1)--(R2)--++(-1,0,0)--cycle;
  \draw[vlak,rand](R3)++(-1,0,0)--(R3)--(R2)--++(-1,0,0)--cycle;

  \tdplotsetrotatedcoords{0}{90}{0}
  \tdplotdrawarc[tdplot_rotated_coords,fill=black,rand]{(R)}{0.1}{0}{360}{}{}
  \draw[thick](R)--(-5,0,1.8)\nodin{M};

  \draw[thick](M)--(5,0,2)\nodin{M1};
  \draw[thick](M)--(5,0,1.6)\nodin{M2};

  \draw[vlak,rand](C)++(-6,0,0)--(C)--(B)--++(-6,0,0)--cycle;
  \draw[vlak,rand](A)++(-6,0,0)--(A)--(B)--++(-6,0,0)--cycle;


  \draw(5,0,0)\nodin{S};
  \draw[opp,rand](S)--++(0,2,0)--++(0,0,3.6)--++(0,-4,0)--++(0,0,-3.6)--cycle;

  \draw(5,0,1.8)\nodin{K};

  \foreach \x in{0.025,0.05,...,1}{
   \tdplotdrawarc[tdplot_rotated_coords,fill=black!80,opacity=0.15*\x,draw=none]{(K)}{1-     \x}{0}{360}{}{}
   }

  \tdplotdrawarc[tdplot_rotated_coords,fill=black]{(M1)}{0.03}{0}{360}{}{}
  \tdplotdrawarc[tdplot_rotated_coords,fill=black]{(M2)}{0.03}{0}{360}{}{}
  \draw[->](S)++(3,0,4)node[left]{klassiek}to[out=0,in=130](5,-0.6,2.4);

  \draw[->,shorten >=0.8pt](S)++(3,0,2)node[left]{kwantummechanisch}to[out=0,in=150](M1);
  \draw[->,shorten >=0.8pt](S)++(3,0,2)to[out=0,in=220](M2);

  \draw(R1)++(-1,0,2.5)\nodin{R4};
  \draw(S)++(2,0,0)\nodin{S1};

  \draw[use as bounding box](S1)rectangle(R4);

 \end{tikzpicture}\caption{Het Stern-Gerlach Experiment}\label{}\end{figure}
 \end{document}

 \tikzend{}{}
 \end{document}

any help as to why this happens would be appreciated.

Best Answer

You need to set the bounding box first. The bounding box in a TikZ/PGF picture can only be increased, never shrunk. So when you use the use as bounding box option then that says "Ignore all following constructions when computing the bounding box.", it has no effect on any previous components.