3

enter image description here

If I have three parts of paths and nodes in a tikzpicture: what is the best way to split that up with a foreach-loop into 3 tikzpictures, where only one part is showing?
Like:
enter image description here, enter image description here, enter image description here

\documentclass[margin=5pt, tikz]{standalone}
\usepackage{tikz}

\begin{document}
\foreach \part in {1,...,3}{
\begin{tikzpicture}[
mystyle/.style={->, thin, font=\sffamily\tiny, blue, text=black, inner sep=0.5pt, very near start},
]
% 1:
\draw[mystyle] (140:1.2) -- +(0.6,0.05) node[mystyle, above]{1};

% 2:
\draw[mystyle, purple] (163:0.9) -- +(0.9,0.05) node[mystyle, above]{2};

% 3: 
\draw[mystyle, red, rounded corners=3] (200:0.6) -- ++(-0.3,-0.3) -- ++(0.4,-0.4) node[mystyle, below]{3};
\node[fill=red, circle, minimum width=2mm] at (210:0.1) {};
\end{tikzpicture}
}
\end{document}
3
  • 2
    Idea: You could try to use the beamer class (also with standalone) and its overlay feature for this. Jun 3, 2020 at 13:35
  • 2
    This looks like the same problem as this one, doesn't it? I agree with @MartinScharrer that the simplest way would be to use beamer, or better a beamer standaloneframe (from the standalone package) combined with the overlay-beamer-styles library.
    – user194703
    Jun 3, 2020 at 13:52
  • 2
    @Schrödinger'scat you're so fast I don't even have time to quot my own question!!
    – JeT
    Jun 3, 2020 at 19:24

2 Answers 2

5

You can use the undocumented discard layer of PGF for this as described in tikz: construct a phantom path. I wrapped it into a nice environment (which can't be nested).

\documentclass[margin=5pt, tikz]{standalone}
\usepackage{tikz}

\let\showonlyend\relax

\newenvironment{showonly}[2]{%
\ifnum#1=#2\else%
\let\showonlyend\endpgfonlayer
\pgfonlayer{discard}\fi
}{%
\showonlyend
}

\pgfdeclarelayer{discard}

\begin{document}
\foreach \p in {1,...,3}{
\begin{tikzpicture}[
mystyle/.style={->, thin, font=\sffamily\tiny, blue, text=black, inner sep=0.5pt, very near start},
]
% 1:
\begin{showonly}{\p}{1}
\draw[mystyle] (140:1.2) -- +(0.6,0.05) node[mystyle, above]{1};
\end{showonly}

% 2:
\begin{showonly}{\p}{2}
\draw[mystyle, purple] (163:0.9) -- +(0.9,0.05) node[mystyle, above]{2};
\end{showonly}

% 3: 
\begin{showonly}{\p}{3}
\draw[mystyle, red, rounded corners=3] (200:0.6) -- ++(-0.3,-0.3) -- ++(0.4,-0.4) node[mystyle, below]{3};
\node[fill=red, circle, minimum width=2mm] at (210:0.1) {};
\end{showonly}

\end{tikzpicture}
}
\end{document}
2

You can use if:

\documentclass[margin=5pt, tikz]{standalone}
\usepackage{tikz}

\begin{document}
\foreach \part in {1,...,3}{
\begin{tikzpicture}[
mystyle/.style={->, thin, font=\sffamily\tiny, blue, text=black, inner sep=0.5pt, very near start},
]
% 1:
\ifnum\part=1
    \draw[mystyle] (140:1.2) -- +(0.6,0.05) node[mystyle, above]{1};
\fi

% 2:
\ifnum\part=2
    \draw[mystyle, purple] (163:0.9) -- +(0.9,0.05) node[mystyle, above]{2};
\fi

% 3: 
\ifnum\part=3
    \draw[mystyle, red, rounded corners=3] (200:0.6) -- ++(-0.3,-0.3) -- ++(0.4,-0.4) node[mystyle, below]{3};
    \node[fill=red, circle, minimum width=2mm] at (210:0.1) {};
\fi
\end{tikzpicture}
}
\end{document}

== EDIT ==

As @Martin Scharrer♦ says the answer above is not what wanted.
You can achieve that using opacity:

\documentclass[margin=5pt, tikz]{standalone}
\usepackage{tikz}

\begin{document}
\foreach \a/\b/\c in {1/0/0, 0/1/0, 0/0/1}{
\begin{tikzpicture}[
mystyle/.style={->, thin, font=\sffamily\tiny, blue, text=black, inner sep=0.5pt, very near start},
]
% 1:
\draw[mystyle, opacity=\a] (140:1.2) -- +(0.6,0.05) node[mystyle, above]{1};

% 2:
\draw[mystyle, purple, opacity=\b] (163:0.9) -- +(0.9,0.05) node[mystyle, above]{2};

% 3: 
\draw[mystyle, red, rounded corners=3, opacity=\c] (200:0.6) -- ++(-0.3,-0.3) -- ++(0.4,-0.4) node[mystyle, below]{3};
\node[fill=red, circle, minimum width=2mm, opacity=\c] at (210:0.1) {};
\end{tikzpicture}
}
\end{document}
4
  • 1
    This will simply skip the other parts and not reserve empty space, so that every picture only contains the current part. You could just create three separate picture environments instead to get the same result.I think the OP wants to have the parts not being displayed but the space still reserved. Jun 3, 2020 at 13:33
  • The 2nd MWE looks interesting. It should be possible to incorporate a simplification using a pgfmathsetmacro. And then only \foreach \part in {1,2,3}
    – cis
    Jun 3, 2020 at 15:48
  • PS: I used \pgfmathsetmacro\y{1 == \part ? 1 : 0} and then \draw[..., opacity=\y] ...
    – cis
    Jun 3, 2020 at 16:00
  • 1
    DPD, @cis: There might also be a way to use the 'use as bounding box' feature of TikZ/PGF together with discarding the draw path afterwards, so nothing is drawn. The opacity will actually draw something, but in a way that it is not visible. Jun 4, 2020 at 6:36

You must log in to answer this question.

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