Nesting blochspheres in tikzpictures using the blochsphere package

nestingtikz-pgf

I am using the blochsphere package (see 1) and I want to nest it in another tikzpicture, to work with several blochspheres. The manual tells one to put "nested=true" in the settings for \begin{tikzpicture}. Unfortunately there are many errors, even when using the blochsphere example, given in the manual – which I use to provide a MWE:

\begin{tikzpicture}
\draw (0,0) node [fill=red]{
\begin{blochsphere}[radius=1.5 cm,tilt=15,rotation=-20,nested=true]
\drawBallGrid[style={opacity=0.3}]{30}{30}


\drawGreatCircle[style={dashed}]{-60}{0}{0}
\drawGreatCircle[style={dashed}]{60}{0}{0}

\drawRotationLeft[scale=1.3,style={red}]{-60}{0}{0}{15}
\drawRotationRight[scale=1.3,style={red}]{60}{0}{0}{15}

\node at (-0.8,1.9) {\textcolor{red}{\tiny $J_{12}(t)$}};
\node at (1.1,1.8) {\textcolor{red}{\tiny $J_{23}(t)$}};

\labelLatLon{up}{90}{0};
\labelLatLon{down}{-90}{90};
\node[above] at (up) {{\tiny $\left|1\right>$ }};
\node[below] at (down) {{\tiny $\left|0\right>$}};
\labelLatLon[labelmark=false]{d}{15}{90};
\node at (d) {\color{gray}\fontsize{0.15cm}{1em}\selectfont $60^\circ$};

\labelLatLon[labelmark=false]{d2}{5}{78};
\end{blochsphere}
}
\end{tikzpicture}

All I changed was the first and last two lines of code and the nested=true in the third line. One error is (e.g.)

! Undefined control sequence.\tmp ->\endgroup \draw[current plane,on layer=back,opacity=0.3] (\agamma -\... \drawBallGrid[style={opacity=0.3}]{30}{30}

In the case of my personal blochsphere I get those as well as other errors like missing number treated as zero etc.
Could someone help me make the blochsphere package work to nest in another tikzpicture?

Thank you!

Best Answer

There is an issue with using the nested=true key on the blochsphere environment. In a normal blochsphere environment, the initialisation is done before the tikzpicture is started. With a nested one, the initialisation is done after the tikzpicture is started. For most of the relevant code, that doesn't matter. However, one part does need to be in place before the tikzpicture is started and that is the layers. So you need to have the following lines in your preamble:

\pgfdeclarelayer{back}%
\pgfdeclarelayer{front}%
\pgfsetlayers{back,main,front}%

Once this is in place, a blochsphere environment can be used directly inside a tikzpicture environment. It does not need to be inside a node (and, indeed, should not be used inside a node). But it can be included inside a scope, and that scope can be repositioned to change the location of the blochsphere. It may be that simple translations are sufficient, as in the following code, but if you want a more sophisticated positioning syntax then consider the development version of the tikzmark which allows you to define "anchors" on a scope in a similar fashion to nodes.

Full code follows. There were a few other minor issues which I've fixed in the code.

\documentclass{article}
%\url{https://tex.stackexchange.com/q/653567/86}
\usepackage{tikz}
\usepackage{blochsphere}

% These lines are needed when using the `nested=true` option
\pgfdeclarelayer{back}%
\pgfdeclarelayer{front}%
\pgfsetlayers{back,main,front}%

\begin{document}

\begin{tikzpicture}
\begin{scope}
\begin{blochsphere}[radius=1.5 cm,tilt=15,rotation=-20,nested=true]
% The next line is automatically included when not nesting
% but needs to be here when nesting
\drawBall
\drawBallGrid[style={draw,opacity=0.3}]{30}{30}

% These two commands only take 3 arguments (including the optional style)
\drawGreatCircle[style={dashed}]{-60}{0}%{0}
\drawGreatCircle[style={dashed}]{60}{0}%{0}

\drawRotationLeft[scale=1.3,style={red}]{-60}{0}{0}{15}
\drawRotationRight[scale=1.3,style={red}]{60}{0}{0}{15}

\node at (-0.8,1.9) {\textcolor{red}{\tiny $J_{12}(t)$}};
\node at (1.1,1.8) {\textcolor{red}{\tiny $J_{23}(t)$}};

% There were a few extra ;s in these lines
\labelLatLon{up}{90}{0}
\labelLatLon{down}{-90}{90}
\node[above] at (up) {{\tiny $\left|1\right>$ }};
\node[below] at (down) {{\tiny $\left|0\right>$}};
\labelLatLon[labelmark=false]{d}{15}{90}
\node at (d) {\color{gray}\fontsize{0.15cm}{1em}\selectfont $60^\circ$};

\labelLatLon[labelmark=false]{d2}{5}{78}

\end{blochsphere}
\end{scope}

\begin{scope}[shift={(4,0)}]
\begin{blochsphere}[radius=1.5 cm,tilt=15,rotation=-20,nested=true]
\drawBall
\drawBallGrid[style={draw,opacity=0.3}]{30}{30}

\drawGreatCircle[style={dashed}]{-60}{0}%{0}
\drawGreatCircle[style={dashed}]{60}{0}%{0}

\drawRotationLeft[scale=1.3,style={red}]{-60}{0}{0}{15}
\drawRotationRight[scale=1.3,style={red}]{60}{0}{0}{15}

\node at (-0.8,1.9) {\textcolor{red}{\tiny $J_{12}(t)$}};
\node at (1.1,1.8) {\textcolor{red}{\tiny $J_{23}(t)$}};

\labelLatLon{up}{90}{0}
\labelLatLon{down}{-90}{90}
\node[above] at (up) {{\tiny $\left|1\right>$ }};
\node[below] at (down) {{\tiny $\left|0\right>$}};
\labelLatLon[labelmark=false]{d}{15}{90}
\node at (d) {\color{gray}\fontsize{0.15cm}{1em}\selectfont $60^\circ$};

\labelLatLon[labelmark=false]{d2}{5}{78}

\end{blochsphere}
\end{scope}
\end{tikzpicture}
\end{document}

Two blochspheres inside a single tikzpicture

Related Question