2

I wish to use pgfdeclareshape draw a file archive shape.

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\makeatletter
\pgfdeclareshape{mrects}{
    \inheritsavedanchors[from=rectangle]
    \inheritanchorborder[from=rectangle]
    \inheritbackgroundpath[from=rectangle]
    \foreach \i in {center,north,south,east,west} {
        \inheritanchor[from=rectangle]{\i}
    }
    \anchor{text} {
        \pgfpoint{-0.5\wd\pgfnodeparttextbox}{-0.5\ht\pgfnodeparttextbox}
    }
    \backgroundpath{
        % store lower right in xa/ya and upper right in xb/yb
        \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
        \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
        % construct main path
        \pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
        \pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@yb}}
        \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yb}}
        \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@ya}}
        \pgfpathclose
        % my code
        \newdimen\pgf@xd
        \newdimen\pgf@yd
        \newdimen\pgf@xe
        \newdimen\pgf@ye
        \newdimen\pgf@xg
        \newdimen\pgf@yg
        \newdimen\pgf@xh
        \newdimen\pgf@yh
        %
        \pgf@xg=\pgf@xa
        \pgf@yg=\pgf@yb
        \pgf@xh=\pgf@xb
        \pgf@yh=\pgf@ya
        \foreach \i [evaluate=\i as \j using int(\i*2)] in {1,...,2} {
            \pgf@xd=\pgf@xg \advance\pgf@xd by \j pt
            \pgf@yd=\pgf@yg \advance\pgf@yd by \j pt
            \pgf@xe=\pgf@xh \advance\pgf@xe by \j pt
            \pgf@ye=\pgf@yh \advance\pgf@ye by \j pt
            % add line
            \pgfpathmoveto{\pgfpoint{\pgf@xd}{\pgf@yg}}
            \pgfpathlineto{\pgfpoint{\pgf@xd}{\pgf@yd}}
            \pgfpathlineto{\pgfpoint{\pgf@xe}{\pgf@yd}}
            \pgfpathlineto{\pgfpoint{\pgf@xe}{\pgf@ye}}
            \pgfpathlineto{\pgfpoint{\pgf@xh}{\pgf@ye}}
            \pgf@xg=\pgf@xd
            \pgf@yg=\pgf@yd
            \pgf@xh=\pgf@ye
            \pgf@yh=\pgf@ye 
        }   
    }
    \foregroundpath{
        \pgfusepath{stroke}
    }
}
\makeatother
\begin{document}
    \begin{tikzpicture}
    \node[draw,mrects,minimum width=6em,minimum height=2em] (N1) {hello world};
    \end{tikzpicture}
\end{document}

enter image description here

But two issues need your help:

  1. The text not at the center.
  2. The north west and south east corner lines crossed together.sounds line the assignment at the end of foreach loop doesn't work.
  • I would be very careful with using a \foreach loop in a shape declaration. If you use the thing in another foreach loop over \i you will get even more surprised than you already are. – marmot Jan 11 at 19:23
  • 1
    do you look 69.3.2 Copy Shadows in tikz & pgf manual, pp 757 (v 3.1)? there is described double copy shadow, which seem to do what you like to achieve ... – Zarko Jan 11 at 19:25
  • duplicate?: tex.stackexchange.com/questions/316455/… – Ignasi Jan 11 at 19:38
2

As Zarko pointed, a double copy shadow solves your problem:

\documentclass[border=2mm, tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{shadows}

\begin{document}
    \begin{tikzpicture}
    \node[double copy shadow,
        fill=white,
        draw=black,
        minimum width=6em,
        minimum height=2em] (N1) {Hello World};
    \end{tikzpicture}
\end{document}

enter image description here

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.