# Writing to files through postscript via pstricks

I want to use file operations in postscript: right now I need them to print large debugging data, which otherwise I have to copy and paste from the standard output.

More importantly, in the future, I want to use file operations to cache results of large computations (for example, a rendered 3d figure).

However, the code below does not work. I checked using my gs interpreter: when the code is directly executed via gs, it creates a file successfully. What is the issue? How can I gain standard postscript file access via LaTeX/pstricks? Thanks!

Answer by AlexG (many thanks!) compile with

ps2pdf -dNOSAFER
pdflatex -shell-escape


[Edit:] after a tiny bit of digging into the documentation of auto-pst-pdf I found out what options to turn on to make it work with pdflatex as well :)

That appears to solve the problem! Have not tested the remaining file operations yet. The file needs to be closed too.

%Compile with:
%    pdflatex -shell-escape
\documentclass{article}
\usepackage{pstricks}
\usepackage[dvips={-o -Ppdf}, pspdf={-dNOSAFER -dAutoRotatePages=/None}, pdfcrop={}]{auto-pst-pdf}

\begin{document}
\begin{pspicture}
\pstVerb{
(./myFile.txt) (w) file
closefile
}
\phantom{A}
\end{pspicture}
\end{document}


The issue appears to be one of file access permissions: the error I get is from the postscript interpretter:

This is dvips(k) 5.993 Copyright 2013 Radical Eye Software
(www.radicaleye.com) ' TeX output 2014.10.09:1519' ->
using-files-pstricks-autopp.ps

</usr/share/texlive/texmf-dist/dvips/base/tex.pro>
</usr/share/texlive/texmf-dist/dvips/config/alt-rule.pro>
</usr/share/texlive/texmf-dist/dvips/pstricks/pstricks.pro>

</usr/share/texlive/texmf-dist/dvips/pstricks/pst-algparser.pro>
</usr/share/texlive/texmf-dist/dvips/pst-tools/pst-tools.pro>
</usr/share/texlive/texmf-dist/dvips/pstricks/pst-dots.pro>
</usr/share/texlive/texmf-dist/dvips/base/special.pro>. [1]

Error: /invalidfileaccess in --file-- Operand stack: (./myFile.txt)
(w) Execution stack: %interp_exit .runexec2 --nostringval--
--nostringval-- --nos

tringval-- 2 %stopped_push --nostringval-- --nostringval--
--nostringval-- false 1 %stopped_push 1916 1 3 %oparray_pop 1915 1 3
%oparray_pop 1899 1 3 %oparray_pop 1787 1 3 %oparray_pop
--nostringval-- %errorexec_pop .runexec2 --nostringval--
--nostringval-- --nostringval-- 2 %stopped_push --nostringval--
Dictionary stack: --dict:1176/1684(ro)(G)-- --dict:0/20(G)--
--dict:118/200(L)-- --dict:100/300(L)-- Current allocation mode is
local Last OS error: No such file or directory Current file position
is 85884 GPL Ghostscript 9.10: Unrecoverable error, exit code 1

Prozess endete normal


On tangentially related note, here's the kind of image I am computing and need to cache. This is an example of a Moebius strip, but I can plot arbitrary geometry with reasonably accurate Z-buffering. My contours are much finer than what you get with a simple triangle mesh (my mesh interior is a straight line approximation, but the contours are sampled much finer). Notice how all contour lines on the background are drawn with a dotted line.

The 3d library is rather long, contained here: the needed pstricks-commands.sty file

Modulo my library, here's what the code for drawing the strip will look like:

\documentclass{article}
\usepackage{pstricks}
\usepackage[dvips={-o -Ppdf}, pspdf={-dNOSAFER -dAutoRotatePages=/None}, pdfcrop={}]{auto-pst-pdf}
\usepackage{../pstricks-commands}
%file pstricks-commands to be found here:
%https://sourceforge.net/p/freecalculus/code/651/tree/trunk/lectures/pstricks-commands.sty
\pagestyle{empty}
\begin{document}
\begin{pspicture}(-1, -1)(2,2)
\fcBoundingBox{-3}{-3}{4}{4} %
\fcStartIIIdScene%
\fcSurfaceInScene[iterationsU=33, iterationsV=5]{0}{-1}{360}{1}{%
[10 dict begin
/R 2 def
/r {0.8 v mul} def
/theta u def
/phi {u 0.5 mul} def
/A {phi cos r mul R add} def
theta cos A mul theta sin A mul phi sin r mul
end %
]%
}%
\fcFinishIIIdScene % <- this operation is really expensive.
%need to cache it's output so that it's computed only once for a given input.
\end{pspicture}

\end{document}

• Are you compiling this will all the goodies enabled? --enable-write18 and/or --shell-escape? – Werner Oct 9 '14 at 18:28
• Yes, the -shell-escape option is set. – Todor Milev Oct 9 '14 at 19:19
• That may just be it - it's not allowed. Someone with more PS knowledge can chime in... – Werner Oct 9 '14 at 19:22
• @Werner ps2pdf -dNOSAFER ..., perhaps. – AlexG Oct 10 '14 at 6:19
• -dNOSAFER solved the problem for the dvi->ps->pdf toolchain, thanks a lot! – Todor Milev Oct 14 '14 at 16:52

It makes no real sense to use it with pst-pdf. However, you have to use the postscript environment and a dummy character inside this environment because it will be inserted as an image later.

\documentclass{minimal}
\usepackage{pstricks}
\usepackage[pspdf={-dNOSAFER -dAutoRotatePages=/None}, crop=off]{auto-pst-pdf}

\begin{document}
\begin{postscript}
\pstVerb{
/DataFile (myFile.txt) (w) file def
DataFile (foo) writestring
DataFile (bar) writestring
DataFile closefile
}
\phantom{A}
\end{postscript}

foo
\end{document}


It works for me with pdflatex --shell-escape <file>

• "It makes no real sense to use it with pst-pdf". I understand that pst-pdf uses on the background the dvi->ps->pdf toolchain, so technically running with pdf-latex is redundant. However, I am making lecture slides being compiled on various machines by various people with even more varying technical skills. If someone checks out my repo and compiles with pdflatex, I want no error messages displayed. Would save me 15 minutes of emailing and explanations every time I get a new user of the slides! – Todor Milev Oct 14 '14 at 19:23