LaTeX/PSTricks

From Wikibooks, open books for an open world
Jump to navigation Jump to search

LaTeX

Getting Started
  1. Introduction
  2. Installation
  3. Installing Extra Packages
  4. Basics
  5. How to get help

Common Elements

  1. Document Structure
  2. Text Formatting
  3. Paragraph Formatting
  4. Colors
  5. Fonts
  6. List Structures
  7. Special Characters
  8. Internationalization
  9. Rotations
  10. Tables
  11. Title creation
  12. Page Layout
  13. Customizing Page Headers and Footers‎
  14. Importing Graphics
  15. Floats, Figures and Captions
  16. Footnotes and Margin Notes
  17. Hyperlinks
  18. Labels and Cross-referencing
  19. Initials

Mechanics

  1. Errors and Warnings
  2. Lengths
  3. Counters
  4. Boxes
  5. Rules and Struts

Technical Text

  1. Mathematics
  2. Advanced Mathematics
  3. Theorems
  4. Chemical Graphics
  5. Algorithms
  6. Source Code Listings
  7. Linguistics

Special Pages

  1. Indexing
  2. Glossary
  3. Bibliography Management
  4. More Bibliographies

Special Documents

  1. Scientific Reports (Bachelor Report, Master Thesis, Dissertation)
  2. Letters
  3. Presentations
  4. Teacher's Corner
  5. Curriculum Vitae
  6. Academic Journals (MLA, APA, etc.)

Creating Graphics

  1. Introducing Procedural Graphics
  2. MetaPost
  3. Picture
  4. PGF/TikZ
  5. PSTricks
  6. Xy-pic
  7. Creating 3D graphics

Programming

  1. Macros
  2. Plain TeX
  3. Creating Packages
  4. Creating Package Documentation
  5. Themes

Miscellaneous

  1. Modular Documents
  2. Collaborative Writing of LaTeX Documents
  3. Export To Other Formats

Help and Recommendations

  1. FAQ
  2. Tips and Tricks

Appendices

  1. Authors
  2. Links
  3. Package Reference
  4. Sample LaTeX documents
  5. Index
  6. Command Glossary

edit this boxedit the TOC

PSTricks is a set of extensions. The base package is pstricks, other packages may be loaded when required.

The xcolor extension gets loaded along PSTricks, so there is no need to load it manually.

PSTricks has one technical specification: it uses PostScript internally, hence the name. Thus you cannot use the pdftex or pdflatex compilers, you will need to use dvips to get your proper document. It is still possible to get PDF from PS files thanks to ps2pdf. There is also the possibility to use the PDFTricks extension, which makes it feasible to use pdflatex together with PSTricks commands.

However, if you have installed the package xetex-pstricks, you can use pstricks with xetex or xelatex without modification of source file.

The pspicture environment[edit | edit source]

PSTricks commands are usually placed in a pspicture environment.

\begin{pspicture}(x1,y1)
% ...
\end{pspicture}

The first argument between parentheses specifies the coordinates of the upper-right corner of the picture. The bottom-left corner is at (0,0) and is placed at the reference point of the next character in the LaTeX document.

It is also possible to specify the coordinates (x0,y0) of the bottom-left corner:

\begin{pspicture}(x0,y0)(x1,y1)
% ...
\end{pspicture}

Thus the size of the picture is (x1–x0) × (y1–y0). The default unit for coordinates is centimeters (cm); this can be changed with \psset, as in \psset{unit=1bp}. Any TeX dimension is allowed.

Fundamental objects[edit | edit source]

Lines and polylines[edit | edit source]

A simple line gets printed with

\psline(x0,y0)(x1,y1)

To get a vector, add an arrow as parameter:

\psline{->}(x0,y0)(x1,y1)

You can add as many points as you want to get a polyline:

\psline(x0,y0)(x1,y1)(x2,y3)…(xn,yn)

To get rounded corners, add the following option:

\psline[linearc=0.2]{->}(0,0)(2,1)(1,1)

or

\psline[linearc=0.2,arrows=->](0,0)(2,1)(1,1)

Rectangles[edit | edit source]

\psframe(x0,y0)(x1,y1)
\psframe*(x0,y0)(x1,y1)

The starred version prints a filled rectangle. Use the following parameter to get rounded corners:

