PsTricks
Sandeep Kumar
Shaheed Bhagat Singh College
(University of Delhi)
August 2, 2017
Sandeep Kumar (SBSC, DU) PsTricks August 2, 2017 1 / 23
Introduction
Introduction: PsTricks
LATEX has very limited capabilities in terms of drawing. If you want to
generate some graphs or pictures, then PSTricks package is an add on
over LATEX that can be used for this task.
Sandeep Kumar (SBSC, DU) PsTricks August 2, 2017 2 / 23
Introduction
Introduction: PsTricks
LATEX has very limited capabilities in terms of drawing. If you want to
generate some graphs or pictures, then PSTricks package is an add on
over LATEX that can be used for this task.
PSTricks is developed by Timothy van Zandt, is a package based on
PostScript that can be included in LATEX documents for drawing of
pictures and graphs.
Sandeep Kumar (SBSC, DU) PsTricks August 2, 2017 2 / 23
Introduction
Introduction: PsTricks
LATEX has very limited capabilities in terms of drawing. If you want to
generate some graphs or pictures, then PSTricks package is an add on
over LATEX that can be used for this task.
PSTricks is developed by Timothy van Zandt, is a package based on
PostScript that can be included in LATEX documents for drawing of
pictures and graphs.
PSTricks provides many in-built functions to draw mathematical
objects that makes it easy to use.
Sandeep Kumar (SBSC, DU) PsTricks August 2, 2017 2 / 23
Introduction
Program to Compile
Sandeep Kumar (SBSC, DU) PsTricks August 2, 2017 3 / 23
Introduction
Program to Compile
Because PSTricks is based on PostScript, we can not use the program
pdfLaTeX to compile.
You have to use the program dvips.
If you are using the editor TexMaker, set the run/build option as:
Options → Configure TexMaker → Quick Build →
LaTeX+dvips+ps2pdf+ViewPDF.
Sandeep Kumar (SBSC, DU) PsTricks August 2, 2017 3 / 23
Introduction
A Simple Example
Include the package ‘pstricks’ in the preamble of the document.
\ d o c u m e n t c l a s s [ 1 1 p t ] { book }
\ usepackage { p s t r i c k s }
\ b e g i n { document }
\ begin { p s p i c t u r e }(0 , 0)(3 ,3)
\ p s c i r c l e [ l i n e s t y l e =s o l i d ] ( 2 , 2 ) { 1 }
\ end { p s p i c t u r e }
\ end { document }
The command
‘\begin{pspicture}(m0,n0)(m1, n1)’
sets aside space in coordinate system with lower-left corner (m0, n0) (by
default (0, 0)) and upper-right corner (m1, n1).
Sandeep Kumar (SBSC, DU) PsTricks August 2, 2017 4 / 23
Introduction
General Syntax of Functions
Some general rules regarding the use of arguments/parameters.
The compulsory arguments are specified in curly braces { }.
The optional arguments/parameters are specified in square braces [ ].
Parenthesis ( ) are used for co-ordinates (separated by commas).
‘=’ (equal to) symbol is used to specify arguments/parameters value.
Observe the above rules for the command
‘\pscircle[linestyle=solid](2,2){1}’ that generates a circle with
center (2, 2), radius 1 units and with solid boundary.
Sandeep Kumar (SBSC, DU) PsTricks August 2, 2017 5 / 23
Graphic Objects
Grid
Sandeep Kumar (SBSC, DU) PsTricks August 2, 2017 6 / 23
Graphic Objects
Grid
PSTricks provides the macro ‘psgrid’ to generate a grid. The general syntax is:
\psgrid[par1=val1, par2=val2, ..., parn=valn](x0,y0)(x1, y1)(x2, y2)
where (x1, y1), (x2, y2) are the coordinates of opposite corners of the grid. The horizontal labels
are positioned at the positions (x0, ∗) and vertical labels at the positions (∗, y 0). If (x0, y 0) is
not specified then (x1, y 1) is used for labeling purpose.
3
2
\ begin { p s p i c t u r e }(0 , 0)(3 ,3)
\ psgrid (0 ,0)(3 ,3)
\ end { p s p i c t u r e } 1
0
0 1 2 3
Sandeep Kumar (SBSC, DU) PsTricks August 2, 2017 6 / 23
Graphic Objects
Grid
One can also customize a grid using different parameters and their possible values, which are
given in the following table:
Parameter Description Parameter Possible Values
gridcolor Color of grid lines red, green, gray, ... etc.
griddots Specify number of dots per division 1, 2, 3, ...
gridlabelcolor Specify color of grid labels red, green, blue, ...
subgriddiv Specify number of sub grid subdivisions 1, 2, 3, ...
gridlabels Size of labels 1pt, 2pt, 3pt, ...
gridwidth The width of grid lines/dots 1pt, 2pt, 3pt, ...
Sandeep Kumar (SBSC, DU) PsTricks August 2, 2017 7 / 23
Graphic Objects
Dots
Sandeep Kumar (SBSC, DU) PsTricks August 2, 2017 8 / 23
Graphic Objects
Dots
PSTricks provides the macro \psdot to draw dots of different types. The general syntax for this
command is:
\psdot[par1=val1, par2=val2, ..., par3=val3](x1, y1)(x2, y2), ... ,(x3, y3)
where par1, par2,..., par3 and val1, val2, ..., val3 specify parameter names and their
corresponding values. The pairs (x1, y1)(x2, y2) ... (x3, y3) specifies the coordinates where the
dots are to be drawn.
Different parameters and their possible values are given in the following table:
Parameter Description Parameter Possible Values
dotstyle Specify dot type *, +, o, square, square*, triangle,
triangle*, diamond, diamond* etc.
fillcolor Specify center fill color of a dot red, green, blue, gray, ...
dotsize Specify size of dot 1, 2, 3, ...
Sandeep Kumar (SBSC, DU) PsTricks August 2, 2017 8 / 23
Graphic Objects
Plotting Dots
\ p s s e t { x u n i t =0.7cm , y u n i t =0.7cm} 3 u u
\ p s g r i d [ s u b g r i d d i v =1 , g r i d d o t s = 4 ] ( 0 , 0 ) ( 3 , 3 )
\ p s d o t s [ d o t s t y l e =+](1 , 1 ) ( 2 , 1 ) ( 3 , 1 ) 2 ut ut ut
\ p s d o t s [ d o t s t y l e= t r i a n g l e ] ( 1 , 2 ) ( 2 , 2 ) ( 3 , 2 )
\ p s d o t s [ d o t s t y l e= t r i a n g l e ∗ ] ( 1 , 3 ) ( 2 , 3 ) 1
\ end { p s p i c t u r e } + + +
0
0 1 2 3
Sandeep Kumar (SBSC, DU) PsTricks August 2, 2017 9 / 23
Graphic Objects
Lines and Arrows
PSTricks provides the macro ‘psline’ to draw a line. The general syntax is:
\psline[par1=val1, par2=val2, ..., par3=val3]{arrows}(x1, y1)(x2, y2).
where par1, par2,..., par3 and val1, val2, ..., val3 specify parameter names and their
corresponding values. The argument arrows specifies the type of arrows. (x1, y1), (x2, y2) are
the starting and ending points of the line.
Parameter Description Possible Values
linewidth Specify line width 1pt, 2pt, 3pt, ...
linecolor Specify line color red, green, blue, gray, ...
arrows Specify arrows ↔, ←, →, | - |, ...
linearc Radius of arcs drawn at the corner of lines 1, 2, 3, ...
To draw a collection of line segments, connected end to end, use the following command:
\psline[par1=val1, par2=val2, ..., parn=valn]{arrows}(x1, y1)(x2, y2)...(xn, yn).
where {(x1, y1), (x2, y2)}, {(x2, y2), (x3, y3)}, ... , {(x(n-1), y(n-1)), (xn, yn)} are the end
points of the line segments.
Sandeep Kumar (SBSC, DU) PsTricks August 2, 2017 10 / 23
Graphic Objects
Drawing Line
3
\ begin { p sp i c t u re }(4 ,4)
\ p s g r i d [ s u b g r i d d i v =1 , g r i d d o t s = 4 ] ( 0 , 0 ) ( 4 , 4 )
\ p s l i n e (0 , 0 ) ( 2 , 1) 2
\ p s l i n e [ l i n e w i d t h =1p t ]{<−>}(0, 2 ) ( 3 , 2 )
\ p s l i n e [ l i n e w i d t h =2p t ]{<−>}(2, 0 ) ( 4 , 2 )
(2 , 4 ) ( 0 , 3) 1
\ end { p s p i c t u r e }
0
0 1 2 3 4
Sandeep Kumar (SBSC, DU) PsTricks August 2, 2017 11 / 23
Graphic Objects
Circle
PSTricks provides the macros ‘pscircle’ and ‘pscircle*’ to draw a hollow circle and a filled
circle respectively. The general syntax is:
\pscircle[par1=val1, par2=val2, ..., par3=val3](x, y){r}
The above command draws a circle of radius r with center (x, y ). The possible parameters and
their values are same, as those of \psline.
4
3
\ begin { p sp i c t u re }(4 ,4)
\ p s g r i d [ s u b g r i d d i v =1 , g r i d d o t s = 4 ] ( 0 , 0 ) ( 4 , 4 ) 2
\ p s c i r c l e [ l i n e c o l o r =g r e e n ] ( 2 , 2 ){ 2 }
\ p s c i r c l e ∗ [ l i n e w i d t h =1p t ] ( 2 , 2 ) { 1 }
\ end { p s p i c t u r e } 1
0
0 1 2 3 4
Sandeep Kumar (SBSC, DU) PsTricks August 2, 2017 12 / 23
Graphic Objects
Ellipse
PSTricks provides the macros ‘psellipse’ and ‘psellipse*’ to draw a hollow ellipse and a
filled ellipse respectively. The general syntax for this macro is:
\psellipse[par1=val1, par2=val2, ..., par3=val3](x, y)(r1, r2)
The above command draws an ellipse with r 1 and r 2 as horizontal and vertical radii respectively
and center (x, y ). The possible parameters and their values are same, as those of \psline.
3
2
\ begin { p sp i c t u re }(3 ,3)
\ p s g r i d [ s u b g r i d d i v =1 , g r i d d o t s = 4 ] ( 0 , 0 ) ( 3 , 3 )
\ p s e l l i p s e [ l i n e w i d t h =1p t ] ( 1 . 5 , 1 . 5 ) ( 1 . 5 , 1 ) 1
\ end { p s p i c t u r e }
0
0 1 2 3
Sandeep Kumar (SBSC, DU) PsTricks August 2, 2017 13 / 23
Graphic Objects
Putting Label at a point
PSTricks provides the macros ‘put’ and ‘rput’ to put a label at a specified location. The
general syntax is:
\put(x, y){text}
\rput{A}(x, y){text}
where (x, y) is the point where the label ‘text’ is to be putted. In ‘rput’ macro, A specifies the
angle at which the label is to be rotated.
(3, 3)
3
\ begin { p sp i c t u re }(3 ,3) 2
2)
\ p s g r i d [ s u b g r i d d i v =1 , g r i d d o t s = 4 ] ( 0 , 0 ) ( 3 , 3 )
(2,
\ r p u t { 3 0 } ( 1 , 1 ) { ( 1 , 1 )}
\ r p u t { 6 0 } ( 2 , 2 ) { ( 2 , 2 )} 1
\ r p u t { 9 0 } ( 3 , 3 ) { ( 3 , 3 )} 1)
(1,
\ end { p s p i c t u r e }
0
0 1 2 3
Sandeep Kumar (SBSC, DU) PsTricks August 2, 2017 14 / 23
Graphic Objects
Co-ordinate Axes
PSTricks provides the macro ‘psaxes’ to draw the coordinate axes. The general syntax is:
\psaxes[par1=val1, par2=val2, ..., par3=val3]{arrows}(x, y)(x1, y1)(x2, y2)
where the co-ordinates (x, y), (x1, y1), (x2, y2) define the intersection point of axes, lower-left
corner and upper-right corner, respectively, of the co-ordinate system. The possible arrows type,
parameters and their values are same, as those of \psline.
y
2
\ begi n { p s p i c t u r e }( −1.5 , − 1 . 5 ) ( 2 . 5 , 2 . 5 )
1
\ p s a x e s [ l i n e c o l o r =g r a y , l i n e w i d t h =0.5 p t ]{ −}
( 0 , 0)( −1 , −1)(2 , 2 )
\ put ( 2 . 3 , −0.1){ $x$ }
x
\ put ( − 0 . 1 , 2 . 3 ) { $y$ }
\ end { p s p i c t u r e } −1 1 2
−1
Sandeep Kumar (SBSC, DU) PsTricks August 2, 2017 15 / 23
Graphic Objects
Plotting Mathematical Functions
PSTricks provides the macro ‘psplot’ to plot functions. Before using this function, include the
package ‘pst-plot’ in the preamble of the document. The general syntax is:
\psplot[par1=val1, par2=val2, ..., par3=val3]{x_min}{x_max}{myFunction}
where x_min and x_max specify the domain of the function. The parameters and their values are
same as those of macro \psline.
‘yMinValue’ and ‘yMaxValue’ that restrict the graph of the given function between these two
values.
myFunction is the function that is to be plotted, which needs to be in postfix form, which is a
bit inconvenient to use.
By setting the property ‘algebraic=true’, we can avoid the postfix notation and can work with
standard notations.
Sandeep Kumar (SBSC, DU) PsTricks August 2, 2017 16 / 23
Graphic Objects
Plotting Functions
5
\ begi n { p s p i c t u r e }( −3.5 , − 1 . 5 ) ( 3 . 5 , 5 . 5 ) 4
\ p s a x e s {−>}(0, 0 ) ( − 3 . 5 , − 1 . 5 ) ( 3 . 5 , 5 . 5 )
x2
3
\ p s p l o t [ a l g e b r a i c =t r u e ]{ −3}{3}{ x+2}
2
+
\ p s p l o t [ a l g e b r a i c =t r u e , yMaxValue =5]{ −3}{3}{ x ˆ2} 2
x
\ r p u t { 4 5 } ( 1 , 2 . 3 ) { $x+2$ }
1
\ r p u t {90}( −2 , 3 ){ $x ˆ2 $ }
\ end { p s p i c t u r e }
−3 −2 −1
−1 1 2 3
Sandeep Kumar (SBSC, DU) PsTricks August 2, 2017 17 / 23
Graphic Objects
Mathematical Functions
PSTricks also provides standard mathematical functions. A list of all these functions is given in
the below table:
Function Description Function Description
sin Sine function cos Cosine function
tan Tangent function ASIN Arc sine function
ACOS Arc cosine function ATAN Arc tangent function
EXP Exponential function SINH Sine hyperbolic function
COSH Cosine hyperbolic function TANH Tangent hyperbolic function
log Log function (base 10) ln Log function (base e)
fact Factorial function sqrt Square root function
ceiling Ceiling function floor Floor function
abs Absolute function round Round off function
Sandeep Kumar (SBSC, DU) PsTricks August 2, 2017 18 / 23
Graphic Objects
Plotting Sine and Cosine Functions
\psset{xunit=0.8cm,yunit=0.8cm}
\begin{pspicture}(-4.5, -2.5)(4.5,2.5)
\psaxes(0, 0)(-4,-2)(4, 2)
\psplot[algebraic=true, linecolor=red]{-4}{4}{sin(x)}
\psplot[algebraic=true, linecolor=blue]{-4}{4}{cos(x)}
\rput{-20}(2, 1.2){$Sin(x)$}
\rput{45}(-1.5, 0.6){$Cos(x)$}
\end{pspicture}
2
Sin(
x )
1
x)
s(
Co
−4 −3 −2 −1 1 2 3 4
−1
−2
Sandeep Kumar (SBSC, DU) PsTricks August 2, 2017 19 / 23
Graphic Objects
Plotting Absolute and Round off Functions
\psset{xunit=0.6cm,yunit=0.6cm}
\begin{pspicture}(-3.5, -3.5)(3.5, 3.5)
\psaxes(0, 0)(-3,-3 )(3, 3)
\psplot[algebraic=true, linecolor=red, linewidth=1pt]{-3}{3}{abs(x)}
\psplot[algebraic=true, linecolor=gray, linewidth=1pt]{-3}{3}{round(x)}
\rput{0}(-2, -1.5){$[x]$}
\rput{0}(-1.5, 2){$|x|$}
\end{pspicture}
|x| 2
−3 −2 −1 1 2 3
−1
[x]
−2
−3
Sandeep Kumar (SBSC, DU) PsTricks August 2, 2017 20 / 23
Graphic Objects
Graphical Representation of Rolle’s Theorem
(f ′ (c) = 0)
\begin{pspicture}(-4,-1)(1,4)
\psplot[algebraic=true]{-2}{0}{x^3 -4*x}
\psline(-3,3.08)(1,3.08)
\put(-2.5, -0.5){$(a, f(a))$}
\put(-0.5, -0.5){$(b, f(b))$}
\put(-1.75, 3.5){$(f’(c) = 0)$} f (a) = f (b)
\put(-4,1){$f(a) = f(b)$}
\end{pspicture}
(a, f (a)) (b, f (b))
Sandeep Kumar (SBSC, DU) PsTricks August 2, 2017 21 / 23
Graphic Objects
Exercises:
1 Plot a circle of radius 2, center at (0,0).
2 Plot the function x 3 over the domain (-3, 3).
3 Plot the function ln(x) over the domain (1, 10).
Sandeep Kumar (SBSC, DU) PsTricks August 2, 2017 22 / 23
Graphic Objects
Thank You
Sandeep Kumar (SBSC, DU) PsTricks August 2, 2017 23 / 23