Tikz:矩形边框中的箭头

Tikz:矩形边框中的箭头

我怎样才能像上图一样在虚线矩形边框中放置箭头?谢谢

在此处输入图片描述

我有这个代码:

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

在此处输入图片描述

答案1

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

在此处输入图片描述

答案2

使用节点代替矩形:

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

在此处输入图片描述

相关内容