Everything is in the title...

I'm trying to draw a simple square wave , but I'm reading that tikz cannot draw the vertical line of the discontinuity...

Is there a way to overpass this ? PSTricks maybe ?

This is what I'm trying to represent :

what I'm trying to do

  • Seriously? I would be very surprised if tikz wasn't able to draw vertical lines... What might be true is that you will need to draw each segment separately (I unfortunately don't know tikz, otherwise I would propose an answer). – Xavier May 7 '13 at 19:07
  • 3
    ?? TikZ can certainly draw this. What might be trickier is to draw it as the graph of a function. A simple \draw (0,0) \foreach \k in {0,...,5} { let \n1={-2*mod(\k,2) + 1} in -- ++(0,\n1) -- ++(1,0)}; will draw the wave. – Loop Space May 7 '13 at 19:28
  • 1
    @KevinC True, but then I left it as a comment to show that it was possible and that I felt that the question needed making more precise to show where the problem lay, not to provide an answer. – Loop Space May 7 '13 at 19:35
  • 2
    A square wave doesn't have vertical lines. (No function does.) – Kaz May 7 '13 at 21:05
  • 1
    @Kaz Try with an oscilloscope hence the name wave :) – percusse May 7 '13 at 21:52

Without TikZ.

enter image description here


  • 3
    OP had pstricks tag With PSTricks would suffice rather than a negation. – texenthusiast May 7 '13 at 22:21

A suggestion with pgfplots:

x axis line style={-stealth},
y axis line style={-stealth},
title={Square wave},
ymax = 1.5,xmax=7.5,
axis lines*=center,
xlabel={Time $\rightarrow$},
xlabel near ticks,
ylabel near ticks]
\addplot+[thick,mark=none,const plot]
{(0,0) (0,1) (1,0) (2,1) (3,0) (4,1) (5,0) (6,1) (7,0)};


enter image description here


Here's one possibility using TikZ and a simple \foreach loop (I didn't drew the axes since the problem seemed to be the wave itself):



\foreach \ini [evaluate=\ini as \inieval using 2*\ini] in {0,...,6}
\draw[ultra thick,cyan] (\inieval,0) -- ++(0,1) -| (\inieval+1,0) -- (\inieval+2,0);


enter image description here

  • Is there a reason that you use evaluate instead of {0,2,...,12}? – Tobi May 8 '13 at 16:48
  • @Tobi I was going to give a more general approach, allowing to control the amplitude and the time interval of each pulse. Finally I went for a simple direct answer so, in this case, the use of evaluate seems superfluous; in the more general setting (that can easily be obtained with minor changes from my code) I had originally devised, it could be useful. – Gonzalo Medina May 8 '13 at 16:56
  • Just a naive question, if I would like to start from top line instead of fall, how to go about? I am just trying without any success(as I didn't understand your code) – L.K. Apr 26 '17 at 18:17
\def\func{ x T1 gt { 
      T1 yVal \tx@ScreenCoor 
      2 copy 4 2 roll L pop yVal L 
      /yVal yVal 0 gt {0}{yMax}ifelse def 
      /T1 T1 T0 add def } if yVal }

\psset{xAxisLabel=t, yAxisLabel=U}
  [ /yMax 1 def /T0 1 def /T1 T0 def /yVal yMax def ]{ \func }
  [ /yMax 0.75 def /T0 1.5 def /T1 T0 def /yVal yMax def ]{ \func }

enter image description here


Cheating with pgfplots (verticals are not true verticals :) but gets better with increased sample number).

Jake has brought me to my senses :)

title=Square Wave,xlabel={Time$\rightarrow$},ylabel=Amplitude]
\addplot+[thick,const plot, no marks,samples at={0,1,...,10}] {(mod(x,2)>0?0:1)};

enter image description here

  • 3
    No need to cheat: \addplot+[thick,const plot, no marks,samples at={0,1,...,10}] {(mod(x,2)>0?0:1)}; (with true vertical lines!) – Jake May 7 '13 at 23:19
  • @Jake /facepalm.... – percusse May 8 '13 at 16:44

Just for the (my) fun of it, a fairly automatic TikZ solution which also incorporates shifts and the duty factor:





    \draw[thick,-latex] (\myxlowborder,0) -- (\myxtopborder+0.5,0) node[right] {\myxlabel};
    \draw[thick,-latex] (0,\myminy) -- (0,\mymaxy+0.5) node[above] {\myylabel};
    \foreach \x in {\myxlowborder,...,\myxtopborder} \draw (\x,0.03) -- (\x,-0.03) node[below ] {\x};
    \foreach \y in {\myminy,...,\mymaxy} \draw (0.03,\y) -- (-0.03,\y) node[left] {\y};
    \clip (\myxlowborder,\myminy) rectangle (\myxtopborder,\mymaxy);
    \foreach \x in {\myminindex,...,\mymaxindex} \draw[red] (\x*\myperiod+\myxshift,\mylowlevel) -- (\x*\myperiod+\myxshift,\myhihlevel) -- (\x*\myperiod+\myxshift+\mydutyfactor*\myperiod,\myhihlevel) -- (\x*\myperiod+\myxshift+\mydutyfactor*\myperiod,\mylowlevel) -- (\x*\myperiod+\myxshift+\myperiod,\mylowlevel);



enter image description here


using gnuplottex package with -shell-escape enabled and gnuplot 4.4

enter image description here

% Code compiled with pdflatex engine via frozen texlive 2012 on Linux 
% Need "-shell-escape" enabled and gnuplot 4.4 
\usepackage{gnuplottex} % http://www.ctan.org/pkg/gnuplottex
# xy co-ordinates range
unset border   # border off
unset xtics    # remove xaxis tics
set ytics ("0" -1, "0.5" 0, "1" 1) nomirror  # remove rightside yaxis ticks
set samples 300  # number of sample points
set arrow from xmin,ymin to xmax,ymin linewidth 1.5 # x-axis 
set arrow from xmin+0.15,ymin to xmin+0.15,ymax+0.2 linewidth 1.5 # y-axis 
set arrow from -5,ymin-0.1 to -3,ymin-0.1 # Time arrow
set title 'Square wave'
set xlabel "Time" 
set ylabel "Amplitude"  
# based on "Other definitions" at http://en.wikipedia.org/wiki/Square_wave
plot [xmin:xmax] [ymin:ymax] sgn(sin(x)) linecolor 3 linewidth 3  notitle;
  • Use \usepackage[miktex]{gnuplottex} with -shell-escape enabled using pdflatex – texenthusiast Aug 23 '13 at 15:34

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.