TransWikia.com

How to add finishing to a zig-zag line in tikzpicture

TeX - LaTeX Asked on August 31, 2021

Consider the following MWE

documentclass{article}
usepackage{tikz}
usetikzlibrary{decorations.pathmorphing}
begin{document}
    begin{center}
        begin{tikzpicture}
        draw[thick] (3,-0.5)--(3,-0.8)--(0.2,-0.8)--(0.2,0.8)--(3,0.8)--(3,0.5);
        draw[thick,decoration={zigzag,segment length=2mm}] decorate{(3,-0.5)--(3,0.5)};
        end{tikzpicture}
    end{center}
end{document}

This is the output shown by this code:

Note that the point where the zigzag and the straight lines combine doesn’t have nice finishing, as shown in this figure.

How to achieve the correct finishing??

2 Answers

Splitting a draw command into two is not a good idea. Try like this:

begin{tikzpicture}
draw[thick] (3,-0.8)--(0.2,-0.8)--(0.2,0.8)--(3,0.8)--(3,0.5) decorate [decoration={zigzag,segment length=2mm}] { -- (3,-0.5)} -- cycle;
end{tikzpicture}

Result (zoomed in) : result

Correct answer by domperor on August 31, 2021

Your figure is composed of two paths and is therefore not closed, which is why there is this discontinuity. The idea is to make a single closed path and using the options pre=lineto,pre length=5pt,post=lineto,post length=5pt just give the coordinates of the four vertices of the rectangle.

screenshot

documentclass[tikz,border=5mm]{standalone}
usetikzlibrary{decorations.pathmorphing}
begin{document}

begin{tikzpicture}
draw[thick](3,-0.8)--(0.2,-0.8)--(0.2,0.8)--(3,0.8) 
        decorate[decoration={zigzag,segment length=2mm,pre=lineto,pre length=5pt,post=lineto,post length=5pt}] {--cycle};
end{tikzpicture}

end{document}

Answered by AndréC on August 31, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP