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

  • 6
    ?? 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. Commented May 7, 2013 at 19:28
  • 2
    @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. Commented May 7, 2013 at 19:35
  • 5
    A square wave doesn't have vertical lines. (No function does.)
    – Kaz
    Commented May 7, 2013 at 21:05
  • 2
    @Kaz Try with an oscilloscope hence the name wave :)
    – percusse
    Commented May 7, 2013 at 21:52
  • 2
    @percusse My analog scope has a 1Khz square vave test signal point right on the front panel where you can attach a probe. All you see is is flat tops and bottoms.
    – Kaz
    Commented May 7, 2013 at 22:00

8 Answers 8


Without TikZ.

enter image description here


  • 3
    OP had pstricks tag With PSTricks would suffice rather than a negation. Commented May 7, 2013 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

  • 1
    Is there a reason that you use evaluate instead of {0,2,...,12}?
    – Tobi
    Commented May 8, 2013 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. Commented May 8, 2013 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.
    Commented Apr 26, 2017 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

  • 4
    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
    Commented May 7, 2013 at 23:19
  • @Jake /facepalm....
    – percusse
    Commented May 8, 2013 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;
  • 1
    Use \usepackage[miktex]{gnuplottex} with -shell-escape enabled using pdflatex Commented Aug 23, 2013 at 15:34

Using TikZ decorations (this example is from TikZ manual):


    \draw[decorate,decoration=example](0,0)-- (3,0);
    \draw[red,decorate,decoration=example] (0,0)to [out=45,in=135] (3,0);


You must log in to answer this question.

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