多个 \pic 对象的水平对齐

多个 \pic 对象的水平对齐

我想将一些 tikz 绘图组合在一起,为此使用了pic。我还希望将这些图片当作普通节点使用,并绘制一些连接箭头。我怎样才能将图片沿其边界框的中心水平对齐?我想避免指定数字坐标。我的代码的精简版本如下。

似乎指定right = of X将图片边界框的(0,0)坐标置于该点,但我更希望将其置于(0, 0.5 * bounding box height)该点。我尝试输入一些anchor选项,但似乎没有任何效果——我猜在定位图片时图片边界框的大小是未知的。

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[
    inner sep=0mm,
    stack/.pic={
        \foreach \i in {#1}
            \draw [pic actions] (0+\i,0+\i) rectangle (1+\i,1+\i);
    },
    squares/.style={
        black,
        fill=orange!75,
    },
    edge/.style={
        ->,
        black,
        ultra thick,
        shorten <=1mm,
        shorten >=1mm,
    },
]
\pic [
    squares,
    local bounding box=A,
] {stack={0,0.2,...,1}};
\pic [
    squares,
    local bounding box=B,
    scale=0.5,
    right= of A,
] {stack={0,0.2,...,1}};
\pic [
    squares,
    local bounding box=C,
    scale=0.25,
    right= of B,
] {stack={0,0.2,...,1}};
\draw [edge] (A) -- (B);
\draw [edge] (B) -- (C);
\end{tikzpicture}
\end{document}

ABC

答案1

如果你需要在绘制之前计算某个对象的大小,则需要使用保存框。或者绘制两次。

使用保存箱时请注意替换[tikz]\usepackage{tikz}

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}

\newsavebox{\boxa}
\newsavebox{\boxb}
\newsavebox{\boxc}

\begin{document}

\savebox{\boxa}{\begin{tikzpicture}[
    inner sep=0mm,
    stack/.pic={
        \foreach \i in {#1}
            \draw [pic actions] (0+\i,0+\i) rectangle (1+\i,1+\i);
    },
    squares/.style={
        black,
        fill=orange!75,
    },
]
\pic [
    squares,
] {stack={0,0.2,...,1}};
\end{tikzpicture}}%
\savebox{\boxb}{\begin{tikzpicture}[
    inner sep=0mm,
    stack/.pic={
        \foreach \i in {#1}
            \draw [pic actions] (0+\i,0+\i) rectangle (1+\i,1+\i);
    },
    squares/.style={
        black,
        fill=orange!75,
    },
]
\pic [
    squares,
    scale=0.5,
] {stack={0,0.2,...,1}};
\end{tikzpicture}}%
\savebox{\boxc}{\begin{tikzpicture}[
    inner sep=0mm,
    stack/.pic={
        \foreach \i in {#1}
            \draw [pic actions] (0+\i,0+\i) rectangle (1+\i,1+\i);
    },
    squares/.style={
        black,
        fill=orange!75,
    },
]
\pic [
    squares,
    scale=0.25,
] {stack={0,0.2,...,1}};
\end{tikzpicture}}%
\begin{tikzpicture}[
    inner sep=0mm,
    edge/.style={
        ->,
        black,
        ultra thick,
        shorten <=1mm,
        shorten >=1mm,
    },
]
\node (A) {\usebox\boxa};
\node[right=of A] (B) {\usebox\boxb};
\node[right=of B] (C) {\usebox\boxc};

\draw [edge] (A) -- (B);
\draw [edge] (B) -- (C);
\end{tikzpicture}
\end{document}

答案2

事实证明,可以将 tikz 绘图(\tikz \pic ...在本例中)放置在 的“文本”部分内\node。tikz 可能已将节点的文本内容保存到框中(否则 tikz 怎么知道如何将节点锚定在其中心?),因此我们可以重复使用它以避免自己做\savebox舞蹈。

\documentclass[tikz,border=1mm]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[
    inner sep=0mm,
    stack/.pic={
        \foreach \i in {#1}
            \draw [pic actions] (0+\i,0+\i) rectangle (1+\i,1+\i);
    },
    squares/.style={
        fill=orange!75,
    },
    edge/.style={
        ->,
        ultra thick,
        shorten <=1mm,
        shorten >=1mm,
    },
]

\node (A) { \tikz \pic[squares] {stack={0,0.2,...,1}}; };
\node [right=of A] (B) { \tikz \pic[squares, scale=0.5] {stack={0,0.2,...,1}}; };
\node [right=of B] (C) { \tikz \pic[squares, scale=0.25] {stack={0,0.2,...,1}}; };
\draw [edge] (A) -- (B);
\draw [edge] (B) -- (C);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容