\psframe[framearc=0.2](x0,y0)(x1,y1)

Polygons[edit | edit source]

Polygons are always closed. The syntax is the same as for \psline:

\pspolygon(x0,y0)(x1,y1)(x2,y2)...(xn,yn)

As for rectangles, the starred version prints a filled polygon. And the linearc=0.2 option will print rounded corners.

Circles, arc and ellipses[edit | edit source]

Starred version fills the shape.

For circles, you need to provide center coordinates and radius:

\pscircle(x,y){r}

To restrict the drawing to an arc, append the starting and ending angles in trigonometric notation:

\psarc(x,y){r}{angle1}{angle2}

Finally, ellipses:

\psellipse(x,y)(horizontal_axis,vertical_axis)


Curves[edit | edit source]

\psparabola(x0,y0)(x1,y1)

will print a symmetric parabola with vertical asymptote, vertex (x1,y1) and ending at (x0,y0).

Use \psbezier to print a Bézier curve with an arbitrary number of control points. Arcs have at most 4 control points. Use the showpoints=true option to print the control points and the tangents.

Use \pscurve to print the interpolation of the given points. The \psecurve command omits the first and the last arcs.

Text[edit | edit source]

Use

\rput(x,y){text}

to print text. Provide an angle to rotate the text.

\rput{angle}(x,y){text}

You can provide the anchor of the text which will be at the specified coordinate.

\rput[t]{45}(5,5){text}

Available anchors:

  • B, Bl, Br: baseline center, left and right.
  • t, tl, tr: top center, left and right.
  • b, bl, br: bottom center, left and right.

There is also the \uput command with further options:

\uput{distance}[angle](x,y){text}

The distance parameter is the distance from the coordinate.

PSTricks features several frame style for text.

  • \psframebox{text}: rectangle.
  • \psdblframebox{text}: double rectangle.
  • \psshadowbox{text}: shaded rectangle.
  • \pscirclebox{text}: circle.
  • \psovalbox{text}: oval.
  • \psdiabox{text}: diamond.
  • \pstribox{text}: triangle.

Example:

\rput(5,5){\psdiabox*[fillcolor=green]{text}}


Using the pst-text extension, it is possible to draw a text path.

\pstextpath{shape}{text}

To print a text following a path without printing the path, you need to use \psset{linestyle=none}.

Example:

\usepackage{pst-text}

% ...
\begin{pspicture}(5,5)
\psset{linestyle=none}
\pstextpath{\psline(0,0)(1,1)(2,0)}{triangle text}
\end{pspicture}

Grids[edit | edit source]

Without any parameter, the \psgrid command will print a grid all over the pspicture, with a spacing of 0.2 (i.e. 2mm). You can specify parameters:

  • \psgrid(xmax,ymax): prints a grid from (0,0) to (xmax,ymax).
  • \psgrid(xmin,ymin)(xmax,ymax): prints a grid from (xmin,ymin) to (xmax,ymax).
  • \psgrid(x0,y0)(xmin,ymin)(xmax,ymax): prints a grid from (xmin,ymin) to (xmax,ymax), one of the node is at (x0,y0).
  • griddots=value: the full line of the main graduations is replaced by a dotted line. The value is the number of dots per graduation.
  • subgriddots=value: same as griddots but for sub-graduations.
  • gridcolor=color,subgridcolor=color: color of graduations and sub-graduations.
  • gridwidth=value,subgridwidth=value: width of the lines.
  • subgriddiv=value: number of subgraduations between two main graduations.
  • gridlabels=value: size of the label numbers.
  • ticksize=value: self-explanatory.
  • ticksize=valueneg valuepos: same as above, but valueneg specifies the size for negative coordinates, valuepos for positive coordinates.
  • ticklinestyle=value: self-explanatory. value may be one of solid, dashed, dotted. This is useful for huge graduations (i.e. ticksize is high).
Example
\psgrid[griddots=5, subgriddiv=0, gridlabels=0pt](-1,-1)(5,5)
Axis

If you want to add axes, use the pstricks-add extension with the following commands:

\psaxes(xmin,ymin)(xmax,ymax)
\psaxes(x0,y0)(xmin,ymin)(xmax,ymax)

(xmin,ymin) and (xmax,ymax) being the extreme, (x0,y0) being the intersection.

