Friday, November 25, 2011

Begin with Pstricks----Hello,world

It has be a tradition to give a "Hello, world" program first when a programming language is introduced. Pstricks is not a programming language (In fact, it is a set of macros that allow the inclusion of postscript drawings directly inside Tex or Latex code), but we will also begin it with "Hello, world".


\documentclass{article}
\usepackage{pstricks}
\begin{document}
  \rput(1,1){Hello,world} 
\end{document}

There are Some explanations on this example. "\rput(1,1){Hello,world}" draw text at position (1,1). The default coordinate Psstrick used is set up like this: the current point in Tex is used as the origin. The horizontal line through this point is the x-axes, and left is the positive direction. The vertical line through this point is the y-axes, and up direction is positive. The default unit is 1cm.

Save it as a file (for example, with file name Pstircks_hello_world.tex), then Tex compile it with "latex" command. To get a postscript file, "dvips" or other dvi to postscript program should be used. And to get a pdf file, a ps to pdf program is also needed. Note that dvi2pdf may not produce a correct pdf file.

At last the output file looks like this:

Fig.1 A simple Pstricks "Hello, world!" example

Files Download: tex ps pdf

Now we come to a a little more complex "Hello, world" example.

\documentclass{article}
\usepackage{pstricks}
\begin{document}
Pstricks Hello world! Pstricks Hello world! 

Hello,world! 
\rput(0,0){\color{red} Hello,world!} 
Hello,World!

Pstricks Hello world! Pstricks Hello world! 
\end{document}

This tex file will produce a ps or pdf file like this:
Fig.2 An advanced Pstricks "Hello, world!" example
Of course, This is not what we want. The pstricks element is overwritten. This is because Tex do not reserve any space for the pstricks element. So kept in mind that when you insert any pstricks element in, make sure you have leave proper space for it. And to solve it, Pstricks provide a very convenient method--pspicture environment. And it may be talked in the next posts.

Files Download: tex ps pdf

No comments:

Post a Comment

Creative Commons License
Except as otherwise noted, the content of this page is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.