2

I have several TikZ pictures of different sizes, and I need to use the resizebox function to convert them to the same size and put them into a single figure by subfigure. I also want to add some edges between these subfigures.

The image I expect to get is as follows:

Here is my current approach.

I use \tikz[remember picture] \node () {...}; as mentioned in Draw over mutliple subfigures [duplicate]. The resulting picture is like:

Then I add the resizebox function, but the result is not as expected. It seems that the original positions of the nodes are remembered instead of the resized ones.

How to properly draw over multiple subfigures after resizebox? Any help would be greatly appreciated!

Below is my code:

\documentclass[preview]{standalone}
\usepackage{caption, subcaption}  % for subfigure
\usepackage{tikz}

\begin{document}

\begin{figure} \centering

\begin{subfigure}[b]{0.3\textwidth} \centering \tikz[remember picture] \node[draw, dashed] (9) {\resizebox{0.9\linewidth}{!}{\begin{tikzpicture} \foreach \i in {0,...,9} {\node[draw] (9-\i) at (\i, \i) {\i};} \end{tikzpicture}}}; \caption{9 boxes} \end{subfigure} \hfill \begin{subfigure}[b]{0.3\textwidth} \centering \tikz[remember picture] \node[draw, dashed] (6) {\resizebox{0.9\linewidth}{!}{\begin{tikzpicture} \foreach \i in {0,...,6} {\node[draw] (6-\i) at (-\i,\i) {\i};} \end{tikzpicture}}}; \caption{6 boxes} \end{subfigure} \hfill \begin{subfigure}[b]{0.3\textwidth} \centering \tikz[remember picture] \node[draw, dashed] (3) {\resizebox{0.9\linewidth}{!}{\begin{tikzpicture} \foreach \i in {0,...,3} {\node[draw] (3-\i) at (\i,\i) {\i};} \end{tikzpicture}}}; \caption{3 boxes}

\begin{tikzpicture}[overlay, remember picture] \draw (9-9.north east) -- (6.north west); \draw (9-9.south east) -- (6.south west); \draw (6-3.north east) -- (3.north west); \draw (6-3.south east) -- (3.south west); \end{tikzpicture}

\end{subfigure} \end{figure}

\end{document}

Carlos
  • 31
  • TikZ doesn't seem to know anything about the outer \resizebox and uses the original placement. Do you want to actually resize your individual figures or do you just want to reduce the distance between nodes? Maybe, the spy library is a better approach to your diagram. – Qrrbrbirlbel Mar 22 '23 at 12:25

2 Answers2

1

Nesting TikZ pictures is a dangerous thing. Let's try to avoid this.

For this, I'm using the experimental ext.scalepicture library from my tikz-ext package together with transform shape. Instead of a picture inside a node, we will just fit a dashed node around the bounding box at the end of the picture.

I'm sure with a bunch of math we could compute the appropriate scaling but the library does it by remembering the last picture size via the AUX file.

Code

\documentclass{article}
\usepackage{caption, subcaption}  % for subfigure
\usepackage{tikz}
\usetikzlibrary{ext.scalepicture, fit}
\newcommand*\tikznnodes[2][]{%
  \tikz[
    picture width=.9\linewidth, transform shape, remember picture,
    execute at end picture={%
      \node[draw,dashed,outer sep=+0pt,
        fit=(current bounding box)](#2){};},#1]
    \foreach \i in {0,...,#2}
      \node[draw] (#2-\i) at (\i, \i) {\i};}
\begin{document}

\begin{figure} \centering \begin{subfigure}[t]{0.3\textwidth} \centering \tikznnodes{9} \caption{9 boxes} \end{subfigure} \hfill \begin{subfigure}[t]{0.3\textwidth} \centering \tikznnodes{6} \caption{6 boxes} \end{subfigure} \hfill \begin{subfigure}[t]{0.3\textwidth} \centering \tikznnodes{3} \caption{3 boxes} \tikz[overlay, remember picture] \foreach \A/\B in {9-9/6, 6-3/3} \draw (\A.north east) -- (\B.north west) (\A.south east) -- (\B.south west); \end{subfigure} \end{figure} \end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821
0

This solution uses a \pic for the 'squares' that, like all \pics, is scalable. Then it creates a macro for the subfigures, only to reduce code.

\documentclass{article}
\usepackage{lipsum}     % dummy text
\usepackage{showframe}  % only tosee the page layout
\usepackage{subcaption}
\usepackage{tikz}

\tikzset {% pic that draws a square with #1 nodes (starting from 0) % if #1>0 draws them raising form left to right % if #1<0 draws them raising form right to left pics/my square/.style={ code={% \pgfmathtruncatemacro\myabs{abs(#1)} \pgfmathtruncatemacro\mysign{#1/\myabs} \begin{scope}[x=\mysign cm,shift={(0.5*\mysign-0.5,0)}] \draw[dashed] (0,0) rectangle (1,1); \foreach\i in {0,...,\myabs} \node[draw,dashed,inner sep=0,minimum size=3mm] (-\i) at ({(\i+0.5)/(1+\myabs)},{(\i+0.5)/(1+\myabs)}) {$\i$}; \end{scope} \coordinate (-sw) at (0,0); \coordinate (-se) at (1,0); \coordinate (-ne) at (1,1); \coordinate (-nw) at (0,1); }}, }

% A macro that draws the subfigures using the \pic (scaled) \NewDocumentCommand{\boxes}{mm} {% \begin{subfigure}[b]{0.3\textwidth} \tikz\pic[dashed,scale=\mysize] (#1) {my square=#2}; \caption{\pgfmathparse{int(abs(#2))}\pgfmathresult{} boxes} \end{subfigure}% }

\begin{document} \lipsum[1] \begin{figure}[ht] \centering \pgfmathsetmacro\mysize{0.30.0351459804\textwidth} % converts 0.3\textwidth to cm \tikzset{every picture/.style={remember picture}} \boxes{a}{9}% \hfill \boxes{b}{-6}% \hfill \boxes{c}{3}% \begin{tikzpicture}[overlay,red] \draw (b-nw) -- (a-9) -- (b-sw); \draw (c-nw) -- (b-3) -- (c-sw); \end{tikzpicture} \end{figure}

\lipsum[2] \end{document}

enter image description here

Juan Castaño
  • 28,426