7

How can I put arrows in the dashed rectangle borders like the picture above? Thanks

enter image description here

I have this code:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{arrows}

\begin{document}
    \begin{tikzpicture}[scale=1.5]    
    \draw [draw=black, fill=black] (2.25,1) rectangle (2.75,-2);
    \draw [draw=black, fill=gray!30] (0,0) rectangle (5,0.5);
    \draw [draw=black, fill=orange!60] (0,-0.4) rectangle (5,-0.6);    
    \draw [draw=black, fill=gray!30] (0,-1) rectangle (5,-1.5);
    \draw [draw=red, dashed, line width = 0.3mm] (0.25,0.25) rectangle (1,-1.25);
    \draw [draw=red, dashed, line width = 0.3mm] (1.25,0.25) rectangle (2,-1.25);
    \draw [draw=red, dashed, line width = 0.3mm] (3,0.25) rectangle (3.75,-1.25);
    \draw [draw=red, dashed, line width = 0.3mm] (4,0.25) rectangle (4.75,-1.25);
    \end{tikzpicture}
\end{document}

enter image description here

2 Answers 2

5
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows, arrows.meta, decorations.markings, shapes.geometric}

\tikzset{
    myarrows/.style={postaction={decorate,decoration={
                markings,
                mark=at position 0.1 with {\arrow{Triangle}},
                mark=at position 0.25 with {\arrow{Triangle}},
                mark=at position 0.61 with {\arrow{Triangle}},
                mark=at position 0.76 with {\arrow{Triangle}}
}}}}

\begin{document}
    \begin{tikzpicture}[scale=1.5]    
        \draw [draw=black, fill=black] (2.25,1) rectangle (2.75,-2);
        \draw [draw=black, fill=gray!30] (0,0) rectangle (5,0.5);
        \draw [draw=black, fill=orange!60] (0,-0.4) rectangle (5,-0.6);    
        \draw [draw=black, fill=gray!30] (0,-1) rectangle (5,-1.5);
        \draw [myarrows, draw=red, dashed, line width = 0.3mm] (0.25,0.25) rectangle (1,-1.25);
        \draw [myarrows, draw=red, dashed, line width = 0.3mm] (1.25,0.25) rectangle (2,-1.25);
        \draw [myarrows, draw=red, dashed, line width = 0.3mm] (3,0.25) rectangle (3.75,-1.25);
        \draw [myarrows, draw=red, dashed, line width = 0.3mm] (4,0.25) rectangle (4.75,-1.25);
    \end{tikzpicture}
\end{document}

enter image description here

4

With use of nodes instead rectangles:

\documentclass[border=3.141592mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, 
                decorations.markings,
                positioning
                }

\tikzset{
arr/.style = {decoration={markings,
        mark=at position 0.11 with {\arrow{Triangle}},
        mark=at position 0.26 with {\arrow{Triangle}},
        mark=at position 0.62 with {\arrow{Triangle}},
        mark=at position 0.77 with {\arrow{Triangle}}
                         },
        red, semithick, rounded corners=2.2pt, densely dashed, 
        postaction={decorate}},
box/.style = {draw, fill=#1, 
              minimum width=50mm, minimum height=8mm, inner sep=0mm,
              node contents={}},
box/.default = gray!30    
        }

\begin{document}
    \begin{tikzpicture}[node distance = 5mm]
        \draw [draw, fill=black] (-0.2,1) rectangle (0.2,-3.2);
\node (n1) [box];
\node (n2) [box=orange!60, minimum height=4mm, below=of n1];
\node (n3) [box, below=of n2] (0,-1);
%
\draw [arr] (-2.2,0) rectangle (-1.4,-2.25);
\draw [arr] (-1.2,0) rectangle (-0.4,-2.25);
\draw [arr] ( 0.4,0) rectangle ( 1.2,-2.25);
\draw [arr] ( 1.4,0) rectangle ( 2.2,-2.25);
    \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.