# Tikz: positionning a \begin{scope}…\end{scope} relatively to the left of another block

I have two blocks of the form \begin{scope}...\end{scope}, and I would like to position the second block at a fixed distance from the rightmost border of the first block (for instance 1cm). But if you use xshift = cst for the second block, you position the second block relatively to the leftmost border of the first block. Is there an easy way to do that ?

Here is the comment regarding the suggested similar question with its two answers: the first answer does not work for some unknow reason: the second block get shifted both horizontally and vertically; the second answer works sometimes, but not always as demonstred by the following example where (1) the second box is shifted horizontally and vertically, and the scale is broken (see code and example at the end of this post

Here is an example where the xshift parameter of the second block is adjusted manually (rather than directly setting a distance of 1cm between the 2 blocks).

\documentclass[a4paper]{article}
\usepackage{pgfplots}
\usepackage{pifont}

\begin{document}

\def\argument#1{\mathtt{#1}}
\def\constraint#1{\textsc{#1}}

\begin{tikzpicture}[scale=0.7]
\begin{scope}[xshift=0cm,yshift=0cm]
\begin{axis}[axis equal image=true,xlabel={nb\_bump\_on\_decreasing\_sequence},
ylabel={nb\_decreasing},
title style={yshift=1.2ex},
title={$\argument{sv}=12$},
xtick ={0,2,...,2},
ytick ={0,2,...,10},
minor tick num=1,
grid=both,grid style={line width=.1pt, draw=gray!10},
major grid style={line width=.2pt,draw=gray!20},
xmin=-1,xmax=4,
ymin=-1,ymax=12,
extra description/.code={
\node[inner sep=-1.8pt,fill=white,circle] (description1) at (c1) {\large\ding{172}};
\node[inner sep=-1.8pt,fill=white,circle] (description2) at (c2) {\large\ding{173}};
\node[inner sep=-1.8pt,fill=white,circle] (description3) at (c3) {\large\ding{174}};
\node[inner sep=-1.8pt,fill=white,circle] (description4) at (c4) {\large\ding{175}};
}]

\addplot[blue!20,only marks]
coordinates {(0,0)(0,1)(0,2)(0,3)(0,4)(0,5)(0,6)(0,7)(0,8)(0,9)(0,10)(0,11)};
\addplot[blue!50,only marks]
coordinates {(1,4)(1,5)(1,6)(1,7)(1,8)(1,9)(1,10)(2,6)(2,7)(2,8)(2,9)(3,8)};
\addplot[red,only marks]
coordinates {(1,3)};
\addplot[blue!50,densely dashed]
coordinates {(0,0)(3,8)(0,11)(0,0)(0,0)};
\draw[fill=pink,opacity=0.1] (axis cs:-1.5,-1) -- (axis cs:5.0,12) -- (axis cs:5.0,12) -- (axis cs:5.0,-1) -- cycle;
\addplot[black!20,thick]
coordinates {(-1.5,-1)(5.0,12)};
\draw[fill=pink,opacity=0.1] (axis cs:-1,4) -- (axis cs:4,4) -- (axis cs:4,-1) -- (axis cs:-1,-1) -- cycle;
\addplot[black!20,thick]
coordinates {(-1,4)(4,4)};
\draw[fill=cyan,opacity=0.2] (axis cs:-1,12.0) -- (axis cs:4,7.0) -- (axis cs:4,12) -- (axis cs:-1,12) -- cycle;
\addplot[black!50,thick]
coordinates {(-1,12.0)(4,7.0)};
\addplot[orange]
coordinates {(1,3)(4,3)};
\addplot[orange,only marks,mark=otimes*]
coordinates {(1,3)};
\addplot[orange,only marks,mark=diamond*]
coordinates {(2,3)(3,3)};
\coordinate (c1) at (axis cs:4.6,10.0);
\coordinate (c2) at (axis cs:4.6,4);
\coordinate (c3) at (axis cs:4.6,7.0);
\coordinate (c4) at (axis cs:4.6,3);
\end{axis}
\end{scope}
\begin{scope}[xshift=7cm,yshift=0cm]
\begin{axis}[axis equal image=true,xlabel={nb\_bump\_on\_decreasing\_sequence},
ylabel={nb\_decreasing},
title style={yshift=1.2ex},
title={$\argument{sv}=11$},
xtick ={0,2,...,3},
ytick ={0,2,...,11},
minor tick num=1,
grid=both,grid style={line width=.1pt, draw=gray!10},
major grid style={line width=.2pt,draw=gray!20},
xmin=-1,xmax=3,
ymin=-1,ymax=11,
extra description/.code={
\node[inner sep=-1.8pt,fill=white,circle] (description1) at (c1) {\large\ding{172}};
\node[inner sep=-1.8pt,fill=white,circle] (description2) at (c2) {\large\ding{173}};
\node[inner sep=-1.8pt,fill=white,circle] (description3) at (c3) {\large\ding{174}};
\node[inner sep=-1.8pt,fill=white,circle] (description4) at (c4) {\large\ding{175}};
}]

\addplot[blue!20,only marks]
coordinates {(0,0)(0,1)(0,2)(0,3)(0,4)(0,5)(0,6)(0,7)(0,8)(0,9)(0,10)};
\addplot[blue!50,only marks]
coordinates {(1,4)(1,5)(1,6)(1,7)(1,8)(1,9)(2,6)(2,7)(2,8)};
\addplot[red,only marks]
coordinates {(1,3)};
\addplot[blue!50,densely dashed]
coordinates {(0,0)(2,6)(2,8)(0,10)(0,0)(0,0)};
\draw[fill=pink,opacity=0.1] (axis cs:-1.5,-1) -- (axis cs:4.5,11) -- (axis cs:4.5,11) -- (axis cs:4.5,-1) -- cycle;
\addplot[black!20,thick]
coordinates {(-1.5,-1)(4.5,11)};
\draw[fill=pink,opacity=0.1] (axis cs:-1,4) -- (axis cs:3,4) -- (axis cs:3,-1) -- (axis cs:-1,-1) -- cycle;
\addplot[black!20,thick]
coordinates {(-1,4)(3,4)};
\draw[fill=cyan,opacity=0.2] (axis cs:-1,11.0) -- (axis cs:3,7.0) -- (axis cs:3,11) -- (axis cs:-1,11) -- cycle;
\addplot[black!50,thick]
coordinates {(-1,11.0)(3,7.0)};
\addplot[orange]
coordinates {(1,3)(3,3)};
\addplot[orange,only marks,mark=otimes*]
coordinates {(1,3)};
\addplot[orange,only marks,mark=diamond*]
coordinates {(2,3)};
\coordinate (c1) at (axis cs:3.55,8.0);
\coordinate (c2) at (axis cs:3.55,4);
\coordinate (c3) at (axis cs:3.55,7.0);
\coordinate (c4) at (axis cs:3.55,3);
\end{axis}
\end{scope}
\end{tikzpicture}

\end{document}


EXAMPLE THAT DOES NOT WORK WITH SOLUTION SUGGESTED BY THE SIMILAR QUESTION

\documentclass[a4paper]{article}
\usepackage{pgfplots}
\usepackage{pifont}
\usetikzlibrary{calc}

\begin{document}

\def\argument#1{\mathtt{#1}}
\def\constraint#1{\textsc{#1}}

\begin{tikzpicture}[scale=0.7]
\node(scope1){
\begin{tikzpicture}
\begin{axis}[axis equal image=true,xlabel={nb\_bump\_on\_decreasing\_sequence},
ylabel={nb\_dip\_on\_increasing\_sequence},
title style={yshift=1.2ex},
title={$\argument{sv}=12$},
xtick ={0,2,...,2},
ytick ={0,2,...,2},
minor tick num=1,
grid=both,grid style={line width=.1pt, draw=gray!10},
major grid style={line width=.2pt,draw=gray!20},
xmin=-1,xmax=4,
ymin=-1,ymax=4,
extra description/.code={
\node[inner sep=-1.8pt,fill=white,circle] (description1) at (c1) {\large\ding{172}};
\node[inner sep=-1.8pt,fill=white,circle] (description2) at (c2) {\large\ding{173}};
\node[inner sep=-1.8pt,fill=white,circle] (description3) at (c3) {\large\ding{174}};
\node[inner sep=-1.8pt,fill=white,circle] (description4) at (c4) {\large\ding{175}};
\node[inner sep=-1.8pt,fill=white,circle] (description5) at (c5) {\large\ding{176}};
}
]

\addplot[blue!20,only marks]
coordinates {(0, 0)(0, 1)(0, 2)(0, 3)(1, 0)(2, 0)(3, 0)};
\addplot[blue!50,only marks]
coordinates {(1, 1)};
\addplot[red,only marks]
coordinates {(1, 2)(2, 1)};
\addplot[blue!50,densely dashed]
coordinates {(0, 0)(3, 0)(0, 3)(0, 0)(0, 0)};
\draw[fill=pink,opacity=0.1] (axis cs:-1,0.6666666666666666) -- (axis cs:4,2.3333333333333335) -- (axis cs:4,7) -- (axis cs:-1,7) -- cycle;
\addplot[black!20,thick]
coordinates {(-1, 0.6666666666666666)(4, 2.3333333333333335)};
\draw[fill=pink,opacity=0.1] (axis cs:0.6666666666666666,-1) -- (axis cs:2.3333333333333335,4) -- (axis cs:4.0,4) -- (axis cs:4.0,-1) -- cycle;
\addplot[black!20,thick]
coordinates {(0.6666666666666666, -1)(2.3333333333333335, 4)};
\draw[fill=pink,opacity=0.1] (axis cs:-1,3.3333333333333335) -- (axis cs:4,-1.6666666666666667) -- (axis cs:4,10) -- (axis cs:-1,10) -- cycle;
\addplot[black!20,thick]
coordinates {(-1, 3.3333333333333335)(4, -1.6666666666666667)};
\addplot[brown,only marks,mark=otimes*]
coordinates {(2, 1)};
\addplot[brown,only marks,mark=otimes*]
coordinates {(1, 2)};
\coordinate (c1) at (axis cs:4.2,2.3333333333333335);
\coordinate (c2) at (axis cs:2.3333333333333335,4.2);
\coordinate (c3) at (axis cs:3.333333333333334,-1.2);
\coordinate (c4) at (axis cs:2.2,1.2);
\coordinate (c5) at (axis cs:1.2,2.2);
\end{axis}
\end{tikzpicture}
};
\node[at={($(scope1.east)+(1cm,0)$)},anchor=west] (scope2){
\begin{tikzpicture}
\begin{axis}[axis equal image=true,xlabel={nb\_bump\_on\_decreasing\_sequence},
ylabel={nb\_dip\_on\_increasing\_sequence},
title style={yshift=1.2ex},
title={$\argument{sv}=11$},
xtick ={0,2,...,3},
ytick ={0,2,...,3},
minor tick num=1,
grid=both,grid style={line width=.1pt, draw=gray!10},
major grid style={line width=.2pt,draw=gray!20},
xmin=-1,xmax=3,
ymin=-1,ymax=3,
extra description/.code={
\node[inner sep=-1.8pt,fill=white,circle] (description1) at (c1) {\large\ding{172}};
\node[inner sep=-1.8pt,fill=white,circle] (description2) at (c2) {\large\ding{173}};
\node[inner sep=-1.8pt,fill=white,circle] (description3) at (c3) {\large\ding{174}};
}
]

\addplot[blue!20,only marks]
coordinates {(0, 0)(0, 1)(0, 2)(1, 0)(2, 0)};
\addplot[blue!50,only marks]
coordinates {(1, 1)};
\addplot[blue!50,densely dashed]
coordinates {(0, 0)(2, 0)(0, 2)(0, 0)(0, 0)};
\draw[fill=pink,opacity=0.1] (axis cs:-1,0.3333333333333333) -- (axis cs:3,1.6666666666666667) -- (axis cs:3,5) -- (axis cs:-1,5) -- cycle;
\addplot[black!20,thick]
coordinates {(-1, 0.3333333333333333)(3, 1.6666666666666667)};
\draw[fill=pink,opacity=0.1] (axis cs:0.3333333333333333,-1) -- (axis cs:1.6666666666666667,3) -- (axis cs:3.0,3) -- (axis cs:3.0,-1) -- cycle;
\addplot[black!20,thick]
coordinates {(0.3333333333333333, -1)(1.6666666666666667, 3)};
\draw[fill=pink,opacity=0.1] (axis cs:-1,3.0) -- (axis cs:3,-1.0) -- (axis cs:3,9) -- (axis cs:-1,9) -- cycle;
\addplot[black!20,thick]
coordinates {(-1, 3.0)(3, -1.0)};
\coordinate (c1) at (axis cs:3.15,1.6666666666666667);
\coordinate (c2) at (axis cs:1.6666666666666667,3.15);
\coordinate (c3) at (axis cs:3.15,-1.0);
\coordinate (c4) at (axis cs:-1,-1);
\coordinate (c5) at (axis cs:-1,-1);
\end{axis}
\end{tikzpicture}
};
\end{tikzpicture}

\end{document}


HOW TO EXTERNALISE THE PROPOSED SOLUTION?

I did not manange to externalise the figure of the proposed solution, maybe because of the \sbox or because of the multiple tikzpicture. I proceed as follows:

1. put the first example in fig1.tex

\tikzexternalenable\tikzsetnextfilename{ext_fig1} \sbox\leftplot{ \begin{tikzpicture}[scale=0.7] ... {\usebox\rightplot}; \end{tikzpicture} \tikzexternaldisable

2. put the second example if fig2.tex

3. create two dummy files file_fig1.tex and file_fig2.tex

4. create a final file file_fig12.tex

5. compile using the following commands:

pdflatex -shell-escape file_fig1.tex

pdflatex -shell-escape file_fig2.tex

pdflatex file_fig12.tex

I got a compilation error, while typing pdflatex -shell-escape file_fig1.tex

Now I give fig1.tex fig2.tex file_fig1.tex file_fig2.tex file_fig12.tex for which the externalisation works (just to say that it works on standard TikZ figures):

fig1.tex

\tikzexternalenable\tikzsetnextfilename{ext_fig1}
\begin{tikzpicture}
\filldraw[fill=pink, draw=black] (0,0) rectangle (3,2);
\end{tikzpicture}
\tikzexternaldisable


fig2.tex

\tikzexternalenable\tikzsetnextfilename{ext_fig2}
\begin{tikzpicture}
\filldraw[fill=cyan, draw=black] (0,0) rectangle (2,3);
\end{tikzpicture}
\tikzexternaldisable


file_fig1.tex

\documentclass[a4paper]{article}
\usepackage{pgfplots}
\usetikzlibrary{positioning,external}
\usepackage{pifont}
\newsavebox{\leftplot}
\newsavebox{\rightplot}
\tikzexternalize
\begin{document}
Dummy Document Containing Figure 1 for Getting Externalize TikZ Figures in a Fast Way
\input{fig1.tex}
\end{document}


file_fig2.tex

\documentclass[a4paper]{article}
\usepackage{pgfplots}
\usetikzlibrary{positioning,external}
\usepackage{pifont}
\newsavebox{\leftplot}
\newsavebox{\rightplot}
\tikzexternalize
\begin{document}
Dummy Document Containing Figure 2 for Getting Externalize TikZ Figures in a Fast Way
\input{fig2.tex}
\end{document}


file_fig12.tex

\documentclass[a4paper]{article}
\usepackage{pgfplots}
\usetikzlibrary{positioning,external}
\usepackage{pifont}
\newsavebox{\leftplot}
\newsavebox{\rightplot}
\tikzexternalize
\begin{document}
\tikzexternaldisable % by default disable

The first figure.

\input fig1.tex

The second figure

\input fig2.tex

\end{document}

• @BambOo Even though I suspect that a solution using local bounding box may be possible, I do not think that this question is a duplicate of the question you link. Rather, it is a nontrivial extension (IMHO) and would therefore vote against closing this question as a duplicate (which you did not suggest, but others may want to do seeing this link). – marmot May 29 '18 at 16:34
• @marmot I think I see what you mean, but then I guess the title and the beginning of the post do not agree ("relatively to the left" and "from the rightmost border" ) , right ? Also, I think I do not really get what the OP wants... I mean does he need an automatic way of placing two scope relatively to one another ? – BambOo May 29 '18 at 18:08
• @BambOo I think the OP wants to achieve the current output in a more suggestive way, and I think this is a very nice question and deserves a good answer. As the OP clarifies in his update, local bounding box does not work per se. I think one could rephrase his question as : " How can one position scopes with the same methods as one can position nodes?". That is, one may want to put one scope right=2cm of another scope. The OP's example is complex enough such that a simple path picture cheat does not work (at least for me). – marmot May 29 '18 at 18:16
• @marmot Ok now that's clearer to me. Thanks for the edit Nicolas Beldiceanu – BambOo May 30 '18 at 11:52
• @marmot Looking back at it, I agree with you this is an interesting question. I actually wondered how to do this too, but always stumbled upon the previous solutions. My apologies for the misjudgment. – BambOo May 30 '18 at 13:16

## 1 Answer

I have not really found a nice solution. But I have a proposal that at first sight nests tikzpictures. Well, it does nest tikzpictures, but in a safe way, I think. Let us recall why one should avoid nesting tikzpictures first: some options/directives of the ambient tikzpicture may screw up the embedded ones. However, as far as I can tell, this does not happen if one stores the tikzpictures in \saveboxes, and this is what I'm doing in this proposal.

\documentclass[a4paper]{article}
\usepackage{pgfplots}
\usetikzlibrary{positioning}
\usepackage{pifont}
\newsavebox{\leftplot}
\newsavebox{\rightplot}
\begin{document}

\def\argument#1{\mathtt{#1}}
\def\constraint#1{\textsc{#1}}

\sbox\leftplot{
\begin{tikzpicture}[scale=0.7]
\begin{axis}[axis equal image=true,xlabel={nb\_bump\_on\_decreasing\_sequence},
ylabel={nb\_decreasing},
title style={yshift=1.2ex},
title={$\argument{sv}=12$},
xtick ={0,2,...,2},
ytick ={0,2,...,10},
minor tick num=1,
grid=both,grid style={line width=.1pt, draw=gray!10},
major grid style={line width=.2pt,draw=gray!20},
xmin=-1,xmax=4,
ymin=-1,ymax=12,
extra description/.code={
\node[inner sep=-1.8pt,fill=white,circle] (description1) at (c1) {\large\ding{172}};
\node[inner sep=-1.8pt,fill=white,circle] (description2) at (c2) {\large\ding{173}};
\node[inner sep=-1.8pt,fill=white,circle] (description3) at (c3) {\large\ding{174}};
\node[inner sep=-1.8pt,fill=white,circle] (description4) at (c4) {\large\ding{175}};
}]

\addplot[blue!20,only marks]
coordinates {(0,0)(0,1)(0,2)(0,3)(0,4)(0,5)(0,6)(0,7)(0,8)(0,9)(0,10)(0,11)};
\addplot[blue!50,only marks]
coordinates {(1,4)(1,5)(1,6)(1,7)(1,8)(1,9)(1,10)(2,6)(2,7)(2,8)(2,9)(3,8)};
\addplot[red,only marks]
coordinates {(1,3)};
\addplot[blue!50,densely dashed]
coordinates {(0,0)(3,8)(0,11)(0,0)(0,0)};
\draw[fill=pink,opacity=0.1] (axis cs:-1.5,-1) -- (axis cs:5.0,12) -- (axis cs:5.0,12) -- (axis cs:5.0,-1) -- cycle;
\addplot[black!20,thick]
coordinates {(-1.5,-1)(5.0,12)};
\draw[fill=pink,opacity=0.1] (axis cs:-1,4) -- (axis cs:4,4) -- (axis cs:4,-1) -- (axis cs:-1,-1) -- cycle;
\addplot[black!20,thick]
coordinates {(-1,4)(4,4)};
\draw[fill=cyan,opacity=0.2] (axis cs:-1,12.0) -- (axis cs:4,7.0) -- (axis cs:4,12) -- (axis cs:-1,12) -- cycle;
\addplot[black!50,thick]
coordinates {(-1,12.0)(4,7.0)};
\addplot[orange]
coordinates {(1,3)(4,3)};
\addplot[orange,only marks,mark=otimes*]
coordinates {(1,3)};
\addplot[orange,only marks,mark=diamond*]
coordinates {(2,3)(3,3)};
\coordinate (c1) at (axis cs:4.6,10.0);
\coordinate (c2) at (axis cs:4.6,4);
\coordinate (c3) at (axis cs:4.6,7.0);
\coordinate (c4) at (axis cs:4.6,3);
\end{axis}
\end{tikzpicture}
}

\savebox\rightplot{
\begin{tikzpicture}[scale=0.7]
\begin{axis}[axis equal image=true,xlabel={nb\_bump\_on\_decreasing\_sequence},
ylabel={nb\_decreasing},
title style={yshift=1.2ex},
title={$\argument{sv}=11$},
xtick ={0,2,...,3},
ytick ={0,2,...,11},
minor tick num=1,
grid=both,grid style={line width=.1pt, draw=gray!10},
major grid style={line width=.2pt,draw=gray!20},
xmin=-1,xmax=3,
ymin=-1,ymax=11,
extra description/.code={
\node[inner sep=-1.8pt,fill=white,circle] (description1) at (c1) {\large\ding{172}};
\node[inner sep=-1.8pt,fill=white,circle] (description2) at (c2) {\large\ding{173}};
\node[inner sep=-1.8pt,fill=white,circle] (description3) at (c3) {\large\ding{174}};
\node[inner sep=-1.8pt,fill=white,circle] (description4) at (c4) {\large\ding{175}};
}]

\addplot[blue!20,only marks]
coordinates {(0,0)(0,1)(0,2)(0,3)(0,4)(0,5)(0,6)(0,7)(0,8)(0,9)(0,10)};
\addplot[blue!50,only marks]
coordinates {(1,4)(1,5)(1,6)(1,7)(1,8)(1,9)(2,6)(2,7)(2,8)};
\addplot[red,only marks]
coordinates {(1,3)};
\addplot[blue!50,densely dashed]
coordinates {(0,0)(2,6)(2,8)(0,10)(0,0)(0,0)};
\draw[fill=pink,opacity=0.1] (axis cs:-1.5,-1) -- (axis cs:4.5,11) -- (axis cs:4.5,11) -- (axis cs:4.5,-1) -- cycle;
\addplot[black!20,thick]
coordinates {(-1.5,-1)(4.5,11)};
\draw[fill=pink,opacity=0.1] (axis cs:-1,4) -- (axis cs:3,4) -- (axis cs:3,-1) -- (axis cs:-1,-1) -- cycle;
\addplot[black!20,thick]
coordinates {(-1,4)(3,4)};
\draw[fill=cyan,opacity=0.2] (axis cs:-1,11.0) -- (axis cs:3,7.0) -- (axis cs:3,11) -- (axis cs:-1,11) -- cycle;
\addplot[black!50,thick]
coordinates {(-1,11.0)(3,7.0)};
\addplot[orange]
coordinates {(1,3)(3,3)};
\addplot[orange,only marks,mark=otimes*]
coordinates {(1,3)};
\addplot[orange,only marks,mark=diamond*]
coordinates {(2,3)};
\coordinate (c1) at (axis cs:3.55,8.0);
\coordinate (c2) at (axis cs:3.55,4);
\coordinate (c3) at (axis cs:3.55,7.0);
\coordinate (c4) at (axis cs:3.55,3);
\end{axis}
\end{tikzpicture}
}
\begin{tikzpicture}
\node (left plot){\usebox\leftplot};
\node[right=2cm of left plot.east,anchor=west] (right plot){\usebox\rightplot};
\end{tikzpicture}

\end{document}


• Ah! so we can nest them. :) +1 – gusbrs May 30 '18 at 14:38
• Both examples work, but I don't manage to externalise the compilation of such figures. I will come back with some example showing the problem. Should I post the problem in this question (since it will be related to externalisation)? – Nicolas Beldiceanu May 30 '18 at 21:12
• It work for both figures, but I don't know how to externalise such tikzfigure with \sbox. I add in the post a small example how I externalise the compilation. – Nicolas Beldiceanu May 30 '18 at 21:28
• @NicolasBeldiceanu If you have additional information, please add it to your question. Please refrain from trying to add it to other peoples answers. – user36296 May 30 '18 at 22:18
• @NicolasBeldiceanu See the manual re. externalisation. Was this part of your original question? If not, it should be a follow-up. When TikZ externalises, all other tikzpictures are discarded. You can switch this off, I think, but it may result in longer compilation times, depending on the document. – cfr May 30 '18 at 23:33