我为一个简单的数据库图标编写了一个 TikZ pic
(下面第一张图片)。我想要隐藏覆盖框后面的两个边缘,类似于第二张图片。我想我可以为框添加白色填充,但我该怎么做呢?
PS:此代码相当丑陋且重复,欢迎反馈。
\documentclass{standalone}
\usepackage{tikz}
\tikzset{
db/.pic={
\draw[-,pic actions] (-0.3,0 ) edge[looseness=0.5,bend right=90] (0.3,0 );
\draw[-,pic actions] (-0.3,0 ) -- ++(0,0.2);
\draw[-,pic actions] ( 0.3,0 ) -- ++(0,0.2);
\draw[-,pic actions] (-0.3,0.2 ) edge[looseness=0.5,bend right=90] (0.3,0.2 );
\draw[-,pic actions] (-0.3,0.2 ) edge[looseness=0.5,bend left=90] (0.3,0.2 );
\draw[-,pic actions] (-0.3,0.25) edge[looseness=0.5,bend right=90] (0.3,0.25);
\draw[-,pic actions] (-0.3,0.25) -- ++(0,0.2);
\draw[-,pic actions] ( 0.3,0.25) -- ++(0,0.2);
\draw[-,pic actions] (-0.3,0.45) edge[looseness=0.5,bend right=90] (0.3,0.45);
\draw[-,pic actions] (-0.3,0.45) edge[looseness=0.5,bend left=90] (0.3,0.45);
\draw[-,pic actions] (-0.3,0.5 ) edge[looseness=0.5,bend right=90] (0.3,0.5 );
\draw[-,pic actions] (-0.3,0.5 ) -- ++(0,0.2);
\draw[-,pic actions] ( 0.3,0.5 ) -- ++(0,0.2);
\draw[-,pic actions] (-0.3,0.7 ) edge[looseness=0.5,bend right=90] (0.3,0.7 );
\draw[-,pic actions] (-0.3,0.7 ) edge[looseness=0.5,bend left=90] (0.3,0.7 );
}
}
\begin{document}
\begin{tikzpicture}
\pic {db};
\end{tikzpicture}
\end{document}
答案1
如果代码“重复”,这是一个很好的迹象,表明您应该使用\foreach
某个地方:这三个圆圈本质上是相同的,因此如果您使用语句,则只需编写一次\foreach
。这也使代码更清晰,更易于修改。
至于fill=white
为什么这在问题中的代码中不起作用,是因为没有东西可以填充:你需要有一条圆形路径。天真地说,你可以将一些绘制语句拼接在一起,但是,这也行不通,因为用绘制不会edge
改变路径的“当前坐标”,所以你不会得到一个电路。解决这个问题的一种方法是使用to
。完成此操作后,您可以添加fill=white
以获得您想要的内容,即:
完整代码如下:
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\tikzset{
db/.pic={
\foreach \y in {0,0.25,0.5} {
\draw[fill=white](-0.3,\y) to [looseness=0.5,bend right=90] ++(0.6,0)
-- ++(0,0.2) to [green,looseness=0.5,bend left=90] ++(-0.6,0)
-- ++(0,-0.2);
\draw(-0.3,\y+0.2) edge[looseness=0.5,bend left=90] ++(0.6,0);
}
}
}
\begin{document}
\begin{tikzpicture}
\pic {db};
\end{tikzpicture}
\end{document}