# Cropping a pstricks page/image

How do I go about cropping an image to specific x and y dimensions? I've tried using standalone and the crop parameter, but the result is always a full page.

Here's the code I am using right now:

\documentclass[class=article,crop=true]{standalone}
%,pstricks=true
\usepackage{nopageno,pst-plot,graphicx}
%\def\pshlabel#1{\rotatebox{180}{#1}}
%\def\psvlabel#1{\rotatebox{180}{#1}}
\begin{document}
\psset{linecolor=black,linewidth=1pt,arrowsize=8pt,unit=1bp}
\pstVerb{gsave 1 0 0 setrgbcolor clippath fill grestore}
\begin{pspicture}(-160,-160)(160,160)
% Axes
\psset{linewidth=2pt}
\psaxes[Dx=32,Dy=32,labels=all,ticks=all,ticksize=-4pt 4pt,Ox=-128,Oy=-128]{-}(-128,-128)(-128,-128)(128,128)
\uput{10pt}[70](-160,0){{\large\itshape a}}
\uput{10pt}[0](0,-160){{\large\itshape b}}
\end{pspicture}
\end{document}


Incidentally, adding the pstricks=true parameter causes my plot to disappear, as if it were removed from the page.

Thanks.

Here's my latest code.

\documentclass[pstricks,preview,margin=0pt]{standalone}
\usepackage{pst-plot,graphicx}
\def\pshlabel#1{\rotatebox{180}{#1}}
\def\psvlabel#1{\rotatebox{180}{#1}}
\begin{document}
\psset{linecolor=black,bgcolor=red,linewidth=1pt,arrowsize=8pt,unit=1bp}
\begin{pspicture}(-160,-160)(160,160)
% Axes
\psset{linewidth=2pt}
\psaxes[Dx=32,Dy=32,labels=all,ticks=all,ticksize=-4pt 4pt,Ox=-128,Oy=-128]{-}(-128,-128)(-128,-128)(128,128)
\uput{10pt}[70](-160,0){{\large\itshape\rotatebox{180}{a}}}
\uput{10pt}[0](0,-160){{\large\itshape\rotatebox{180}{b}}}
\end{pspicture}
\end{document}


It works!

Use

• pstricks option only to capture the pspicture environment.
• preview option to activate preview package used internally in standalone class. It is also used to avoid getting known errors.
• margin option to specify the margin. Format margin={left bottom right top} but if you want to make all spaces are equal, use margin=<dimen>.

# MWE

\documentclass[pstricks,preview,margin={1cm 2cm 3cm 4cm}]{standalone}
\usepackage{pst-plot}
\psset{linecolor=black,linewidth=1pt,arrowsize=8pt,unit=1bp,linewidth=2pt}
\pstVerb{gsave 1 0 0 setrgbcolor clippath fill grestore}
\begin{document}
\begin{pspicture}(-160,-160)(160,160)
\psaxes[Dx=32,Dy=32,labels=all,ticks=all,ticksize=-4pt 4pt,Ox=-128,Oy=-128]{-}(-128,-128)(-128,-128)(128,128)
\uput{10pt}[70](-160,0){{\large\itshape a}}
\uput{10pt}[0](0,-160){{\large\itshape b}}
\end{pspicture}
\end{document}


Note: The blue frame is intentionally added when converting the PDF to PNG to show the bounding box. The MWE above does not produce this blue frame for sure!

