[Tex/LaTex] Scaling a tikz scope to the width of another tikz scope

scopingtikz-pgf

Is there a way to scale tikz scope to the width of another tikz scope?
I have an approach in which I put each of the scopes in an individual tikzpicture which is enclosed in a resizebox. This works fine as long as there are no lines to be drawn between coordinates in these respective scopes. If I draw a line between to coordinates (one coordinate in each scope) after rescaling, the coordinates are not in their original position anymore and the line is in a wrong position.

If I would be able to scale a scope with respect to another scope (or with respect to linewidth), I could put everything in one tikzpicture and would not have a problem with wrong coordinates.

I have a code example (unscaled and scaled):

Code:

\documentclass{article}

\usepackage{tikz}


\begin{document}


\begin{tikzpicture}[remember picture]
    \begin{scope}
        \node[circle, thick, minimum width = 3cm, color=blue, draw] (n1) {};
    \end{scope}
\end{tikzpicture}

\begin{tikzpicture}[remember picture]
    \begin{scope}
        \node[circle, thick, minimum width = 2cm, color=green, draw] (n2) {};
    \end{scope}
\end{tikzpicture}

\begin{tikzpicture}[overlay, remember picture]
    \path[thick, red, draw] (n1.east) edge (n2.east);
\end{tikzpicture}

\resizebox{0.5\textwidth}{!}{
    \begin{tikzpicture}[remember picture]
        \begin{scope}
            \node[circle, thick, minimum width = 3cm, color=blue, draw] (s1) {};
        \end{scope}
    \end{tikzpicture}
}

\resizebox{0.5\textwidth}{!}{
    \begin{tikzpicture}[remember picture]
        \begin{scope}
            \node[circle, thick, minimum width = 2cm, color=green, draw] (s2) {};
        \end{scope}
    \end{tikzpicture}
}

\begin{tikzpicture}[overlay, remember picture]
    \path[thick, red, draw] (s1.east) edge (s2.east);
\end{tikzpicture}

\end{document}

coordinates wrong after rescale

Best Answer

I seem to have found a solution that requires the use of fake nodes to get the original width of the later to be scaled scope or node. The width of the fake node(s) is used to calculate the ratio of the two scopes and then scale one of them to the width of the other.

Code:

\documentclass{article}

\usepackage{calc}


\usepackage{tikz}
\usetikzlibrary{calc}

\usepackage{pgf}

\newdimen\XCoord
\newdimen\YCoord
\newcommand*{\ExtractCoordinate}[1]{\path (#1); \pgfgetlastxy{\XCoord}{\YCoord};}%

\newlength{\firstscope}
\newlength{\secondscope}


\begin{document}


\resizebox{0.5\linewidth}{!}{
    \begin{tikzpicture}
        \begin{scope}
            \node[circle, thick, minimum width = 3cm, color=blue, draw] (t1) {};
        \end{scope}
        \coordinate (width1) at ($(t1.east)-(t1.west)$);
        \ExtractCoordinate{width1}
        \setlength{\firstscope}{\XCoord}

        % get fake node to determine width of scope
        \node[circle, thick, minimum width = 2cm, color=black!0, draw, opacity=0] (t2) {};
        \coordinate (width2) at ($(t2.east)-(t2.west)$);
        \ExtractCoordinate{width2}
        \setlength{\secondscope}{\XCoord}

        \pgfmathsetmacro{\ratio}{\firstscope/\secondscope}
        
        \begin{scope}[scale=\ratio, every node/.append style={scale=\ratio}, yshift=-70]
            \node[circle, thick, minimum width = 2cm, color=green, draw] (t2) {};
        \end{scope}

        \node at ($(t1)!0.5!(t2)$) {ratio=\ratio};

        \path[ultra thick, red, draw] (t1.east) edge (t2.east);
    \end{tikzpicture}
}

\end{document}

The result looks like this (with original ratio specified): One scope scaled to the width of another.