# Problem with pstricks and htlatex/tex4ht

Consider the following MWE:

\documentclass{article}
\usepackage{pst-all}

\begin{document}
\begin{pspicture}(0,-3)(12,5)
\psline[linewidth=2pt]{->}(1.5,0)(0,0)
\end{pspicture}
\end{document}


This compiles fine with latex (not with pdflatex) but when run with htlatex it leads to the ghostscript error:

System call: dvips -E -q -Ppdf -f q.idv -pp 2 > zzq.ps
System return: 0
System call: gs -sDEVICE=pngalpha -sOutputFile=q0x.png -r110x110 -dEPSCrop -dBackgroundColor=16#ffffff -dTextAlphaBits=2 -dGraphicsAlphaBits=2 -q -dbatch -dNOPAUSE zzq.ps -c quit
Error: /configurationerror in --setpagedevice--
Operand stack:
--nostringval--   (%%BoundingBox: 72 72 72 72)   --dict:1/2(ro)(G)--   --nostringval--   --nostringval--   (%%BoundingBox: 72 72 72 72)   1   72   72   false   --dict:1/1(L)--
Execution stack:
%interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   1951   1   3   %oparray_pop   1950   1   3   %oparray_pop   --nostringval--   1934   1   3   %oparray_pop   1820   1   3   %oparray_pop   --nostringval--   %errorexec_pop   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   %ztokenexec_continue   (scanner state)   --nostringval--   --nostringval--   --nostringval--   --nostringval--   --nostringval--   --nostringval--   1913   10   3   %oparray_pop   --nostringval--   --nostringval--   --dict:1/10(L)--   --nostringval--   15   %dict_continue
Dictionary stack:
--dict:1190/1684(ro)(G)--   --dict:0/20(G)--   --dict:82/200(L)--
Current allocation mode is local
Last OS error: Invalid argument
Current file position is 169
GPL Ghostscript 9.16: Unrecoverable error, exit code 1
--- Warning --- System return: 256
Entering q.css
Entering q.tmp


This code used to work fine. From other examples I suspect that the problem has something to do with dvips having trouble determining the bounding box for the image (compare with Cannot determine size of graphic -- although the solution given here, of using separate files for each image, is not a viable solution for me), but I have not been able to work out what the problem is.

Does anyone have any ideas?

I've tried some Pstricks examples and it seems that this error happens only sometimes.

You can easily modify the image conversion process with make4ht, which is included in TL 2015. By default, dvips and ghostscript are used for image conversion. It isn't really elegant, dvipng gives better results (with anti-aliasing), but it seems that it doesn't support pstricks. Other possible solution is to use dvisvgm, which seems best to me, as vector images are superior to bitmaps for drawings.

I've created a build file for make4ht, with two possible options for image conversion: dvisvgm for svg and dvips + gs for png. The second option uses exactly the same options as default tex4ht ones. It doesn't work for your sample file, but it may work for other images.

The file is named hello.mk4:

if mode == "draft" then
Make:htlatex{}
else
Make:htlatex{}
Make:htlatex{}
Make:htlatex{}
end

Make:image("svg$", "dvisvgm -n -o${output}  -p ${page}${source}")

Make:image("png$",function(opt) local dvips = "dvips -E -q -Ppdf -f -pp${page} ${source} -o zz${input}.ps" % opt
local gs = "gs -sDEVICE=pngalpha -sOutputFile=${output} -r110x110 -dEPSCrop -dBackgroundColor=16#ffffff -dTextAlphaBits=2 -dGraphicsAlphaBits=2 -q -dbatch -dNOPAUSE zz${input}.ps -c quit" % opt
print(dvips)
os.execute(dvips)
print(gs)
os.execute(gs)
end)


it is a Lua script, first few lines drives number of LaTeX runs (mode variable is set with -m option for make4ht, so make4ht -m draft filename will run LaTeX only one time, it is real time saver in comparison with htlatex)

More interesting are Make:image calls, first parameter is file name pattern, it is Lua regex, so it matches svg or png at the end of filename. Second parameter may be string template, which will be then executed, or function, where you may run commands directly with os.execute function. This is used for png conversion. I included this version mainly for educational purposes, I would use svg

As png pictures are produced by tex4ht by default, you also need to configure the output to svg. It can be enabled with \Configure{Picture}{.svg} in the custom config file (hello.cfg):

\Preamble{xhtml}
\ConfigureEnv{psmatrix}{\Picture*{}}{\EndPicture}{}{}
\Configure{Picture}{.svg}
\begin{document}
\EndPreamble


compile the file with

 make4ht -m draft -c hello.cfg -e hello.mk4 filename


the result:

and some more interesting example to illustrate the difference between default png and svg conversion.

png version:

and the svg:

Yes, the png version is really truncated, I don't know whether it was caused by wrong options for dvips or for gs.

• Thanks. I think that using svg solves my problem. I haven't quite gotten my application working yet as the real code is hidden in a python app but I am hopeful! – Andrew Aug 11 '15 at 5:19
• Michal, thanks very much for this. it has taken me a while to get back to this and now I fully appreciate and understand your code. Thanks also for make4ht, it is making my life much easier! (I'm cleaning up a package that I have for converting latex files into html quizzes and your code is letting me support pstricks images in the quizzes.) – Andrew Mar 9 '17 at 1:02
• @Andrew thanks for your kind words. We fixed the svg support in tex4ht in the meantime, so it should work even without .mk4 file now. – michal.h21 Mar 9 '17 at 11:07
• Really?! Perhaps I am doing something wrong but the exampke above does not work for me if I have \ConfigureEnv{psmatrix}{\Picture*{}}{\EndPicture}{}{} \Configure{Picture}{.svg} in my cfg file and run this through tex4ht/make4ht. If this fix in texlive2016? – Andrew Mar 12 '17 at 13:26
• @Andrew yes, it works for me in up to date TL 2016. tex4ht uses dvisvgm for conversion when svg pictures are requested now. – michal.h21 Mar 12 '17 at 16:10