如果我在 tikzpicture 中有三个部分的路径和节点:最好的拆分方法是什么使用 foreach 循环 分成 3 张 tikzpictures,其中只有一部分显示?
例如:
,
,
\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}
答案1
您可以使用未记录的discard
PGF 层来实现此目的,如中所述tikz:构建幻影路径。我把它包装到一个不错的环境中(不能嵌套)。
\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
您可以使用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}
== 编辑 ==
正如@Martin Scharrer♦所说,上面的答案不是你想要的。
您可以使用不透明度来实现这一点:
\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}