2

EDIT:

Juan have a great approach when the coordinates can be know since before drawing the whole picture. But sometimes there are complex paths with relative positions which may require that the coordinates are created in the middle of the path.

Original question:

How may I hide or not show a pic but still have it calculate the internal coordinates/nodes?

Here I draw blue lines from the origin to every internal coordinate of the pic. My current solution is by having the command opacity=0 for the scope of the picture:

Drawn

Not drawn

But it does not hold when any internal drawing of the pic has its opacity changed as it overrides the opacity=0 command:

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

Not drawn, opacity changed internally

\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}
10
  • For the original problem of remembering coordinates before they are defined, the tikzmark library can be used to write locations to the aux file that can be used on a subsequent run. Would that help at all? Aug 20, 2021 at 11:22
  • @AndrewStacey That might be something, could you provide an example as an answer? Aug 20, 2021 at 11:53
  • @AndrewStacey Or point me in the right direction Aug 20, 2021 at 14:36
  • I can do that. What's the purpose in remembering these coordinates? What are you using them for? Did this come out of another question on this site that you can point me to? Aug 20, 2021 at 15:14
  • @AndrewStacey Yes, it is about anchoring pics with their internal coordinates. I have searched a lot after an answer to the relevant question and thereby tried to find a solution myself. See my answer: tex.stackexchange.com/a/610872/93369 Aug 20, 2021 at 15:17

1 Answer 1

2

If I understand you correctly you can define the coordinates in your pic code first and then draw the pic (or not) inside an \ifnum command.

Something like:

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

which produces: enter image description here

6
  • I thought about that approach too, but realised that some times there are complex paths which may require one to create the coordinates while creating the path. Specially when using relative coordinates. Aug 20, 2021 at 10:27
  • @RobinHellmers in this case you can put the coordinates in a \path[draw=none]... coordinate (-point 1) ... and then draw or fill the desired lines or shapes inside the \ifnum. It requires to duplicate some code, which I don't like, but I don't know the real pic you need to draw Aug 20, 2021 at 10:34
  • Yes that is an option. I tried to have a MWE and also keep the question general for others to have use of it in the future. It might not exist any other better option, but I would like to keep the question open for a while in case of another option. Aug 20, 2021 at 10:37
  • As a side note, you probably know that you can save your paths and reuse them afterwards, which could be an improvement of the solution Juan provided.
    – SebGlav
    Aug 20, 2021 at 11:28
  • @SebGlav I'm actually not sure how to do that. Could you expand on that? Aug 20, 2021 at 14:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .