1

enter image description here

How can I change the left and the right line of a rectangle to 'zigzag'?

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

With two nodes and less explicit coordinations:

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

enter image description here

As Ross suggested, with line cap=round option in the \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);

the joins are better:

enter image description here

Another expert told me there is the possibility to decorate part of the path without using 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}

enter image description here

  • Nice. Maybe add line cap=round to get better joins. – Ross Jul 16 at 4:34
  • @Ross Added a note, thanks! – CarLaTeX Jul 16 at 4:44
2

Try to repeat some coordinates:

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

enter image description here

  • Ah ok, very nice. – cis Jul 15 at 23:32

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.