Hello World (Pic)

From LiteratePrograms

Jump to: navigation, search
Other implementations: Ada | ALGOL 68 | Alice ML | Amiga E | Applescript | AspectJ | Assembly Intel x86 Linux | Assembly Intel x86 NetBSD | AWK | bash | BASIC | Batch files | C | C, Cairo | C, Xlib | Candle | Clojure | C++ | C# | Delphi | Dylan | E | Eiffel | Erlang | Forth | FORTRAN | Fortress | Go | Groovy | Haskell | Hume | IBM PC bootstrap | Inform 7 | Java | Java, Swing | JavaScript | LaTeX | Lisp | Logo | Lua | Maple | MATLAB | Mercury | OCaml/F Sharp | occam | Oz | Pascal | Perl | PHP | Pic | PIR | PLI | PostScript | Prolog | Python | Rexx | Ruby | Scala | Scheme | Seed7 | sh | Smalltalk | SQL | Standard ML | SVG | Tcl | Tcl Tk | Visual Basic | Visual Basic .NET | XSL

This is a simple pic script that prints a string to the document.

Contents

Requirement

  • pic interpreter: We recommend Dwight Aplevich's dpic.
  • LaTeX
  • eps2png: We need it only if we want to convert an EPS document to a png file.

Drawing "Hello, world!" in a monochrome document

Enlarge
Simple example of PIC output

Every pic document begins with .PS and ends with .PE.

.PS
pic codes...
.PE

For the first example, we draw an invisible box and centers the text "Hello, world!" in it.

<<basic.pic>>=
.PS
box invis "Hello, world!"
.PE

We should convert basic.pic to the LaTeX picture command.

[crypto@crypto ~/Draw/]$ dpic -p basic.pic > basic.tex
[crypto@crypto ~/Draw/]$ cat basic.tex
\psset{unit=1in,cornersize=absolute,dimen=middle}%
\begin{pspicture}(0,-0.25)(0.75,0.25)%
% dpic version 03.Jan.06 for PSTricks 0.93a
\rput(0.375,0){Hello, world!}
\end{pspicture}%

Now we have to embed basic.tex in a normal LaTeX document. head.tex comes before basic.tex and tail.tex follows basic.tex.

<<head.tex>>=
\documentclass{article}
\usepackage{pst-eps, boxdims, graphicx, pst-grad}
\usepackage{palatino}
\usepackage[usenames]{color}
\pagestyle{empty}
\thispagestyle{empty}
\begin{document}
\Huge
\newbox\graph
\begin{TeXtoEPS}
<<tail.tex>>=
\box
\graph
\end{TeXtoEPS}
\end{document}

All these three files must be concatenated to one LaTeX source file.

[crypto@crypto ~/Draw/]$ cat head.tex basic.tex tail.tex > doc.tex

We compile doc.tex to obtain a dvi document, which is converted to EPS document.

[crypto@crypto ~/Draw/]$ latex doc.tex
[crypto@crypto ~/Draw/]$ dvips -X 1440 -Ppdf -G0 -E doc.dvi -o doc.eps

Now this EPS document can be further converted to any graphic file. For example, we can convert EPS into PNG using eps2png.

[crypto@crypto ~/Draw/]$ eps2png doc.eps

boxdims.sty

We use boxdims.sty in head.tex in order to control tricky box settings. boxdims.sty is originally written by Dwight Aplevich. The original style file is available at http://www.tug.org/tex-archive/graphics/circuit_macros/boxdims.sty in Circuit macro package.

<<boxdims.sty>>=
%* Circuit_macros Version 5.9, copyright (c) 2006 J. D. Aplevich, under    *
%* the LaTeX Project Public License. The files of this distribution may    *
%* be redistributed or modified, provided that this copyright notice is    *
%* included and provided that modifications are clearly marked to          *
%* distinguish them from this distribution.  There is no warranty          *
%* whatsoever for these files.                                             *
%
% boxdims.sty, for use with m4 preprocessors.  Last modified 30 Apr 2004.
%
% \boxdims{arg1}{arg2} expands to arg2, but writes into file \jobname.dim
% the m4 definitions for macros arg1_h, arg1_w, arg1_d, the height, width
% and depth of \hbox{arg2}.
%
% \defboxdim{arg1}{arg2} writes the definitions but expands to nothing.
%
% \boxdimfile{filename} sets the output file to filename, default \jobname.dim
%
\ProvidesPackage{boxdims}
         [2004/04/30 v2.0 Macros: boxdimfile, boxdims, defboxdim (DA)]
\newwrite\@dimensionfile
\newif\if@dimfile
\newbox\dimbox
\def\boxdimfile#1{\immediate\openout\@dimensionfile=#1\global\@dimfiletrue%
  \typeout{ boxdims.sty v2.0: Writing dimension file #1 }}%
\def\boxdims#1#2{\defboxdim{#1}{#2}#2}
\def\defboxdim#1#2{\if@dimfile\else%
    \immediate\openout\@dimensionfile=\jobname.dim\global\@dimfiletrue%
    \typeout{ boxdims.sty v2.0: Writing dimension file \jobname.dim }\fi%
  \setbox\dimbox=\hbox{#2}%
  \begingroup\@sanitize\edef\@tempa{\write\@dimensionfile{%
  \@defboxdim{#1}}}\expandafter\endgroup\@tempa}
\def\@defboxdim#1{%
define(`#1_w',\the\wd\dimbox__)%
define(`#1_h',\the\ht\dimbox__)%
define(`#1_d',\the\dp\dimbox__)dnl}


Makefile

All these tricky process can be automated by Makeile. Typing make would open an EPS document. Typing make png would convert the EPS document into a PNG file.

<<Makefile>>=
# The name of a drawing file
FIG=basic
# Target document
DOC=doc
# PIC command
PIC=dpic -p
# dvips options
DVIPSOPT=-X 1440 -Ppdf -G0 -E
HEAD=head.tex
TAIL=tail.tex

all:$(FIG).eps
	gsview32 $(FIG).eps &

png:$(FIG).eps
	eps2png $(FIG).eps

$(FIG).eps:$(DOC).dvi
	dvips $(DVIPSOPT) $(DOC).dvi -o $(FIG).eps

$(DOC).dvi:$(DOC).tex $(FIG).tex
	latex $(DOC)

$(DOC).tex:$(FIG).tex
	cat $(HEAD) $(FIG).tex $(TAIL) > $(DOC).tex

$(FIG).tex:$(FIG).pic
	$(PIC) $(FIG).pic > $(FIG).tex

clean:
	rm -f *~
	find . -maxdepth 1 -type f -name "$(DOC).*" ! -name "$(DOC).png" -exec rm -f {} \;

Colorful example

Enlarge
Colorful example of PIC output

Since pic compiles descriptions of picture into commands that are interpreted by (La)TeX, we can use virtually every trick in (La)TeX. For example, we can generate colorful output using LaTeX color package.

<<colorful.pic>>=
.PS
box invis "\color{red}H\color{blue}e\color{green}l\color{yellow}l\color{cyan}o\color{black},\
 \color{magenta}w\color{red}o\color{blue}r\color{green}l\color{cyan}d!"
.PE

Typing make png FIG=colorful would produce the colorful "Hello, world!" PNG file.

External links

Download code
Views
Personal tools