Thursday, June 16, 2011

Creating image files from pstricks code

Yesterday I explained how to create a dependency graph in LaTex using pstricks. It is simple to include the code in your pdf file directly, but there are two major problems with this approach:

  1. You must then use latex and dvipdf in order to compile the file, rather than just pdflatex. 
  2. It lacks modularity. This is particularly problematic when you are not completely sure what the finished graph should look like. I often create a graph, decide to try something else, comment out all or part of the graph and make my changes. This results in a lot of commented-out code, particularly if I add comments to explain what differentiates one graph from another. It would be cleaner to create a graphic with a descriptive name and include that descriptively-named graphic in a file. Then, even if I decide to try something different, I can simply create a second graphic, and my text file remains much cleaner.
The steps to creating a dependency graph (or any other figure) as a stand-alone pdf file that can then be included as a graphic is to:
  1. create the graphic normally as you would any in any pdf file
  2. produce the pdf file. It will be a full-size page with excess white space.
  3. crop the excess white space.
  4. include the cropped output file in your destination file.
1. Create the graphic normally as you would any in any pdf file.

Follow the steps in my previous post. You'll now have a tex file. You don't want page numbers, so make sure the preamble includes the following command:

\pagestyle{empty}

2. Produce the pdf file. It will be a full-size page with excess white space.

You can't use pdflatex with pstricks; instead use

latex example.tex
dvipdf example.dvi

This will produce the output example.pdf. It's a full-size page including excess white space.

3. Crop the excess white space.

The command

pdfcrop example.pdf

will create the output file example-crop.pdf.

4. Include the cropped output file in your destination file.

 First, add to the preamble

\usepackage{graphicx}

\begin{center}                                    
\includegraphics[]{example-crop.pdf}
\end{center}

5. Compile your final file. Unless something else is preventing you, you can now use pdflatex to compile the final file.

You should now have created a dependency graph as a stand-alone file, and included it in a separate pdf file! It took me a long time to get all these steps. I was stuck on trying to use dvipng, which I was not able to get to recognize the arcs in the dependency graph. So I hope this prevents someone else from having the same trouble!

No comments:

Post a Comment