圆角矩形,每边中间都有箭头

圆角矩形,每边中间都有箭头

我需要绘制一个圆角矩形,每条边的中间都有一个箭头。通过谷歌搜索,我找到了这个代码来获取“中间箭头”,但当我开始调试时,它返回一个错误(“尺寸太大 [...]”)

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{decorations.pathreplacing,decorations.markings}

\tikzset{
% style to apply some styles to each segment of a path
on each segment/.style={
    decorate,
    decoration={
        show path construction,
        moveto code={},
        lineto code={
            \path [#1]
            (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
        },
        curveto code={
            \path [#1] (\tikzinputsegmentfirst)
            .. controls
            (\tikzinputsegmentsupporta) and (\tikzinputsegmentsupportb)
            ..
            (\tikzinputsegmentlast);
        },
        closepath code={
            \path [#1]
            (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
        },
    },
},
% style to add an arrow in the middle of a path
mid arrow/.style={postaction={decorate,decoration={
            markings,
            mark=at position .5 with {\arrow[#1]{stealth}}
}}},
}

\begin{document}    
\begin{tikzpicture}
    \draw [thick, rounded corners=2pt, postaction={on each segment={mid 
    arrow=black}}] (0.1,0.1) -- (0.1,0.9) -- (0.9,0.9) -- (0.9,0.1) -- cycle;
\end{tikzpicture}
\end{document}

我设法通过增加矩形的尺寸解决了这个问题(我严格要求尺寸是那样的)。另一个问题是,在最终的图片中,我在角落也得到了箭头,就像这样

在此处输入图片描述

我只想获得像这样的一个矩形

在此处输入图片描述 在此处输入图片描述

感谢帮助!

答案1

问题是你给所有线段都加了箭头。你可能只想给直线段加箭头。

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{decorations.pathreplacing,decorations.markings}
\newcounter{flipflop}
\tikzset{
% style to apply some styles to each segment of a path
on each straight segment/.style={
    decorate,
    decoration={
        show path construction,
        moveto code={},
        lineto code={
            \path [#1]
            (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
        },
        curveto code={
            \path  (\tikzinputsegmentfirst)
            .. controls
            (\tikzinputsegmentsupporta) and (\tikzinputsegmentsupportb)
            ..
            (\tikzinputsegmentlast);
        },
        closepath code={
            \path 
            (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
        },
    },
},
set flipflop/.code=\setcounter{flipflop}{#1},
on each other straight segment/.style={
    decorate,
    decoration={
        show path construction,
        moveto code={},
        lineto code={\stepcounter{flipflop}
            \path \ifodd\value{flipflop} [#1]\fi
            (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
        },
        curveto code={
            \path  (\tikzinputsegmentfirst)
            .. controls
            (\tikzinputsegmentsupporta) and (\tikzinputsegmentsupportb)
            ..
            (\tikzinputsegmentlast);
        },
        closepath code={
            \path 
            (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
        },
    },
},
% style to add an arrow in the middle of a path
mid arrow/.style={postaction={decorate,decoration={
            markings,
            mark=at position .5 with {\arrow[#1]{stealth}}
}}},
}

\begin{document}    
\begin{tikzpicture}
    \draw [thick, rounded corners=2pt, postaction={on each straight segment={mid 
    arrow=black}}] (0.1,0.1) -- (0.1,0.9) -- (0.9,0.9) -- (0.9,0.1) -- cycle;
  \begin{scope}[xshift=1.5cm]
    \draw [thick, rounded corners=2pt, postaction={set flipflop=0,on each other straight segment={mid 
    arrow=black}}] (0.1,0.1) -- (0.1,0.9) -- (0.9,0.9) -- (0.9,0.1) -- cycle;
  \end{scope}
  \begin{scope}[xshift=3cm]
    \draw [thick, rounded corners=2pt, postaction={set flipflop=1,on each other straight segment={mid 
    arrow=black}}] (0.1,0.1) -- (0.1,0.9) -- (0.9,0.9) -- (0.9,0.1) -- cycle;
  \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

还有一个建议:

\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{shapes.arrows}
\tikzset{
myarrow/.style={
  draw,thick,
  single arrow,
  text width=16pt,
%  fill=white
  },
}

\begin{document}
\begin{tikzpicture}[arrowed/.style={inner sep=6pt,path picture={
\draw[-latex,#1] ([xshift=2pt,yshift=-2pt]path picture bounding box.north west)
-- ([xshift=-2pt,yshift=-2pt]path picture bounding box.north east);
\draw[-latex,#1] ([xshift=-2pt,yshift=-2pt]path picture bounding box.north east)
-- ([xshift=-2pt,yshift=2pt]path picture bounding box.south east);
\draw[-latex,#1] ([xshift=-2pt,yshift=2pt]path picture bounding box.south east)
-- ([xshift=2pt,yshift=2pt]path picture bounding box.south west);
\draw[-latex,#1] ([xshift=2pt,yshift=-2pt]path picture bounding box.north west)
-- ([xshift=-2pt,yshift=-2pt]path picture bounding box.north east);
\draw[-latex,#1] ([xshift=2pt,yshift=2pt]path picture bounding box.south west)
-- ([xshift=2pt,yshift=-2pt]path picture bounding box.north west);
}}]
\node[arrowed] at (0,0) {some text};
\end{tikzpicture}

\vspace{2pc}

 \begin{tikzpicture}
   \node [draw,rectangle,minimum size=8pc](r){};
   \node[myarrow,rotate=90] at (r.west){};
   \node[myarrow] at (r.north){};
 \end{tikzpicture}%

\end{document}

在此处输入图片描述

相关内容