TikZ:隐藏图片,同时仍计算内部坐标

TikZ:隐藏图片,同时仍计算内部坐标

编辑:

Juan 的方法很棒,因为在绘制整个图片之前就可以知道坐标。但有时存在具有相对位置的复杂路径,可能需要在路径中间创建坐标。

原始问题:

我怎样才能隐藏或不显示pic但仍让它计算内部坐标/节点?

这里我画了从原点到 的每个内部坐标的蓝线。我当前的解决方案是使用图片范围的pic命令:opacity=0

画

沒有绘制

但是,当任何内部绘图的pic不透明度发生变化时,它就不成立,因为它会覆盖opacity=0命令:

\fill[fill=yellow,opacity=0.5] (0,0) rectangle (-1,-1) coordinate (-point 2);

未绘制,不透明度内部改变

\documentclass[margin=1cm]{standalone}
\usepackage{tikz}

\tikzset
{
    draw it/.store in=\drawit,
    draw it=1,
    random symbols/.pic=
    {
        \begin{scope}[opacity=\drawit]
            \draw (1,-1) -- +(0,1) coordinate (-point 1);
            \fill[fill=yellow] (0,0) rectangle (-1,-1) coordinate (-point 2);
            \node[draw,minimum size=1cm] (-point 3) at (-1,1) {a};
            \path (0,0)
                -- ++(1,1)
                edge[red] coordinate[pos=1] (-point 4) +(-1,0);
        \end{scope}
    }
}

\begin{document}
\begin{tikzpicture}[x=2cm,y=2cm]

    \path[draw it=1] pic (thepic) at (0,0) {random symbols};

    \draw[blue,very thick] (0,0)
        edge (thepic-point 1)
        edge (thepic-point 2)
        edge (thepic-point 3)
        edge (thepic-point 4);

\end{tikzpicture}
\end{document}

答案1

如果我理解正确的话,您可以先在代码中定义坐标pic,然后在命令pic内绘制(或不绘制)\ifnum

就像是:

\documentclass[margin=1cm]{standalone}
\usepackage{tikz}

\tikzset
{
    draw it/.store in=\drawit,
    draw it=1,
    random symbols/.pic=
    {   % coordinates (and one node)
        \node[minimum size=1cm] (-aux) at (-1, 1) {};
        \coordinate (-point 1) at ( 1, 0);
        \coordinate (-point 2) at (-1,-1);
        \coordinate (-point 3) at (-aux.south east);
        \coordinate (-point 4) at ( 0, 1);
        % optional drawing
        \ifnum\drawit = 1
            \draw (1,-1) -- (-point 1);
            \fill[fill=yellow,opacity=0.5] (0,0) rectangle (-point 2);
            \node[draw,minimum size=1cm] at (-aux) {a};
            \path (0,0) -- (1,1) edge[red] (-point 4);
        \fi
    }
}

\begin{document}
\begin{tikzpicture}[x=2cm,y=2cm]

\foreach\i in {0,1}
{
    \path pic[draw it=\i] (thepic\i) at (3*\i,0) {random symbols};

    \draw[blue,very thick] (3*\i,0)
        edge (thepic\i-point 1)
        edge (thepic\i-point 2)
        edge (thepic\i-point 3)
        edge (thepic\i-point 4);
}
\end{tikzpicture}
\end{document}

生成结果: 在此处输入图片描述

相关内容