3

In a software diagram, I'd like to be able to represent multiple instances of an object by two rectangles with one of them slightly shifted. I recently learned about the pic style so I'm using it with two nodes to achieve what I want.

The following example does work, but I'd like to know if I can do the same without using the fill=white. My hack kind of assumes the entire picture background is white and that may not always hold. I'd like to be able to crop the unwanted picture part out.

\documentclass[crop,border=3pt]{standalone}

\usepackage{pgf,tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}[
    every node/.style={font=\sffamily\small},
    block/.style={
        rectangle, draw, align=center, minimum width=2cm,
        minimum height=1.4cm, font=\sffamily\small
    },
    multinode/.pic={
        \node [block] at (0.1,0.1) {};
        \node [block, \tikzpictextoptions, fill=white] at (0,0) {\tikzpictext};% <-- FILL WHITE
    }
]
    \node [block] (STG) at (2,5) {single\\instance};
    \pic [pic text={multi\\instance}] at (5,5) {multinode};
\end{tikzpicture}
\end{document}

The following images show first one version with the fill white hack, and then one version without it.

With fill white

Without fill white, notice the additional lines showing

Daniel
  • 429

2 Answers2

5

There's no need for pics, you can use a copy shadow or double copy shadow:

\documentclass[tikz, border=5mm]{standalone}

\usepackage{pgf,tikz}
\usetikzlibrary{positioning,shadows}

\begin{document}
\begin{tikzpicture}[
    every node/.style={font=\sffamily\small},
    block/.style={
        rectangle, draw, align=center, minimum width=2cm,
        minimum height=1.4cm, font=\sffamily\small
    },
    multinode/.style={block, copy shadow, fill=white},
    multimultinode/.style={block, double copy shadow, fill=white},
]
    \node [block] (STG) {single\\instance};
    \node [multinode, right = of STG] (Mul) {multi\\instance};
    \node [multimultinode, above= of Mul] (MMul) {multi\\instance};
\end{tikzpicture}
\end{document}

enter image description here

Ignasi
  • 136,588
3

Here is a suggestion without clipping. You could draw only the needed lines of the second rectangle.

\documentclass[border=3pt]{standalone}
\usepackage{tikz}
\pagecolor{orange!20}
\begin{document}
\begin{tikzpicture}[
    every node/.style={font=\sffamily\small},
    block/.style={
        rectangle, draw, align=center, minimum width=2cm,
        minimum height=1.4cm, font=\sffamily\small
    },
    multinode/.pic={
        \node [block, \tikzpictextoptions](n) at (0,0) {\tikzpictext};% <-- FILL WHITE
        \path [block] 
          ([shift={(.1,0)}]n.north west)
          --+(0,.1)
          -|([shift={(.1,.1)}]n.south east)
          --+(-.1,0)
        ;
    }
]
    \node [block] (STG) at (2,5) {single\\instance};
    \pic [pic text={multi\\instance}] at (5,5) {multinode};
\end{tikzpicture}
\end{document}

Result:

enter image description here


Or

enter image description here

with

\documentclass[border=3pt]{standalone}
\usepackage{tikz}
\pagecolor{orange!20}
\begin{document}
\begin{tikzpicture}[
    every node/.style={font=\sffamily\small},
    block/.style={
        rectangle, draw, align=center, minimum width=2cm,
        minimum height=1.4cm, font=\sffamily\small
    },
    pics/multinode/.style={code={
        \node [block, \tikzpictextoptions](n) at (0,0) {\tikzpictext};% <-- FILL WHITE
        \foreach[count=\j from 0] \i in {1,...,#1}
        \path [block] 
          ([shift={(.1*\i,.1*\j)}]n.north west)
          --+(0,.1)
          -|([shift={(.1*\i,.1*\i)}]n.south east)
          --+(-.1,0)
        ;
    }},
    pics/multinode/.default=1
]
    \node [block] (STG) at (2,5) {single\\instance};
    \pic [pic text={multi\\instance}] at (5,5) {multinode};
    \pic [pic text={multi\\instance}] at (8,5) {multinode=5};
\end{tikzpicture}
\end{document}
esdd
  • 85,675