How do I include PSTricks pictures in a LaTeX document?

Say your PSTricks figure file looks like this:

\begin{pspicture}(0,-2.4)(7.72,2.4)
\psframe[linewidth=0.04,dimen=outer](7.72,2.4)(0.0,-2.4)
\end{pspicture} 

saved by the name picture_file.tex in the same folder as your main latex file.

In your latex main file, saved by the name main_file.tex, add

\usepackage{pstricks}    %for embedding pspicture.
\usepackage{graphicx}    %for figure environment.

in the preamble. (You can add additional things also as your needs.)

Now, the picture_file.tex can be included in the main_file.tex as:

%=========================
\begin{figure}[h]
\centering{
\resizebox{0.5\textwidth}{!}{\input{picture_file.tex}}}
\caption{your caption} \label{fig:figureone}
\end{figure}
%===========================

Only the main_file.tex needs be compiled.

Note:-

  1. If you use different fonts other than default ones, the fonts in figures will blend perfectly with the rest of the document by this method.
  2. Make sure the \documentclass[...]{...} does not have minimal or letter as the type, in which case, the figure environment would not be recognized.

If you write each PSTricks diagram in a separate TeX input file and compile it with latex-dvips-ps2pdf (much faster) or xelatex (slower) to obtain a PDF output, you might need the PDF output without white spaces around it.

To obtain the tight PDF output, use the following template for each diagram you want to make.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pstricks-add}

\begin{document}
\begin{pspicture}[showgrid=true](-3,-3)(3,3)
% your drawing code goes here!
\end{pspicture}
\end{document}

It will be better if you put all your TeX input files for creating diagrams in a special folder outside other projects you have. This method will make your diagrams consumable to many projects easily. My directory structures look like below.

enter image description here

All my TeX input file for creating diagrams are placed in Diagrams. The PDF outputs associated with the input files are also in Diagrams for sure.

Another project, namely ProjectA, might use the diagrams you have made before. For example, let Main.tex be the input file in ProjectA folder. You can import the diagrams easily as follows.

\documentclass{article}
\usepackage{graphicx}
\graphicspath{{../Diagrams/}}

\begin{document}
\begin{figure}
\centering
\includegraphics[scale=1.5]{Template}
\caption{This is my PSTricks template.}
\label{fig:Template}
\end{figure}
\end{document}

The important points are:

  • Load graphicx and set the graphics path. Adjust the scale to meet your preference.
  • Compile it with pdflatex or xelatex. You cannot use latex-dvips-ps2pdf unless you have EPS equivalent for each PDF output.

If you have image files (EPS or PDF) you can include them with the graphicx package and \includegraphics{<filename>}. latex needs an EPS and pdflatex a PDF file. If you put both in your folder and omit the suffix in the including macro the package decied which file to use automatically.

If you want to include the PSTricks code directly you may use the {pspicture}environment.

Tags:

Pstricks