7

I am trying to draw a diagram of a square wave. To do this easily I used a foreach loop to recreate a part several times. However, the flat line before and after the square wave pulse does not connect with a smooth transition, as shown on the left in the figure.

enter image description here

What I try to accomplish is the smooth connection as one would get when not using the foreach loop.

The code:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[line join=round, x=1pt, y=1pt, thick]
\draw (0,0) -- (30,0);
\foreach \x in {2,...,9}{
    \draw[yscale=2] (\x*15,0) -- (\x*15,50) -- (\x*15+7.5,50) 
    -- (\x*15+7.5,-50) -- (\x*15+15,-50) -- (\x*15+15, 0);
 }
\draw (150,0) -- (200,0);
\end{tikzpicture}
\end{document}

What is a proper way to get the result on the right in the figure above?

1

1 Answer 1

7

You can make it all in one path so that path is not broken.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[line join=round, x=1pt, y=1pt, thick]
\draw (0,0) -- (30,0);
    \draw[yscale=2] foreach \x in {2,...,9}{
(\x*15,0) -- (\x*15,50) -- (\x*15+7.5,50) 
    -- (\x*15+7.5,-50) -- (\x*15+15,-50) -- (\x*15+15, 0)
 }
 -- (200,0);
\end{tikzpicture}
\end{document}
2
  • 1
    oops I forgot the first part of the path but it's the same idea.
    – percusse
    Feb 28, 2016 at 11:49
  • 1
    Thanks, that was much more simple than I expected. I had tried this, but ending with --, and then starting with (..,..) on the new line, but that of course didn't work...
    – Douwe66
    Feb 28, 2016 at 11:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .