2

Imagine I have two quite unrelated scopes:

  • Scope 1: A geometric drawing with exact dimensions in millimeters and an overall scaling factor
  • Scope 2: A circuittikz drawing using the normal dimensionless grid

Now I would like to place both scopes on top of each other, but vertically and horizontally aligned. I tried to use the local bounding box of the first scope, but as the second scope does not have a mid/center anchor point I don't know how to align the two scopes.

Please assume:

  • The scaling factor of the first scope is arbitrary and should not be used
  • I do know the coordinate of the center point of the second scope

Do you know how I could shift the inner coordinate system of the second scope, so the origin lies in the center? Or is it somehow possible to determine the center of the second scope automatically and use it for alignment?

MWE

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\begin{scope}[scale=2, local bounding box = scope1]
\draw[fill=blue, opacity = 0.5] (0,0) rectangle (1,1);
\end{scope}

\begin{scope}[scale=1.5, shift={(scope1.center)}]
\draw[fill=red, opacity = 0.5] (0,0) rectangle (1,1);
\end{scope}

\end{tikzpicture}

\end{document}

What I get

enter image description here

What I want

enter image description here

Thank you very much for your help!

More complex MWE

Imagine I would like to center the resistor inside the rectangle:

\documentclass{article}

\usepackage{tikz}
\usepackage{circuitikz}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}

\begin{scope}[scale=1.5, local bounding box = scope1]
    % These are real dimensions of a more complex geometry
    \filldraw[thin, fill = gray!20!white] (0mm,0mm) 
    -- ++(0mm, 7mm) 
    -- ++(22mm, 0mm)  
    -- ++(0mm, -7mm) 
    -- cycle;
\end{scope}

\begin{scope}[scale=1.2]
    \draw
    (0,0) to [R=$R$] (2,0)
    ;
\end{scope}

\end{tikzpicture}

\end{document}

2 Answers2

5

In order to shift the second drawing appropriately, you need to now its dimensions. One trick that allows you to position the second drawing where you want is to store it in a pic, and then use a matrix to position it because a matrix "measures" what's inside before drawing it.

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\begin{scope}[scale=2, local bounding box = scope1]
\draw[fill=blue, opacity = 0.5] (0,0) rectangle (1,1);
\end{scope}

\tikzset{pics/centered/.style={code={
\draw[fill=red, opacity = 0.5] (0,0) rectangle (1,1);
}}}

\matrix[at={(scope1.center)}] {
\pic[scale=1.5] {centered};\\
};

\end{tikzpicture}

\end{document}

enter image description here

There are limitations. For instance, the inner drawing cannot contain a matrix.

  • This actually works great, Very creative hack. But I assume it is limited to horizontal centering? (which would be fine for now, the vertical centering I can do manually) – Robert Seifert Nov 26 '19 at 07:02
  • @thewaywewalk No, AFAIK it is not limited to vertical centering. IMHO its main limitation is that you cannot nest matrices (yet?), so the second thingy cannot contain a matrix. –  Nov 26 '19 at 07:04
  • I just tried it with my extended MWE with (4,4) to [R=$R$] (6,4) and it got only horizontally centered. I was actually surprised that it worked also for this case, as the drawing does not start in the origin. – Robert Seifert Nov 26 '19 at 07:22
  • 1
    @thewaywewalk This is circuitikz, right? Yes, one of its authors told me that it does not always work with pics. I cannot say much for circuitikz, and should have specified that the above applies to TikZ and to some extent to pgfplots but not to circuitikz, which redefines certain things, nor forest. –  Nov 26 '19 at 07:28
3

Here's a solution using a recent update to tikzmark (link to github as it isn't yet on CTAN) which introduces code for positioning a scope at an internally defined anchor which is evaluated after the scope is constructed. It uses tikzmark to remember locations which means that it needs two complications to work.

\documentclass{article}
% \url{https://tex.stackexchange.com/q/517894/86}

\usepackage{tikz} \usetikzlibrary{tikzmark}

\begin{document}

\begin{tikzpicture}

\begin{scope}[scale=2, local bounding box = scope1] \draw[fill=blue, opacity = 0.5] (0,0) rectangle (1,1); \end{scope}

\begin{scope}[ scale=1.5, shift={(scope1.center)}, anchor=center, scope anchor, ] \draw[fill=red, opacity = 0.5] (0,0) rectangle (1,1); \end{scope}

\end{tikzpicture}

\end{document}

overlaid scopes

(Note: this is new code so I'm still ironing out some bugs - on the first compilation then it complains about coordinates but it works on the second.)

Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751
  • I think that the last option scope anchor is not a predefined one. Neither in tikz nor in tikzmark. – tush Apr 16 '23 at 06:42
  • @tush As the post says, this is available in the development version of tikzmark available on github, and the link is to that (see, for example, this commit: https://github.com/loopspace/tikzmark/commit/e10e3c0b2720d7476cf64d18fc07cf4f41952084). I then uploaded this version to ctan in October 2022 (https://ctan.org/ctan-ann/id/mailman.1586.1666903667.3715.ctan-ann@ctan.org). So if you have not updated your tex distribution since before then you won't have the right version, but updating should get it for you. – Andrew Stacey Apr 16 '23 at 07:27