90

I thought that PSTricks package was not possible to use in pdfLaTeX but the user Dima claims otherwise.

How can I force pdfLaTeX to use PSTricks then?

4 Answers4

90

The easiest way is running xelatex instead of pdflatex. It detects itself PostScript code or eps images in the document and does the conversion on the fly. If you do not want to use xelatex then there are other solutions:

If you have an up-to-date TeX distribution use

\usepackage[pdf]{pstricks}

and then run your document with pdflatex -shell-escape <file>. Then the PSTricks images are created on-the-fly as stand alone pdf images and saved in <file>-pics.pdf. If you have an older system use

\usepackage{auto-pst-pdf}
\usepackage{pst-...}

For more information see PSTricks web page

For Windows you have to install a Perl version, if you want to use the full power of the auto-pst-pdf package. However, if you do not want or cannot install Perl then use

\usepackage[crop=off]{auto-pst-pdf}

There is also a Perl script pst2pdf which can create the document with pdflatex and also the PSTricks images as pdf|png|whatever images. It takes the preamble of the main document and creates stand-alone-documents for all PostScript specific code.

  • 2
    I see one shortcoming with pstricks and tikz (mentioned several times by other users): this is the fact that we have to compile over and over the same figures when working on a report. With the shell escape, it becomes problematic because the compilation takes longer. Would it be difficult to implement a test so that if the <file>-pics.pdf already exists, latex skips the compilation of the associated pstricks code? Equivalently, it may be more relevant to compile the pstricks figures separately (separate files) and then, input the pic.pdf only in the main file. Any thoughts? – pluton Jan 06 '11 at 19:58
  • @pluton: The tricky part is what to do when the code changes. I suppose it'd be possible to do something like get the MD5 hash of the output and the input, put it in the aux file, and then on recompile, if the hash of the input or output do not match the stored one, recompute. One can use the pdfTeX primitive \pdfmdfivesum to compute the hash. – TH. Jan 06 '11 at 20:15
  • 5
    @pluton: \usepackage[off]{auto-pst-pdf} –  Jan 06 '11 at 20:18
  • 6
    @pluton: for TikZ/pgf you can use the externalise library to do this. I've just been using it on a very picture-intensive document (116 separate pictures) and it saved a lot of hanging around waiting for the thing to compile. – Andrew Stacey Jan 06 '11 at 20:20
  • @Herbert, I found a broken link here http://www.bellarmine.edu/faculty/wrising/HowTo/pstricks.html – Display Name Jan 07 '11 at 01:15
  • where can I find documentation on the [pdf] option for the {pstricks} package? Is the syntax all the same, or do I still need to do something like use a pspicture environment? Thanks. – Heather Feb 23 '11 at 09:59
  • 1
    run texdoc pst-news10 –  Feb 23 '11 at 17:12
  • @Herbert: I am not fully understanding your advice on how to use pstricks in pdflatex. I am not getting any picture output. – night owl Dec 11 '11 at 08:14
  • @night owl: the easiest way is to run xelatex. However, I need more informations to say what goes wrong when you didn't get any output. –  Dec 11 '11 at 08:19
  • I am using: 1) LaTeX => PDF on texnic center. With MikTeK distribution. I am not sure what packages to include. I read everything here and on the website you provided. Just more confusing. It did say something about needing perl or some sort. I am using Windows 7. – night owl Dec 11 '11 at 08:49
  • with Perl (http://activestate.com/perl) you can crop by default the whitespace around the figures which makes sense. Without installoing perl load usepackage[crop=off]{auto-pst-pdf} –  Dec 11 '11 at 09:00
  • @Herbert I tried this, but no graphics appear. This is what happens when compiled.
    [1] http://i.stack.imgur.com/IBEs0.png
    – night owl Dec 14 '11 at 14:37
  • @nightowl: did you run it with pdflatex -shell-escape or on MiKTeX with pdflatex -enable-write18?? –  Dec 14 '11 at 15:04
  • @Herbert: where do I put this -enable- write 18. I was reading http://tug.org/PSTricks/main.cgi?file=pdf/pdfoutput#autopstpdf and they showed a screenshot for MikTeX but did not have the specified field filled in with the information. Could you post a link of a screenshot as to where you would place that, to enable it in MikTeX. – night owl Dec 15 '11 at 08:46
  • it has not really something to do with MiKTeX, it must be modified inside your editor. And there are some examples ... –  Dec 15 '11 at 08:48
  • @Herbert: What editor are we referring to here. I though MikTeK was the actual editor. – night owl Dec 15 '11 at 11:40
  • MiKTeX is a TeX distribution, like TeXLive and not an editor! An editor is the programm which you use for your text input, like TeXNicCenter –  Dec 15 '11 at 12:20
  • @Herbert: So sorry, I was not thinking straight. I was referring to TeXnicCenter this whole time but was saying MikTeX. Wow!. Right, I am using TeXnicCenter. How could I add the -enable- write 18 and where at in TeXnicCenter? – night owl Dec 15 '11 at 12:55
  • while (true) { http://tug.org/PSTricks/main.cgi?file=pdf/pdfoutput#TXC } ... –  Dec 15 '11 at 13:00
  • @Herbert: That snapshot, is still pretty vague with the given directions. It talks about inserting -shell-escape , but do not actually show that on the screen shots. So I tried:

    --enable-write18 interaction=-shell-escape nonstopmode -max-print-line=120 "%pm" with no luck. The editor would not compiler anymore.

    – night owl Dec 15 '11 at 14:05
  • -enable-write18 or alternetively -shell-escape. For more help I need your log file. However, the PSTricks mailing list should give more help here –  Dec 15 '11 at 14:26
  • @Herbert: Okay, so its either or. Hah, okay. The correct command is the following syntax being: -enable-write18 -interaction=nonstopmode -max-print-line=120 "%pm" It has to be exact with spaces and everything or it wont compile. This is for TeXnicCenter. – night owl Dec 15 '11 at 17:35
  • If someone has problems with pdfcrop under MikTeX, see http://www.latex-community.org/forum/viewtopic.php?f=9&t=14677 – letmaik Sep 08 '12 at 08:41
  • What I always have been wondering is about the interaction with the label/ref mechanism: What exactly happens with \label, \ref or \cite commands in my pstricks figure that refer to (or to which I refer from) the main text. Do they survive? Do the hyperlinks survive? I assume, with xelatex this works, but with \usepackage[pdf]{pstricks} it doesn't? Would be great if you could extend your answer in this respect. – Daniel May 23 '13 at 14:45
  • A PSTricks image is the same as a graphic, it is a box. You can refer to the box, which is the pspicture environment. –  May 23 '13 at 14:56
7

While I was not aware of the solution provided by Herbert, let me also introduce another solution.

You can easily use pstricks with pdflatex following this procedure:

  1. Include the pstricks package in your .tex file (the second is necessary only in case that you want to use the pstricks extensions):

    \usepackage{pst-all}
    \usepackage{pstricks-add}
    

Now, when processing your files to generate the final pdf file go through these steps (in the following it is assumed that main.tex is your main tex file):

  1. Create the dvi file:

    latex main.tex
    
  2. Create the postscript file:

    dvips -Ppdf -G0 main.dvi
    
  3. Finally, generate the pdf file:

    ps2pdf main.ps
    

Fortunately, all these steps can be automated in a makefile (I wish there would be a way to attach a file to these answers but I have found none in the advanced help, sorry about that):

NAME    = myfile.pdf
SRC     = main.tex
OBJ     = main.pdf

# Macro Definitions 
LATEX   = latex
DVIPS   = dvips
PSPDF   = ps2pdf
RM      = /bin/rm -f    
TAR     = tar

.SUFFIXES: .tex .dvi .ps .pdf

.tex.dvi:
    $(LATEX) $<

.dvi.ps:
    $(DVIPS) -Ppdf -G0 $<

.ps.pdf:
    $(PSPDF) $<

##############################
# Basic Compile Instructions #
##############################

all:    $(NAME)

$(NAME): $(OBJ)
    @mv $(OBJ) $(NAME)

: $(SRC)

clean:
    @$(RM) *.aux *.log *.nav *.out *.snm *.toc *.dvi *.bbl *.blg *~

delete: clean
    @$(RM) $(NAME) $(OBJ) *.ps *.tar.gz

help:
    @echo "Type 'make; bibtex main; make; make' to get the pdf file"
    @echo "Type 'make clean' to delete all intermediate files"
    @echo "Type 'make delete' to delete all intermediate and output files"

If you copy and paste this makefile to one of yours make sure to put the tabs at the right locations.

An advantage of this approach is that it works smoothly, just typing make after editing your LaTeX files will make all the hard work for you with no other intermediate steps for you to take care of. If you have to process bib references then type: make, then bibtex and make again.

N.N.
  • 36,163
7

Just another way:

% TeX it with pdflatex -shell-escape filename
% filename.tex
\documentclass{article}

\usepackage{filecontents}

%====== begin file contents ======
\begin{filecontents*}{circle.tex}
\documentclass[border=12pt,pstricks]{standalone}
\usepackage{pstricks}
\begin{document}
\begin{pspicture}[showgrid](4,4)
    \pscircle*[linecolor=red](2,2){2}
\end{pspicture}
\end{document}
\end{filecontents*}
%====== end file contents ======


\usepackage{graphicx}
\IfFileExists{circle.pdf}
{% if exist do nothing
 % file age should be taken under consideration but it is ignored for simplicity!
}
{% if not exist do the following
  \immediate\write18{latex circle && dvips circle && ps2pdf -dNOSAFER -dAutoRotatePages=/None circle.ps}%
}
\begin{document}


\begin{figure}
\centering
\includegraphics[scale=2]{circle}
\caption{Red Circle}
\label{fig:RedCircle}
\end{figure}
\end{document}
  • Make sure you compile it with pdflatex --shell-escape filename.tex as I already mentioned at the first comment in the given code. – kiss my armpit Mar 10 '13 at 14:15
1

For completeness, there is a script ps4pdf that runs following commands to convert a .tex file that uses PSTricks into PDF.

latex $1.tex
dvips -Ppdf -o $1-pics.ps $1.dvi
ps2pdf $1-pics.ps $1-pics.pdf
pdflatex $1.tex
bibtex $1
pdflatex $1.tex
morpheus
  • 113