TikZ:如何装饰一个或多个矩形边?

TikZ:如何装饰一个或多个矩形边?

在此处输入图片描述

如何将矩形的左线和右线改为“之字形”?

\documentclass[margin=5pt, tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}

\begin{tikzpicture}[decoration={zigzag,segment length=0.4em, amplitude=.3mm}]
\draw[] (0,3) edge[decorate] (0,0) --+ (0.5,0) edge[decorate, red] (0.5,0) edge[-]+ (0,0);

\draw[xshift=2cm] (0,0) rectangle (0.5,3); 
\end{tikzpicture}

\end{document}

答案1

具有两个节点和不太明确的协调:

\documentclass[margin=5pt]{standalone}% if you load tikz afterward, no need to put it as documentclass option
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing, positioning}
\tikzset{mynode/.style={inner sep=0pt, minimum width=.5cm, minimum height=3cm}}

\begin{document}
\begin{tikzpicture}[decoration={zigzag,segment length=0.4em, amplitude=.3mm}]
\node[mynode] (A) {};
\draw (A.north west) -- (A.north east) edge[decorate] (A.south east) (A.south east) -- (A.south west) edge[decorate] (A.north west);
\node[mynode, right=1.5cm of A, draw]  {}; 
\end{tikzpicture}
\end{document}

在此处输入图片描述

正如 Ross 所建议的,line cap=round选择\draw

\draw[line cap=round] (A.north west) -- (A.north east) edge[decorate] (A.south east) (A.south east) -- (A.south west) edge[decorate] (A.north west);

连接更好:

在此处输入图片描述

另一位专家告诉我,可以不用edges 来装饰部分路径:

\documentclass[margin=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing, positioning}
\tikzset{mynode/.style={inner sep=0pt, minimum width=.5cm, minimum height=3cm}}

\begin{document}
    \begin{tikzpicture}[decoration={zigzag,segment length=0.4em, amplitude=.3mm}]
    \node[mynode] (A) {};
    \draw[line cap=round] (A.north west) -- (A.north east) decorate{-- (A.south east)} -- (A.south west) decorate{-- cycle};
    \node[mynode, right=1.5cm of A, draw]  {}; 
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

尝试重复一些坐标:

\documentclass[margin=5pt, tikz]{standalone}

\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}

\begin{document}

\begin{tikzpicture}[decoration={zigzag,segment length=0.4em, amplitude=.3mm}]

\draw[]  (0,0)   edge [decorate] (0,3) (0,3) -- (0.5,3) (0.5,3) edge [decorate] (0.5,0) (0.5,0) -- (0,0);

\draw[xshift=2cm] (0,0) rectangle (0.5,3);

\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容