5

I wrote a TikZ pic for a simple database icon (first picture below). What I want is to hide the two edges that are behind overlaying boxes, similar to the second picture. I thought I could add a white fill to the boxes, but how do I do that?

enter image description here enter image description here

P.S.: this code is quite ugly and repetitive, feedback is always welcome.

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

If the code is "repetitive" that's a good indication that you should be using \foreach somewhere: the three circles are essentially the same, so you only need to write them once if you use a \foreach statement. This also makes the code clearer and easier to modify.

In terms of adding fill=white the reason why this does not work with the code in the question is that there is nothing to fill: you need to have a circular path. Naively, you could just splice some of your draw statements together, however, this won't work either because drawing with an edge does not change the "current coordinate" of the path, so you won't get a circuit. One way to get around this is to use to instead. Once you have done this then you can add fill=white to get what you want, namely:

enter image description here

Here is the full code:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.