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}

  • 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? – Andrew Stacey Aug 20 '21 at 11:22
  • @AndrewStacey That might be something, could you provide an example as an answer? – Robin Hellmers Aug 20 '21 at 11:53
  • @AndrewStacey Or point me in the right direction – Robin Hellmers Aug 20 '21 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? – Andrew Stacey Aug 20 '21 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: https://tex.stackexchange.com/a/610872/93369 – Robin Hellmers Aug 20 '21 at 15:17
  • @AndrewStacey I updated my answer on the other question, which highlights some of the flaws of my previous answer. – Robin Hellmers Aug 20 '21 at 15:38
  • Ah, I wondered if it might be. I've bookmarked that question so that I can post a tikzmark answer. It actually felt like a useful enough addition to meld into the tikzmark library itself so I've added it to the code on github. There's an example at https://github.com/loopspace/tikzmark/blob/master/tests/piclocation.tex – Andrew Stacey Aug 20 '21 at 16:55
  • @AndrewStacey I just tried to compile your Github example, but it did not work. It doesn't recognise the keys wrap pic and pic anchor. Am I missing something? – Robin Hellmers Aug 20 '21 at 17:01
  • Did you use the version of tikzmark from github? Download tikzmark.dtx and run tex tikzmark.dtx to generate the latest version of the library. – Andrew Stacey Aug 20 '21 at 17:03
  • I've posted a tikzmark solution over at that question. – Andrew Stacey Aug 20 '21 at 17:41

1 Answers1

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

Juan Castaño
  • 28,426
  • 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. – Robin Hellmers Aug 20 '21 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 – Juan Castaño Aug 20 '21 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. – Robin Hellmers Aug 20 '21 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 '21 at 11:28
  • @SebGlav I'm actually not sure how to do that. Could you expand on that? – Robin Hellmers Aug 20 '21 at 14:49
  • @RobinHellmers My spath3 TikZ library can be used for saving and restoring paths. – Andrew Stacey Aug 20 '21 at 15:14