Options
  • Dx=value and Dy=value defines the spacing between graduations.
  • comma lets you use the comma as decimal separator.
  • As for lines, {->} adds arrows on axes.
Example
\usepackage{pstricks-add}
% ...
\begin{pspicture}(-1,-1)(5,5)
\psaxes[comma,Dx=0.5,Dy=0.5]{->}(0,0)(3,3)
\end{pspicture}

Generic parameters[edit | edit source]

All shapes[edit | edit source]

These are to be added between square brackets.

  • linewidth=value: if value is without unit, then the default unit is used.
  • linecolor=color: color is as defined by the xcolorpackage.
  • linestyle=value: value is one of dashed,dotted.
  • doubleline=true.
  • showpoints=true: highlights points.
    • dotscale=value specifies the size of the points.
    • dotstyle=value where value is among:
      • *: disc
      • o: circle
      • +,x: cross
      • square, squarre*: starred version is filled.
      • diamond, diamond*
      • triangle, triangle*
      • etc.

For example

\pscircle[linewidth=0.2,linestyle=dashed,linecolor=blue](0,0){1}

To apply parameters globally:

\psset{linewidth=0.2,linestyle=dashed,linecolor=blue}
\pscircle(0,0){1}

This command also lets you change the default unit for lengths.

  • unit=value
  • xunit=value and yunit=value

value is a number with or without unit. This changes the scale of the drawings, but will not change the width of lines.

Open shapes[edit | edit source]

You can define the extreme of an open shape (line, polyline, arc, etc.) with an optional parameter {symbol1-symbol2}. There is a decent list of available symbols.

  • < or >: arrow.
  • << or >>: double arrow.
  • |: bar.
  • |*: centered bar.
  • oo: circle.
  • o: centered circle.
  • **: disk.
  • *: centered disk.
  • |< or >|: arrow plus bar.
  • cc: rounded extreme.
  • c: centered rounded extreme.

Example:

\psline{|->>}(x0,y0)(x1,y1)

Closed shapes[edit | edit source]

For closed shape you may define the fillstyle.

  • fillstyle=value: pattern. Possible values: crosshatch, crosshatch*, vlines, vlines*, hlines, hlines*, solid.
  • fillcolor=color.
  • hatchcolor=color.
  • hatchwidth=value.
  • hatchsep=value.
  • hatchangle=value.

Example:

\pscircle[hatchcolor=blue,fillstyle=vlines](0,0){1}

Object location[edit | edit source]

The \rput and \uput commands can be used to move any object.

Example
\begin{pspicture}(5,5)
\psline{->}(0,0)(1,1)
\rput(1,1){\psline{->}(0,0)(1,1)}
\end{pspicture}

or

\begin{pspicture}(5,5)
\psline{->}(0,0)(1,1)
\psline{->}(1,1)(2,2)
\end{pspicture}

You can repeat the operation with \multirput:

\multirput(x0,y0)(xoffset, yoffset){times}{object}

You can use the same options as for \rput:

\multirput[reference]{angle}(x0,y0)(xoffset, yoffset){times}{object}

With no text but with graphics only, you can use the \multips command:

\multips(x0,y0)(xoffset, yoffset){times}{object}
\multips{angle}(x0,y0)(xoffset,yoffset){times}{object}

The PDFTricks extension[edit | edit source]

The original PSTricks package does not work with pdflatex, but thankfully PDFTricks allows us to bypass this limitation.

Usage
  • Declare the PDFTricks packages in the preamble.
  • Place all PSTricks extensions in a psinputs environment; place all PSTricks commands in a pdfpic environment.
  • Compile with pdflatex -shell-escape <file>.

The -shell-escape parameter enables shell command calls. It is required for PDFTricks to run.

Example
\documentclass{article}
 
\usepackage{pdftricks}
\begin{psinputs}
   \usepackage{pstricks}
   \usepackage{multido}
\end{psinputs}
 
% ...
 
\begin{document}
 
% ...
 
\begin{pdfpic}
   \psset{unit=\linewidth}
   \begin{pspicture}(0,0)(10,10)
      [...]
   \end{pspicture}
\end{pdfpic}
 
% ...
 
\end{document}

Another way to use PSTricks with pdflatex is the pst-pdf package.


Previous: PGF/TikZ Index Next: Xy-pic