TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
up vote 6 down vote accepted

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}
share|improve this answer
    
oops I forgot the first part of the path but it's the same idea. – percusse Feb 28 at 11:49
    
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 at 11:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.