• I want to capture only the region (-160,-160)(160,160). I don't want any margin in addition to that. Is that possible? – posfan12 Nov 28 '16 at 8:51
• What are you using to compile it? I get an error with latex. Package ifplatform Warning: shell escape is disabled, so I can only detect \ifwindows. ) (C:\Users\Mike-7PRO\AppData\Roaming\MiKTeX\2.9\tex\latex\pst-pdf\pst-pdf.sty ! Extra \fi. l.63 \fi ? – posfan12 Nov 28 '16 at 8:58
• It is almost working now. I updated my first post with my latest code. It fails when it reaches the two lines where "rotatebox" appear. – posfan12 Nov 28 '16 at 9:31
• Good catch. That did the trick! – posfan12 Nov 28 '16 at 9:34
• @posfan12: \rotatebox{180}{a} instead of a should suffice. – Money Oriented Programmer Nov 28 '16 at 9:37

Don't use the article option gor the standalone document class. Further, if you want to crop the image to a specific size, given as an argument to the pspicture environment, use the pspicture* environment: as you can see, the letter b which goes outside the frame of (-160,-160)×(160,160) is partially cropped.

\documentclass[crop=true]{standalone}
\usepackage{pst-plot, auto-pst-pdf}
%\def\pshlabel#1{\rotatebox{180}{#1}}
%\def\psvlabel#1{\rotatebox{180}{#1}}

\begin{document}
\psset{linecolor=black,linewidth=1pt,arrowsize=8pt,unit=1bp}
\pstVerb{gsave 1 0 0 setrgbcolor clippath fill grestore}
\begin{pspicture*}(-160,-160)(160,160)
% Axes
\psset{linewidth=2pt}
\psaxes[Dx=32,Dy=32,labels=all,ticks=all,ticksize=-4pt 4pt,Ox=-128,Oy=-128]{-}(-128,-128)(-128,-128)(128,128)
\uput{10pt}[70](-160,0){{\large\itshape a}}
\uput{10pt}[0](0,-160){{\large\itshape b}}
\end{pspicture*}

\end{document}


Don't forget to compile with pdflatex --enable-write18 (MiKTeX) or pdflatex -shell-escape (TeX Live, MacTeX). Alternatively, don't load auto-pst-ˆdf and compile with xelatex.

• Isn't it meant to be red? I used your other answer for my version, though * seems easier. – cfr Nov 28 '16 at 0:11
• Do you know why adding pstricks makes it all go haywire? – cfr Nov 28 '16 at 0:12
• @cfr: No idea. Actually I never used it as option, and your comment made me test it. Same problem. Note the documentation doesn't explicitly mention such an option: it's only in the .cls coe (viz newif\ifsa@pstricks). Wouldn't it be the default? – Bernard Nov 28 '16 at 0:31
• Found it! Don't how I managed to not see it. Feeling stupid… – Bernard Nov 28 '16 at 1:01
• @postfan12: Under MiKTeX, add the --enable-write18 switch to pdflatex. This has to be done in your TeX editor settings (details depend on the editor). – Bernard Nov 28 '16 at 9:12

Do you want something like this?

Compile the following with pdflatex --shell-escape:

% based a bit on Bernard's answer: http://tex.stackexchange.com/a/341143/
\documentclass[]{standalone}
\usepackage{pst-plot,auto-pst-pdf}
\begin{document}
\psset{linecolor=black,bgcolor=red,linewidth=1pt,arrowsize=8pt,unit=1bp}
\begin{pspicture}(-160,-160)(160,160)
% Axes
\psset{linewidth=2pt}
\psaxes[Dx=32,Dy=32,labels=all,ticks=all,ticksize=-4pt 4pt,Ox=-128,Oy=-128]{-}(-128,-128)(-128,-128)(128,128)
\uput{10pt}[70](-160,0){{\large\itshape a}}
\uput{0pt}[90](0,-160){{\large\itshape b}}
\end{pspicture}
\end{document}

• Yes, this looks like what I want. However, the command adds '-autopp' to the end of my ps file name. This is messing up my workflow, since I later convert the ps file to SVG. How do I get rid of this string? – posfan12 Nov 28 '16 at 5:47
• Also, the PDF output looks like this, which is not quite right: i.imgur.com/Rjk6rIY.png – posfan12 Nov 28 '16 at 6:10
• Well, it will have to add something. Most likely you can change the suffix if you look in auto-pst-pdf's documentation. But you can't have \jobname.pdf because that's already in use. So it is renaming the picture so the PDF can then be included. – cfr Nov 28 '16 at 16:38

without standalone:

\documentclass{article}
\usepackage[paperwidth=320pt,paperheight=330pt,margin=0pt]{geometry}
\usepackage{nopageno,pst-plot,graphicx}
\parindent=0pt
\begin{document}
\psset{linewidth=1pt,arrowsize=8pt,unit=1pt}
\begin{pspicture}(-160,-170)(160,160)
\psframe*[linecolor=red](-160,-170)(160,160)
% Axes
\psset{linewidth=2pt}
\psaxes[Dx=32,Dy=32,labels=all,ticks=all,ticksize=-4pt
4pt,Ox=-128,Oy=-128]{-}(-128,-128)(-128,-128)(128,128)
\uput{10pt}[70](-160,0){{\large\itshape a}}
\uput{10pt}[0](0,-160){{\large\itshape b}}
\end{pspicture}
\end{document}