调整框大小后绘制多个子图

调整框大小后绘制多个子图

我有几张不同大小的 TikZ 图片,我需要使用函数resizebox将它们转换为相同大小并通过 将它们放入单个图形中subfigure。 我还想在这些子图之间添加一些边。

我期望得到的图像如下:

这是我目前的方法。

\tikz[remember picture] \node () {...};使用绘制多个子图[重复]。生成的图片是这样的:

然后我添加了该resizebox函数,但结果并不像预期的那样。似乎记住的是节点的原始位置,而不是调整大小后的位置。

如何正确绘制多个子图resizebox?如能得到任何帮助我将不胜感激!

下面是我的代码:

\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}

答案1

嵌套 TikZ 图片是一件危险的事情。让我们尝试避免这种情况。

为此,我使用ext.scalepicture我的tikz-ext包裹连同transform shape。我们不会在节点内使用图片,而是fit在图片末尾的边界框周围使用虚线节点。

我确信通过大量的数学运算我们可以计算出适当的缩放比例,但是该库是通过 AUX 文件记住最后一张图片的大小来实现的。

代码

\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}

输出

在此处输入图片描述

答案2

此解决方案使用\pic表示“正方形”,与所有 一样\pics,它是可扩展的。然后它为子图创建一个宏,仅用于减少代码。

\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.3*0.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}

在此处输入图片描述

相关内容